public override void startWithTarget (object aTarget) { base.startWithTarget (aTarget); CCSprite sprite = _target as CCSprite; _origFrame = null; if( _animation.restoreOriginalFrame ) _origFrame = sprite.displayedFrame; _nextFrame = 0; _executedLoops = 0; }
public void removeSpriteFramesFromTexture(Texture2D texture) { utList <string> keysToRemove = new utList <string> (); var enumerator = _spriteFrames.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <string, CCSpriteFrame> kv = enumerator.Current; string spriteFrameKey = kv.Key; CCSpriteFrame frame = kv.Value; if (frame.texture == texture) { keysToRemove.DL_APPEND(spriteFrameKey); } } for (var ent = keysToRemove.head; ent != null; ent = ent.next) { _spriteFrames.Remove(ent.obj); } }
/** Returns an Sprite Frame that was previously added. * If the name is not found it will return nil. * You should retain the returned copy if you are going to use it. */ public CCSpriteFrame spriteFrameByName(string name) { CCSpriteFrame frame = _spriteFrames [name]; if (frame == null) { // try alias dictionary string key = _spriteFramesAliases[name]; if (key != null) { frame = _spriteFrames [key]; } if (frame == null) { CCDebug.Info("cocos2d: CCSpriteFrameCache: Frame '{0}' not found", name); } } return(frame); }
/** initializes the action with an Animation and will restore the original frame when the animation is over */ public void initWithAnimation(CCAnimation anim){ NSUtils.Assert( anim!=null, "Animate: argument Animation must be non-nil"); float singleDuration = anim.duration; base.initWithDuration (singleDuration * anim.loops); _nextFrame = 0; this.animation = anim; _origFrame = null; _executedLoops = 0; _splitTimes = new List<float> (anim.frames.Count); float accumUnitsOfTime = 0; float newUnitOfTimeValue = singleDuration / anim.totalDelayUnits; var enumerator = anim.frames.GetEnumerator(); while (enumerator.MoveNext()) { var frame = enumerator.Current; float value = (accumUnitsOfTime * newUnitOfTimeValue) / singleDuration; accumUnitsOfTime += frame.delayUnits; _splitTimes.Add(value); } }
protected virtual void updateMesh() { if (_meshDirty) { CCSpriteFrame spriteFrame = _spriteFrame; Rect textureRect = spriteFrame.textureRect; float textureWidth = spriteFrame.texture.width; float textureHeight = spriteFrame.texture.height; float textureRectWidth = textureRect.width; float textureRectHeigh = textureRect.height; Vector2 anchorPointInPixels = this.anchorPointInPixels; Vector2 contentSize = this.contentSize; Vector2 spriteOffset = spriteFrame.offset; float viewWidth = textureRectWidth, viewHeight = textureRectHeigh; if (spriteFrame.rotated) { viewWidth = textureRectHeigh; viewHeight = textureRectWidth; } Vector2 center = spriteOffset + contentSize / 2 - anchorPointInPixels; Rect viewRect = new Rect(center.x - viewWidth / 2, center.y - viewHeight / 2, viewWidth, viewHeight); Rect clippedViewRect = viewRect; if (_clippingEnabled) { clippedViewRect = ccUtils.RectIntersection(viewRect, _clippingRect); } Vector3[] vertices = new Vector3[4] { new Vector3(clippedViewRect.xMin, clippedViewRect.yMax, 0) / UIWindow.PIXEL_PER_UNIT, //left-top new Vector3(clippedViewRect.xMax, clippedViewRect.yMax, 0) / UIWindow.PIXEL_PER_UNIT, //right-top new Vector3(clippedViewRect.xMax, clippedViewRect.yMin, 0) / UIWindow.PIXEL_PER_UNIT, //right-bottom new Vector3(clippedViewRect.xMin, clippedViewRect.yMin, 0) / UIWindow.PIXEL_PER_UNIT //left-bottom }; Vector2[] uvs = new Vector2[4]; float uLeft = textureRect.xMin / textureWidth; float uRight = textureRect.xMax / textureWidth; float vBottom = textureRect.yMin / textureHeight; float vTop = textureRect.yMax / textureHeight; if (!spriteFrame.rotated) { if (_clippingEnabled) { float uLeftPadding = (clippedViewRect.xMin - viewRect.xMin) / textureWidth; float uRightPadding = (clippedViewRect.xMax - viewRect.xMax) / textureWidth; float vBottomPadding = (clippedViewRect.yMin - viewRect.yMin) / textureHeight; float vTopPadding = (clippedViewRect.yMax - viewRect.yMax) / textureHeight; uLeft += uLeftPadding; uRight += uRightPadding; vBottom += vBottomPadding; vTop += vTopPadding; } uvs [0] = new Vector2(uLeft, vTop); //top-left uvs [1] = new Vector2(uRight, vTop); //top-right uvs [2] = new Vector2(uRight, vBottom); //bottom-right uvs [3] = new Vector2(uLeft, vBottom); //bottom-left } else { if (_clippingEnabled) { float vTopPadding = (viewRect.xMin - clippedViewRect.xMin) / textureHeight; float vBottomPadding = (viewRect.xMax - clippedViewRect.xMax) / textureHeight; float uLeftPadding = (clippedViewRect.yMin - viewRect.yMin) / textureWidth; float uRightPadding = (clippedViewRect.yMax - viewRect.yMax) / textureWidth; uLeft += uLeftPadding; uRight += uRightPadding; vBottom += vBottomPadding; vTop += vTopPadding; } uvs [0] = new Vector2(uRight, vTop); //top-right uvs [1] = new Vector2(uRight, vBottom); //bottom-right uvs [2] = new Vector2(uLeft, vBottom); //bottom-left uvs [3] = new Vector2(uLeft, vTop); //top-left } int[] triangles = new int[] { 0, 1, 2, 2, 3, 0 }; Vector3[] normals = new Vector3[] { Vector3.back, Vector3.back, Vector3.back, Vector3.back }; Mesh mesh = this.meshFilter.sharedMesh; if (mesh == null) { mesh = new Mesh(); } else { //in case the old triangle arrary is a big array mesh.triangles = triangles; } mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; mesh.uv = uvs; this.meshFilter.sharedMesh = mesh; _meshDirty = false; } }
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; }
/// ----------------------------------------------------------------------- /// @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; }
public void initWithImageNamed(string imageName){ gameObject.name = imageName; CCSpriteFrame frame = CCSpriteFrameCache.sharedSpriteFrameCache.spriteFrameByName (imageName); if (frame == null) { CCDebug.Info("cocos2d:CCSprite: Try to load '{0}' as a file.", imageName); frame = new CCSpriteFrame (imageName); } initWithSpriteFrame (frame); }
// ------------------------------------------------------------------------------ // edit tile // ------------------------------------------------------------------------------ public Tile addTile(string frameName, Vector2 centerPosInNodeSpace) { CCSpriteFrame frame = CCSpriteFrameCache.sharedSpriteFrameCache.spriteFrameByName(frameName); return(addTile(frame, centerPosInNodeSpace)); }
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); } }
void addSpriteFrames(NSDictionary dictionary, Texture2D texture, string textureFileName) { NSDictionary metadataDict = dictionary.objectForKey <NSDictionary>("metadata"); NSDictionary framesDict = dictionary.objectForKey <NSDictionary>("frames"); // get the format int format = 0; if (metadataDict != null) { format = metadataDict.objectForKey <int> ("format"); } // get texture size Vector2 textureSize = new Vector2(texture.width, texture.height); // check the format NSUtils.Assert(format >= 0 && format <= 3, @"cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:"); // SpriteFrame info Rect rect = new Rect(); bool textureRotated = false; Vector2 spriteOffset = Vector2.zero; Vector2 originalSize = Vector2.zero; bool semi = false; var enumerator = framesDict.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <object, object> frameDictKeyValue = enumerator.Current; string frameDictKey = (string)frameDictKeyValue.Key; NSDictionary frameDict = (NSDictionary)frameDictKeyValue.Value; 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); textureRotated = false; spriteOffset = new Vector2(ox, oy); originalSize = new Vector2(ow, oh); semi = frameDict.objectForKey <bool>("semi"); } 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; textureRotated = rotated; spriteOffset = offset; originalSize = sourceSize; semi = frameDict.objectForKey <bool>("semi"); } 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); textureRotated = textureRotated_; spriteOffset = spriteOffset_; originalSize = spriteSourceSize; semi = frameDict.objectForKey <bool>("semi"); } if (textureRotated) { rect.size = new Vector2(rect.size.y, rect.size.x); } rect.y = textureSize.y - rect.y - rect.height; // add sprite frame CCSpriteFrame spriteFrame = new CCSpriteFrame(texture, rect, textureRotated, spriteOffset, originalSize, semi); spriteFrame.frameFileName = frameDictKey; spriteFrame.textureFilename = textureFileName; _spriteFrames [frameDictKey] = spriteFrame; } }
public Tile addTile(CCSpriteFrame frame, Vector2 centerPosInNodeSpace) { return(addTile(frame, centerPosInNodeSpace, 1, 1, 0)); }
public Tile addTile(string frameName, Vector2 centerPosInNodeSpace, float scaleX, float scaleY, float degree) { CCSpriteFrame frame = CCSpriteFrameCache.sharedSpriteFrameCache.spriteFrameByName(frameName); return(addTile(frame, centerPosInNodeSpace, scaleX, scaleY, degree)); }
public CCSprite(CCSpriteFrame spriteFrame) { gameObject.name = spriteFrame.frameFileName; initWithSpriteFrame(spriteFrame); }
public void initWithSpriteFrame(CCSpriteFrame spriteFrame) { _flipY = _flipX = false; _anchorPoint = new Vector2(0.5f, 0.5f); this.displayedFrame = spriteFrame; }
public CCSprite(CCSpriteFrame frame){ initWithSpriteFrame (frame); }
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]; }
void updateMesh() { if (_meshDirty) { int tilesCount = _tiles.DL_COUNT(); Vector3[] vertices = new Vector3[tilesCount * 4]; Vector3[] normals = new Vector3[vertices.Length]; Vector2[] uvs = new Vector2[vertices.Length]; int[] triangles = new int[tilesCount * 6]; int tileIndex = 0; for (var ent = _tiles.head; ent != null; ent = ent.next) { Tile tile = ent.obj; CCSpriteFrame spriteFrame = tile.spriteFrame; Rect uTextureRect = spriteFrame.textureRect; uTextureRect.position /= UIWindow.PIXEL_PER_UNIT; uTextureRect.size /= UIWindow.PIXEL_PER_UNIT; float uTextureWidth = spriteFrame.texture.width / UIWindow.PIXEL_PER_UNIT; float uTextureHeight = spriteFrame.texture.height / UIWindow.PIXEL_PER_UNIT; float uTextureRectWidth = uTextureRect.width; float uTextureRectHeigh = uTextureRect.height; Vector2 uSpriteOffset = spriteFrame.offset / UIWindow.PIXEL_PER_UNIT; Vector2 center = uSpriteOffset; float centerX = center.x; float centerY = center.y; float uViewWidth = uTextureRectWidth, uViewHeight = uTextureRectHeigh; if (spriteFrame.rotated) { uViewWidth = uTextureRectHeigh; uViewHeight = uTextureRectWidth; } int verticesIndex = tileIndex * 4; vertices [verticesIndex + 0] = new Vector3(centerX - uViewWidth / 2, centerY + uViewHeight / 2, 0); vertices [verticesIndex + 1] = new Vector3(centerX + uViewWidth / 2, centerY + uViewHeight / 2, 0); vertices [verticesIndex + 2] = new Vector3(centerX + uViewWidth / 2, centerY - uViewHeight / 2, 0); vertices [verticesIndex + 3] = new Vector3(centerX - uViewWidth / 2, centerY - uViewHeight / 2, 0); int trianglesIndex = tileIndex * 6; triangles [trianglesIndex + 0] = verticesIndex + 0; triangles [trianglesIndex + 1] = verticesIndex + 1; triangles [trianglesIndex + 2] = verticesIndex + 2; triangles [trianglesIndex + 3] = verticesIndex + 2; triangles [trianglesIndex + 4] = verticesIndex + 3; triangles [trianglesIndex + 5] = verticesIndex + 0; float uLeft = uTextureRect.xMin / uTextureWidth; float vBottom = uTextureRect.yMin / uTextureHeight; float uRight = uTextureRect.xMax / uTextureWidth; float vTop = uTextureRect.yMax / uTextureHeight; if (!spriteFrame.rotated) { uvs [verticesIndex + 0] = new Vector2(uLeft, vTop); //top-left uvs [verticesIndex + 1] = new Vector2(uRight, vTop); //top-right uvs [verticesIndex + 2] = new Vector2(uRight, vBottom); //bottom-right uvs [verticesIndex + 3] = new Vector2(uLeft, vBottom); //bottom-left } else { uvs [verticesIndex + 0] = new Vector2(uRight, vTop); //top-right uvs [verticesIndex + 1] = new Vector2(uRight, vBottom); //bottom-right uvs [verticesIndex + 2] = new Vector2(uLeft, vBottom); //bottom-left uvs [verticesIndex + 3] = new Vector2(uLeft, vTop); //top-left } //apply transform if (!CGAffineTransform.IsIdentity(tile.transform)) { for (int i = 0; i < 4; i++) { Vector2 vertex = vertices [verticesIndex + i]; vertex = CGAffineTransform.CGPointApplyAffineTransform(vertex, tile.transform); vertices [verticesIndex + i] = vertex; } } tileIndex++; } for (int i = 0; i < normals.Length; i++) { normals [i] = Vector3.back; } Mesh mesh = this.meshFilter.sharedMesh; if (mesh == null) { mesh = new Mesh(); } mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; mesh.uv = uvs; this.meshFilter.sharedMesh = mesh; _meshDirty = false; } }