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

2014/5/4

help with animating clara (pacman game)

dangwang dangwang

2014/5/4

#
correct me if i'm wrong, public void act() runs every frame. so if i were to just call the animate() method only, clara's animation would be very quick. what would be the best way to call the animate() method every so times. say every second time act() is run?
danpost danpost

2014/5/4

#
You could use an instance boolean field and switch its value every act; then, when it is switches to true, call the animate method. However, a boolean is limited in what it can do. Using an instance int field would give you the flexibility to alter the speed. Increment the value every act, and when it reaches a specific value, set it back to zero and call the animate method.
dangwang dangwang

2014/5/4

#
is there an example that i could follow? i'm quite new to programming and have very little understanding as to how to use booleans
danpost danpost

2014/5/4

#
A boolean is just anything that evaluates to a 'true' or 'false' value. All conditional operators (AND '&&', OR '||', IS EQUAL TO '==', IS GREATER THAN '>', etc.) create a boolean value. It can be negated by the NOT '!' operator. So to toggle a boolean named 'executeMethod' you would use:
1
isTimeToExecuteMethod = !isTimeToExecuteMethod;
Then you can use the value for a condition in an 'if' statement, as follows:
1
if (isTimeToExecuteMethod == true) animate();
which could also be written like this:
1
if (isTimeToExecuteMethod) animate();
since the field itself is a boolean and it does not need to made a boolean by the use of '=='.
dangwang dangwang

2014/5/4

#
the boolean must first evaluate to false first right? so before the public void act() i would put boolean isTimeToExecuteMethod = false; in order for this isTimeToExecuteMethod = !isTimeToExecuteMethod; to be true the animation is still too fast to my liking. lol
danpost danpost

2014/5/4

#
dangwang wrote...
the animation is still too fast to my liking. lol
That is why I mentioned the fact that an instance int field might be better.
dangwang dangwang

2014/5/4

#
i'll give that a try. thanks danpost, your help is greatly appreciated
dangwang dangwang

2014/5/4

#
int animationSpeed = 0; boolean a = false; /** * Act method * * Runs of every frame */ public void act() { a = !a; if (a == true) animationSpeed = 1; else if (a == false) animationSpeed = 0; } void claraAnimation() { if ( animationSpeed > 0) { animate(); } } lmao i don't even know what i'm doing. please shed some insight danpost
danpost danpost

2014/5/4

#
You needed to remove the boolean first. Then follow my instructions in my earlier post about using the int.
danpost danpost

2014/5/4

#
'animationSpeed' is not a very good name for what it will do. The higher you let it go, the slower your actor will animate. The following should suffice:
1
2
3
4
5
6
7
8
9
10
11
int animationTimer = 0;
 
public void act()
{
    animationTimer++;
    if (animationTimer == 4)
    {
        animate();
        animationTimer = 0;
    }
}
The '4' can be raised to slow the animation down more (or lowered to speed it up).
dangwang dangwang

2014/5/4

#
You are a legend. Thanks so much!!!!
You need to login to post a reply.