public void initWithSpriteFrame(CCSpriteFrame spriteFrame) { // shader program // self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor]; // _dirty = _recursiveDirty = false; _opacityModifyRGB = true; // _blendFunc.src = CC_BLEND_SRC; // _blendFunc.dst = CC_BLEND_DST; _flipY = _flipX = false; // default transform anchor: center _anchorPoint = new Vector2(0.5f, 0.5f); // zwoptex default values // _offsetPosition = CGPointZero; // _hasChildren = false; // _batchNode = null; // clean the Quad // bzero(&_quad, sizeof(_quad)); // Atlas: Color // ccColor4B tmpColor = {255,255,255,255}; // _quad.bl.colors = tmpColor; // _quad.br.colors = tmpColor; // _quad.tl.colors = tmpColor; // _quad.tr.colors = tmpColor; _quadColor = new Color32(255, 255, 255, 255); this.displayedFrame = spriteFrame; // [self setTexture:texture]; // [self setTextureRect:rect rotated:rotated untrimmedSize:rect.size]; // by default use "Self Render". // if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render" // [self setBatchNode:nil]; }
/// ----------------------------------------------------------------------- /// @name Initializing a CCAnimationFrame Object /// ----------------------------------------------------------------------- /** * Initializes and returns an Animation Frame object using the specified frame name, delay units and user info values. * * @param spriteFrame Sprite Frame. * @param delayUnits Delay time units. * @param userInfo Custom dictionary. * * @return An initialized CCAnimationFrame Object. */ public CCAnimationFrame(CCSpriteFrame spriteFrame, float delayUnits, NSDictionary userInfo) { this.spriteFrame = spriteFrame; this.delayUnits = delayUnits; this.userInfo = userInfo; }
void parseVersion2(NSDictionary animations) { CCSpriteFrameCache frameCache = CCSpriteFrameCache.sharedSpriteFrameCache; var enumerator = animations.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> kv = enumerator.Current; string name = (string)kv.Key; NSDictionary animationDict = (NSDictionary)kv.Value; int loops = 0; object loopsObj = loops; if (!animationDict.TryGetValue("loops", out loopsObj)) { loops = 1; } else { loops = (int)loopsObj; } bool restoreOriginalFrame = (bool)animationDict["restoreOriginalFrame"]; NSArray frameArray = (NSArray)animationDict["frames"]; if (frameArray == null) { CCDebug.Log(@"cocos2d: CCAnimationCache: Animation '%@' found in dictionary without any frames - cannot add to animation cache.", name); continue; } // Array of AnimationFrames List <CCAnimationFrame> array = new List <CCAnimationFrame>(frameArray.Count); var frameArrayEnumerator = frameArray.GetEnumerator(); while (frameArrayEnumerator.MoveNext()) { NSDictionary entry = (NSDictionary)frameArrayEnumerator.Current; string spriteFrameName = (string)entry["spriteframe"]; CCSpriteFrame spriteFrame = frameCache.spriteFrameByName(spriteFrameName); if (spriteFrame == null) { CCDebug.Log("cocos2d: CCAnimationCache: Animation '{0}' refers to frame '{1}' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.", name, spriteFrameName); continue; } float delayUnits = float.Parse(entry["delayUnits"].ToString()); NSDictionary userInfo = entry.objectForKey <NSDictionary>("notification"); CCAnimationFrame animFrame = new CCAnimationFrame(spriteFrame, delayUnits, userInfo); array.Add(animFrame); } float delayPerUnit = (float)animationDict["delayPerUnit"]; CCAnimation animation = new CCAnimation(array, delayPerUnit, (uint)loops); animation.restoreOriginalFrame = restoreOriginalFrame; CCAnimationCache.sharedAnimationCache.addAnimation(animation, name); } }
public CCSprite(CCSpriteFrame frame) { initWithSpriteFrame(frame); }
public void setDisabledSpriteFrame(CCSpriteFrame frame) { this.disabledImage = new CCSprite(frame); }
public void setSelectedSpriteFrame(CCSpriteFrame frame) { this.selectedImage = new CCSprite(frame); }
// // Setter of sprite frames // public void setNormalSpriteFrame(CCSpriteFrame frame) { this.normalImage = new CCSprite(frame); }
void addSpriteFrames(NSDictionary dictionary, Sprite[] sprites) { Dictionary <string, Sprite> spritesDict = new Dictionary <string, Sprite> (); for (int i = 0; i < sprites.Length; i++) { Sprite s = sprites[i]; spritesDict.Add(s.name, s); } NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); int format = 0; // get the format if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // SpriteFrame info // Rect rect = new Rect(); bool isRotated = false; Vector2 frameOffset = Vector2.zero; Vector2 originalSize = Vector2.zero; // add real frames var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; CCSpriteFrame spriteFrame = null; if (format == 0) { // float x = frameDict.objectForKey<float>("x"); // float y = frameDict.objectForKey<float>("y"); // float w = frameDict.objectForKey<float>("width"); // float h = frameDict.objectForKey<float>("height"); float ox = frameDict.objectForKey <float>("offsetX"); float oy = frameDict.objectForKey <float>("offsetY"); int ow = frameDict.objectForKey <int>("originalWidth"); int oh = frameDict.objectForKey <int>("originalHeight"); // check ow/oh if (ow == 0 || oh == 0) { CCDebug.Warning("cocos2d: WARNING: originalWidth/Height not found on the CCSpriteFrame. AnchorPoint won't work as expected. Regenerate the .plist"); } // abs ow/oh ow = Math.Abs(ow); oh = Math.Abs(oh); // set frame info // rect = new Rect(x, y, w, h); isRotated = false; frameOffset = new Vector2(ox, oy); originalSize = new Vector2(ow, oh); // if(isRotated) // rect.size = new Vector2(rect.size.y, rect.size.x); } else if (format == 1 || format == 2) { // Rect frame = ccUtils.RectFromString(frameDict.objectForKey<string>("frame")); bool rotated = false; // rotation if (format == 2) { rotated = frameDict.objectForKey <bool>("rotated"); } Vector2 offset = ccUtils.PointFromString(frameDict.objectForKey <string>("offset")); Vector2 sourceSize = ccUtils.PointFromString(frameDict.objectForKey <string>("sourceSize")); // set frame info // rect = frame; isRotated = rotated; frameOffset = offset; originalSize = sourceSize; } else if (format == 3) { // get values // Vector2 spriteSize = ccUtils.PointFromString(frameDict.objectForKey<string>("spriteSize")); Vector2 spriteOffset = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteOffset")); Vector2 spriteSourceSize = ccUtils.PointFromString(frameDict.objectForKey <string>("spriteSourceSize")); // Rect textureRect = ccUtils.RectFromString(frameDict.objectForKey<string>("textureRect")); bool textureRotated = frameDict.objectForKey <bool>("textureRotated"); // get aliases NSArray aliases = frameDict.objectForKey <NSArray>("aliases"); var aliasesEnumerator = aliases.GetEnumerator(); while (aliasesEnumerator.MoveNext()) { string alias = (string)aliasesEnumerator.Current; if (_spriteFramesAliases.ContainsKey(alias)) { CCDebug.Warning("cocos2d: WARNING: an alias with name {0} already exists", alias); } _spriteFramesAliases[alias] = frameDictKey; } // set frame info // rect = new Rect(textureRect.position.x, textureRect.position.y, spriteSize.x, spriteSize.y); isRotated = textureRotated; frameOffset = spriteOffset; originalSize = spriteSourceSize; } Sprite spt; if (!spritesDict.TryGetValue(frameDictKey, out spt)) { CCDebug.Warning("cocos2d: WARNING: a sprite frame with name {0} not found", frameDictKey); continue; } // add sprite frame spriteFrame = new CCSpriteFrame(spt, originalSize, frameOffset, isRotated); _spriteFrames.Add(frameDictKey, spriteFrame); } }
/** Adds an sprite frame with a given name. * If the name already exists, then the contents of the old name will be replaced with the new one. */ public void addSpriteFrame(CCSpriteFrame frame) { _spriteFrames[frame.textureFilename] = frame; }