This site requires JavaScript, please enable it in your browser!
Greenfoot back
diy_surgery
diy_surgery wrote ...

2011/12/22

AnimatedActor - help needed

diy_surgery diy_surgery

2011/12/22

#
Hi, I am making a plat-former where the player can attack enemies, i have been using the AnimatedActor class that i found here in the greenfoot website. My problem is that i have a character that has a animation for just standing(idle) around, but if i press the attack button the character just plays 1 attack frame and then plays the idle animation. Is there a way for me to force the full animation when ever i press the attack button?
darkmist255 darkmist255

2011/12/25

#
I'd like to know this too, I've tried something like this before. I have some ideas of how it could be done but I'm sure that mine are probably over-complicated.
DonaldDuck DonaldDuck

2011/12/29

#
I don't know anything about the animatedActor class but a simple way to have an animation is to have an image array and set the image to the corresponding frame. Here's some code.
float frame = 0; //current frame of animation
GreenfootImage[] img = new GreenfootImage[]{new GreenfootImage("image1.png"),new GreenfootImage("image2.png")};
Obviously add your own images in that format.
public void animate(int sequence)
{
    switch(sequence) // run a different sequence depending on which integer is given.
    {
        case 1: if(frame<10) { frame += ANIMSPEEDf; } else { frame = 0; } break; // animate frames 0-10 at a speed of ANIMSPEED
        case 2: if(frame<20) { frame += ANIMSPEEDf; } else { frame = 11; } break; //animate frames 11-20 at a speed of ANIMSPEED
    }
    setImage(img[(int)frame]); // set the image of this character to the image for this frame of the animation.
}
Where ANIMSPEED is the speed you want to play your animation at (example 0.3) and remember to have the "f" following it to declare it as a float. Change the values in the if statements and else statements depending on the length of each sequence. In this way, you can tell which sequence to animate. If you want to animate "idle", call animate(1) in your act method, etc. for each sequence.
public void act()
{
    if(idle == true && attacking == false)
    {
        animate(1);
    }
    if(attacking == true)
    {
        animate(2);
    }
}
Sorry that this doesn't mesh with the AnimatedActor but this code is pretty simple and should do the trick for you.
darkmist255 darkmist255

2011/12/29

#
This part is confusing me a little:
    public void animate(int sequence)  
    {  
        switch(sequence) // run a different sequence depending on which integer is given.  
        {  
            case 1: if(frame<10) { frame += ANIMSPEEDf; } else { frame = 0; } break; // animate frames 0-10 at a speed of ANIMSPEED  
            case 2: if(frame<20) { frame += ANIMSPEEDf; } else { frame = 11; } break; //animate frames 11-20 at a speed of ANIMSPEED  
        }  
        setImage(img[(int)frame]); // set the image of this character to the image for this frame of the animation.  
    }  
I'm looking at it, but don't understand what "switch(sequence)" is, or what "case 1:, case 2:, break" is. Any clearing up you can give would be greatly appreciated :D!
DonaldDuck DonaldDuck

2011/12/29

#
The switch is simple really. 1) The integer "sequence" is passed to the void when you call the method animate(int sequence) Side note) Switch is basically shorthand for a bunch of if statements. If you don't write switch(int) then you would have to say if(sequence == 1) {do this} if(switch == 2) {do this} etc. 2) case 1: (meaning sequence is equal to 1) animate the first sequence. The break; prevents it from reading on into case 2. Failure to use break; will make the code keep running until it reaches the end of the switch Hopefully this makes sense :)
DonaldDuck DonaldDuck

2011/12/29

#
This website may help clear it up better.
darkmist255 darkmist255

2011/12/29

#
I think it does, I'll have to try it out sometime. So to verify: "switch(int)" can be used instead of "if(int == 1, 2, 3, 4, 5, etc) { }". Then, "break" makes it so that you won't end up executing all of the "if statements", you will only use the current one (sort of like "else if"). Thanks :D!
DonaldDuck DonaldDuck

2011/12/29

#
Correct :) Check the link above if you haven't already as the explanation there is pretty clear.
darkmist255 darkmist255

2011/12/29

#
I just checked it out, very straightforward. Surprisingly more straightforward explanation than I would expect from a plain HTML page (those plain pages are usually fairly technical) :D.
DonaldDuck DonaldDuck

2011/12/29

#
It's no Wikipedia, but it's a very good source for learning things in java. Glad I could help
You need to login to post a reply.