How to animate sprites?

FilipCinikFilipCinik PLMember

Hello,
I want to animate sprite for my game, but I have problems with running the action. Unfortunately I get "An unhandled exception occured.".

This is what I wrote yet.
It breaks at target.AddAction(anim);.


class ObstacleAnimation : CCFiniteTimeAction
{
public ObstacleAnimation(CCNode target)
{
List frames = new List();
for (int index = 0; index < 7; index++)
{
CCTexture2D texture = new CCTexture2D("hd/animations/Obstacle/CartoonBird" + index);
CCSpriteFrame frame = new CCSpriteFrame(texture, new CCRect(Constants.GAME_WIDTH_CENTER, Constants.GAME_HEIGHT_CENTER, 159, 201));
frames.Add(frame);
}

        CCAnimation animation = new CCAnimation(frames, 0.1f);
        CCAnimate anim = new CCAnimate(animation);

        target.AddAction(anim);

        //StartAction(target);
    }

    protected override CCActionState StartAction(CCNode target)
    {
        return base.StartAction(target);
    }

/*
...
*/
}

Posts

  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai

    Hi Filip,

    We would have to get more details on the type of exception you encountered. However, as a first step, I would check to make sure that your texture is loading correctly.

    Just in case you're unsure on how to do this, check out the documentation here which provides a number of resources that guide you through this.

  • FilipCinikFilipCinik PLMember

    Ok, so because the previous code didn't work I copied new code from this thread:
    https://forums.xamarin.com/discussion/comment/131552#Comment_131552 from Kenneth Pouncey.
    Now it throws "System.InvalidCastException: Cannot cast from source type to destination type." at obs1.RunAction(new CCRepeatForever(planeAnimate)); line.

    I checked and CCAnimate contains textures so the problem is obs1.RunAction(new CCRepeatForever(planeAnimate));

    obs1 is a CCNode with sprite and body.

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    Hey GreeScreen

    What exactly are you trying to do? It looks like you are creating a custom CCFiniteTimeAction to run other actions. Not exactly sure what it is you are trying to achieve with that setup. Could you send a very simple project solution so that we can see from start to finish what you are trying to do.

    Have you tried doing that without a the custom action definition and code and just use a simple setup to see if that works. There are multiple examples here:

    Here is a custom action project with the custom action source here:

  • FilipCinikFilipCinik PLMember

    I want to apply simple sprite animation to my CCNode. That's all I want to achieve. The problem is I'm not entirely sure how to do that.
    I have a few png images, I converted them to .plist and I don't know how to apply them to my CCNode. Every code I've seen includes RunAction and this for some reason doesn't work for me, because the game crashes with exception.
    I just need the simplest version of sprite animation.

    I'll look into examples you provided.

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai

    How about sending us the solution you have with assets and let's see what we can do to help you out.

  • FilipCinikFilipCinik PLMember

    Ok, I'm a little confused. I wanted to make a little sample, because I can't send full solution and... the code from https://forums.xamarin.com/discussion/comment/131552#Comment_131552 worked. I guess the problem is somewhere in my solution so I have to take a closer look into my project. Maybe interference with box2d or bad implementation.
    I'll post answer in few hours.
    Thank you for your help :)

  • FilipCinikFilipCinik PLMember

    Actually I didn't even need hours to solve this... The whole time I worked on bad class. I have class which keeps together sprite and body(box2d) and yesterday I tried to implement animation on that class which is wrong. I had to implement it to the sprite itself.
    Yesterday was just a bad, stressful day. Anyway thank you for your help.

Sign In or Register to comment.