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

2015/5/28

Need too call the act method only every couple of frames

someone1 someone1

2015/5/28

#
I have be stuck on this for a while, i want to use the command animate(which i created) to make my character animate but with the act method my character just keeps cycling through the animations so i just want to call the act method every 6 or 7 frames or so please help. Sorry im pretty newbie at programming
MeinKraftBubba MeinKraftBubba

2015/5/28

#
Use a few variables like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int frameDelay = 10;
int frame = 1;
int imageCounter = 0;
 
public void act()
{
     imageCounter++;
     animate();
}
 
public void animate()
{
     if(imageCounter % frameDelay = 0)
     {
           if(frame == 1)
           {
                 setImage(image2);
                 frame = 2;
           }
           else if(frame == 2)
           {
                setImage(image1);
                frame = 1;
           }
           imageCounter = 0;
     }
}
This code is telling your actor to run an animation method every act method, and to increase your variable imageCounter. Whenever imageCounter is divisible by the value of variable frameDelay, which is 10 in this example, it runs the animation code, which checks which image it's currently using and changes it to the next in sequence. Then, it resets the imageCounter to 0 to repeat that loop of code. After that, you only need to add as many images and frames as you need to complete the animation, and change the frameDelay value to adjust the speed.
someone1 someone1

2015/5/28

#
I dont need to update the image, the command already does that
someone1 someone1

2015/5/28

#
The command animate already advances the character onto the next frame and animates the character so i dont need too worry about the image, i just want to make sure that WHEN my character animates i dont want the code to repeat for another couple of frames plus i tried your code out and it said on line 13 unexpected value, required variable found value
MeinKraftBubba MeinKraftBubba

2015/5/28

#
Can you post your code? The way that I just put up is directly off of my programming teacher's website, so there's probably a conflict between what you've already coded and the additions I suggested.
someone1 someone1

2015/5/28

#
The command animate already advances the the frame and animates
someone1 someone1

2015/5/28

#
1
2
3
4
5
6
7
8
if(frames1 / frameSpace == 0) {
            if(frames == 1) {
                frames = 2;
                animate
            }
            else {[code]int frameSpace = 10;
    int frames = 1;
    int frames1 = 0;
if(frames == 2) { frames = 1; } } }
someone1 someone1

2015/5/28

#
oops wait let me do that again
someone1 someone1

2015/5/28

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int frameSpace = 10;
int frames = 1;
int frames1 = 0;
 
public void act ()
{
frames++;
animateCharacter
 
}
public void animateCharacter {
if(frames1 / frameSpace == 0) {
  if(frames == 1) {
     frames = 2;
     animate = 2;
}
            else {
                if(frames == 2) {
                    frames = 1;
                }
            }
        }
 
 
 
}
someone1 someone1

2015/5/28

#
I didnt use the command animate yet
someone1 someone1

2015/5/28

#
oops another mistake line 15 should not be there
danpost danpost

2015/5/28

#
This should be all you need:
1
2
3
4
5
public void act()
{
    frames = (frames+1)%12;
    if (frames%6 == 0) animate();
}
In the 'animate' method, use 'frames/6' for which image to set. It value will be either zero or one.
someone1 someone1

2015/5/28

#
OMG thank you soo much
You need to login to post a reply.