public SpriteOffsetAnchorSkewScale() { CCSize s = CCDirector.sharedDirector().getWinSize(); for (int i = 0; i < 3; i++) { CCSpriteFrameCache cache = CCSpriteFrameCache.sharedSpriteFrameCache(); cache.addSpriteFramesWithFile("animations/grossini"); cache.addSpriteFramesWithFile("animations/grossini_gray", "animations/images/grossini_gray"); // // Animation using Sprite batch // CCSprite sprite = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); sprite.position = (new CCPoint(s.width / 4 * (i + 1), s.height / 2)); CCSprite point = CCSprite.spriteWithFile("Images/r1"); point.scale = 0.25f; point.position = sprite.position; addChild(point, 1); switch (i) { case 0: sprite.anchorPoint = new CCPoint(0, 0); break; case 1: sprite.anchorPoint = (new CCPoint(0.5f, 0.5f)); break; case 2: sprite.anchorPoint = (new CCPoint(1, 1)); break; } point.position = sprite.position; List <CCSpriteFrame> animFrames = new List <CCSpriteFrame>(); string tmp = ""; for (int j = 0; j < 14; j++) { string temp = ""; if (j + 1 < 10) { temp = "0" + (j + 1); } else { temp = (j + 1).ToString(); } tmp = string.Format("grossini_dance_{0}.png", temp); CCSpriteFrame frame = cache.spriteFrameByName(tmp); animFrames.Add(frame); } CCAnimation animation = CCAnimation.animationWithFrames(animFrames); sprite.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithDuration(2.8f, animation, false))); animFrames = null; // Skew CCSkewBy skewX = CCSkewBy.actionWithDuration(2, 45, 0); CCActionInterval skewX_back = (CCActionInterval)skewX.reverse(); CCSkewBy skewY = CCSkewBy.actionWithDuration(2, 0, 45); CCActionInterval skewY_back = (CCActionInterval)skewY.reverse(); CCFiniteTimeAction seq_skew = CCSequence.actions(skewX, skewX_back, skewY, skewY_back); sprite.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq_skew)); // Scale CCScaleBy scale = CCScaleBy.actionWithDuration(2, 2); CCActionInterval scale_back = (CCActionInterval)scale.reverse(); CCFiniteTimeAction seq_scale = CCSequence.actions(scale, scale_back); sprite.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq_scale)); addChild(sprite, 0); } }
public SpriteBatchNodeOffsetAnchorRotation() { CCSize s = CCDirector.sharedDirector().getWinSize(); for (int i = 0; i < 3; i++) { CCSpriteFrameCache cache = CCSpriteFrameCache.sharedSpriteFrameCache(); cache.addSpriteFramesWithFile("animations/grossini"); cache.addSpriteFramesWithFile("animations/grossini_gray", "animations/images/grossini_gray"); // // Animation using Sprite BatchNode // CCSprite sprite = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); sprite.position = (new CCPoint(s.width / 4 * (i + 1), s.height / 2)); CCSprite point = CCSprite.spriteWithFile("Images/r1"); point.scale = 0.25f; point.position = sprite.position; addChild(point, 200); switch (i) { case 0: sprite.anchorPoint = new CCPoint(0, 0); break; case 1: sprite.anchorPoint = new CCPoint(0.5f, 0.5f); break; case 2: sprite.anchorPoint = new CCPoint(1, 1); break; } point.position = sprite.position; CCSpriteBatchNode spritebatch = CCSpriteBatchNode.batchNodeWithFile("animations/images/grossini"); addChild(spritebatch); List <CCSpriteFrame> animFrames = new List <CCSpriteFrame>(14); string str = ""; for (int k = 0; k < 14; k++) { string temp = ""; if (k + 1 < 10) { temp = "0" + (k + 1); } else { temp = k + 1 + ""; } str = string.Format("grossini_dance_{0}.png", temp); CCSpriteFrame frame = cache.spriteFrameByName(str); animFrames.Add(frame); } CCAnimation animation = CCAnimation.animationWithFrames(animFrames); sprite.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animation, false))); sprite.runAction(CCRepeatForever.actionWithAction(CCRotateBy.actionWithDuration(10, 360))); spritebatch.addChild(sprite, i); //animFrames.release(); // win32 : memory leak 2010-0415 } }
public SpriteFrameTest() { CCSize s = CCDirector.sharedDirector().getWinSize(); // IMPORTANT: // The sprite frames will be cached AND RETAINED, and they won't be released unless you call // CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames); CCSpriteFrameCache cache = CCSpriteFrameCache.sharedSpriteFrameCache(); cache.addSpriteFramesWithFile("animations/grossini"); cache.addSpriteFramesWithFile("animations/grossini_gray", "animations/images/grossini_gray"); cache.addSpriteFramesWithFile("animations/grossini_blue", "animations/images/grossini_blue"); // // Animation using Sprite BatchNode // m_pSprite1 = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); m_pSprite1.position = (new CCPoint(s.width / 2 - 80, s.height / 2)); CCSpriteBatchNode spritebatch = CCSpriteBatchNode.batchNodeWithFile("animations/images/grossini"); spritebatch.addChild(m_pSprite1); addChild(spritebatch); List <CCSpriteFrame> animFrames = new List <CCSpriteFrame>(15); string str = ""; for (int i = 1; i < 15; i++) { string temp; if (i < 10) { temp = "0" + i; } else { temp = i.ToString(); } str = string.Format("grossini_dance_{0}.png", temp); CCSpriteFrame frame = cache.spriteFrameByName(str); animFrames.Add(frame); } CCAnimation animation = CCAnimation.animationWithFrames(animFrames); m_pSprite1.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animation, false))); // to test issue #732, uncomment the following line m_pSprite1.IsFlipX = false; m_pSprite1.IsFlipY = false; // // Animation using standard Sprite // m_pSprite2 = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); m_pSprite2.position = (new CCPoint(s.width / 2 + 80, s.height / 2)); addChild(m_pSprite2); List <CCSpriteFrame> moreFrames = new List <CCSpriteFrame>(20); for (int i = 1; i < 15; i++) { string temp; if (i < 10) { temp = "0" + i; } else { temp = i.ToString(); } str = string.Format("grossini_dance_gray_{0}.png", temp); CCSpriteFrame frame = cache.spriteFrameByName(str); moreFrames.Add(frame); } for (int i = 1; i < 5; i++) { str = string.Format("grossini_blue_0{0}.png", i); CCSpriteFrame frame = cache.spriteFrameByName(str); moreFrames.Add(frame); } // append frames from another batch moreFrames.AddRange(animFrames); CCAnimation animMixed = CCAnimation.animationWithFrames(moreFrames); m_pSprite2.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animMixed, false))); // to test issue #732, uncomment the following line m_pSprite2.IsFlipX = (false); m_pSprite2.IsFlipY = (false); schedule(startIn05Secs, 0.5f); m_nCounter = 0; }
public override void onEnter() { base.onEnter(); CCSize s = CCDirector.sharedDirector().getWinSize(); // IMPORTANT: // The sprite frames will be cached AND RETAINED, and they won't be released unless you call // [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; // // CCSpriteFrameCache is a cache of CCSpriteFrames // CCSpriteFrames each contain a texture id and a rect (frame). CCSpriteFrameCache cache = CCSpriteFrameCache.sharedSpriteFrameCache(); cache.addSpriteFramesWithFile("animations/grossini-aliases", "animations/images/grossini-aliases"); // // Animation using Sprite batch // // A CCSpriteBatchNode can reference one and only one texture (one .png file) // Sprites that are contained in that texture can be instantiatied as CCSprites and then added to the CCSpriteBatchNode // All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call // If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient // // When you animate a sprite, CCAnimation changes the frame of the sprite using setDisplayFrame: (this is why the animation must be in the same texture) // When setDisplayFrame: is used in the CCAnimation it changes the frame to one specified by the CCSpriteFrames that were added to the animation, // but texture id is still the same and so the sprite is still a child of the CCSpriteBatchNode, // and therefore all the animation sprites are also drawn as part of the CCSpriteBatchNode // CCSprite sprite = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); sprite.position = (new CCPoint(s.width * 0.5f, s.height * 0.5f)); CCSpriteBatchNode spriteBatch = CCSpriteBatchNode.batchNodeWithFile("animations/images/grossini-aliases"); spriteBatch.addChild(sprite); addChild(spriteBatch); List <CCSpriteFrame> animFrames = new List <CCSpriteFrame>(15); string str = ""; for (int i = 1; i < 15; i++) { string temp = ""; if (i < 10) { temp = "0" + i; } else { temp = i.ToString(); } // Obtain frames by alias name str = string.Format("dance_{0}", temp); CCSpriteFrame frame = cache.spriteFrameByName(str); animFrames.Add(frame); } CCAnimation animation = CCAnimation.animationWithFrames(animFrames); // 14 frames * 1sec = 14 seconds sprite.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithDuration(14.0f, animation, false))); }
public SpriteBatchNodeOffsetAnchorFlip() { CCSize s = CCDirector.sharedDirector().getWinSize(); for (int i = 0; i < 3; i++) { CCSpriteFrameCache cache = CCSpriteFrameCache.sharedSpriteFrameCache(); cache.addSpriteFramesWithFile("animations/grossini"); cache.addSpriteFramesWithFile("animations/grossini_gray", "animations/images/grossini_gray"); // // Animation using Sprite batch // CCSprite sprite = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); sprite.position = (new CCPoint(s.width / 4 * (i + 1), s.height / 2)); CCSprite point = CCSprite.spriteWithFile("Images/r1"); point.scale = 0.25f; point.position = sprite.position; addChild(point, 200); switch (i) { case 0: sprite.anchorPoint = new CCPoint(0, 0); break; case 1: sprite.anchorPoint = new CCPoint(0.5f, 0.5f); break; case 2: sprite.anchorPoint = (new CCPoint(1, 1)); break; } point.position = sprite.position; CCSpriteBatchNode spritebatch = CCSpriteBatchNode.batchNodeWithFile("animations/images/grossini"); addChild(spritebatch); List <CCSpriteFrame> animFrames = new List <CCSpriteFrame>(); string tmp = ""; for (int j = 0; j < 14; j++) { string temp = ""; if (i + 1 < 10) { temp = "0" + (i + 1); } else { temp = (i + 1).ToString(); } tmp = string.Format("grossini_dance_{0}.png", temp); CCSpriteFrame frame = cache.spriteFrameByName(tmp); animFrames.Add(frame); } CCAnimation animation = CCAnimation.animationWithFrames(animFrames); sprite.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithDuration(2.8f, animation, false))); animFrames = null; CCFlipY flip = CCFlipY.actionWithFlipY(true); CCFlipY flip_back = CCFlipY.actionWithFlipY(false); CCDelayTime delay = CCDelayTime.actionWithDuration(1); CCFiniteTimeAction seq = CCSequence.actions((CCFiniteTimeAction)delay, (CCFiniteTimeAction)flip, (CCFiniteTimeAction)delay.copyWithZone(null), (CCFiniteTimeAction)flip_back); sprite.runAction(CCRepeatForever.actionWithAction((CCActionInterval)seq)); spritebatch.addChild(sprite, i); } }
public SpriteOffsetAnchorScale() { CCSize s = CCDirector.sharedDirector().getWinSize(); for (int i = 0; i < 3; i++) { CCSpriteFrameCache cache = CCSpriteFrameCache.sharedSpriteFrameCache(); cache.addSpriteFramesWithFile("animations/grossini"); cache.addSpriteFramesWithFile("animations/grossini_gray", "animations/images/grossini_gray"); // // Animation using Sprite BatchNode // CCSprite sprite = CCSprite.spriteWithSpriteFrameName("grossini_dance_01.png"); sprite.position = (new CCPoint(s.width / 4 * (i + 1), s.height / 2)); CCSprite point = CCSprite.spriteWithFile("Images/r1"); point.scale = 0.25f; point.position = sprite.position; addChild(point, 1); switch (i) { case 0: sprite.anchorPoint = new CCPoint(0, 0); break; case 1: sprite.anchorPoint = new CCPoint(0.5f, 0.5f); break; case 2: sprite.anchorPoint = new CCPoint(1, 1); break; } point.position = sprite.position; List <CCSpriteFrame> animFrames = new List <CCSpriteFrame>(14); string str = ""; for (int j = 0; j < 14; j++) { string temp = ""; if (j + 1 < 10) { temp = "0" + (j + 1); } else { temp = (j + 1).ToString(); } str = string.Format("grossini_dance_{0}.png", temp); CCSpriteFrame frame = cache.spriteFrameByName(str); animFrames.Add(frame); } CCAnimation animation = CCAnimation.animationWithFrames(animFrames); sprite.runAction(CCRepeatForever.actionWithAction(CCAnimate.actionWithAnimation(animation, false))); CCActionInterval scale = CCScaleBy.actionWithDuration(2, 2); CCActionInterval scale_back = (CCActionInterval)scale.reverse(); CCActionInterval seq_scale = (CCActionInterval)(CCSequence.actions(scale, scale_back)); sprite.runAction(CCRepeatForever.actionWithAction(seq_scale)); addChild(sprite, 0); //animFrames->release(); // win32 : memory leak 2010-0415 } }