This project is read-only.
Creating your first animation requires some thought and finesse. You must first have a sprite sheet or the individual frames of your animation. The sprite sheet should be 2048 x 2048 at the largest because XNA4 can only process that size texture (or smaller, but power-of-two required).

Next, you create your display sprite, which is the visible sprite for your animation. The CCAnimation will use this as the drawing canvas for your frames.

CCSprite sprite1 = new CCSprite("images/dot"); // NOTE: No extension!
sprite1.Position = (new CCPoint(0, 0));
Animator = sprite1;

Now that you have the placeholder sprite, you need to setup the sprite batch. This is where the magic happens, but remember that you need to bind the CCSpriteBatch instance to the placeholder sprite:
CCSpriteBatchNode spritebatch = new CCSpriteBatchNode("MyAnimation_Sprites");

Now for the serious part. You have to load all of the frames into your animation This is done in the following code, assuming that you hare already loaded your sprites into the sprite frame cache.

var animFrames = new List<CCSpriteFrame>(frames);
var cache = CCSpriteFrameCache.SharedSpriteFrameCache;

string str = string.Empty;
for (int i = 1; i <= framesInMyAnimation; i++)
  str = "frame_name_for_this_index";
  CCSpriteFrame frame = cache.SpriteFrameByName(str);
  if (frame != null)
      CCLog.Log("Oops, {0} is not a valid frame for the animation sequence {1}", str, anim);

Animation = new CCAnimation(animFrames, 0.3f);

Now what can we do with this magic? Setup a method to run the animation. The code below references Animator which is the sprite1 CCSprite created earlier.

internal virtual void StartAnimation()
    if (Animator != null && Animation != null)
        Animator.RunAction(new CCRepeatForever(new CCAnimate(Animation)));

Add the spritebatch CCSpriteBatch object to your display, When you want to move your animation, as if your hero is walking, you set the position of the sprite1 object (the Animator).

Last edited May 24, 2013 at 6:44 PM by totallyevil, version 5


No comments yet.