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

2011/7/12

Making a variable raise at a slower rate

w1llis w1llis

2011/7/12

#
So I was wondering if there was a way to make a variable raise at lets say + 1 ever .5 seconds... I'm still a slight beginner, and never got into the very specialty things when i was learning plain Java... Thanks in advnace.
mjrb4 mjrb4

2011/7/12

#
You could use seconds, but it's easier just to use act cycles instead which is what you want most of the time. The usual approach is to increment a counter on every iteration of act - you could do this, and when the counter say reached 20 (fiddle with this number to get the effect you want) set the counter back to 0 and increment the variable you want to increment every few seconds.
w1llis w1llis

2011/7/12

#
Thanks... I was puzzled at this one for a looong time...
faizakamal faizakamal

2011/10/3

#
how do you increment a counter? my problem is that I have one image, and I want to change that to another image every 3 seconds, then change it back to the first image and carry on doing that. how do I do that?
Duta Duta

2011/10/4

#
I'd explain now but I've got something to do (I'll try to remember to explain later). Also how long do you want the second image to be up for?
faizakamal faizakamal

2011/10/4

#
each image should be up for like 2 seconds then change so my image doesnt look like it has a hyperactive disorder :D
Morran Morran

2011/10/12

#
@faizakamal You want an Actor to change its image every couple of seconds? Here's a suggestion:
public class Actor {

    //...(your normal instance variables)

    //add this after your normal variables.
    private int frameNum;

    //...(methods)

    public void act()
    {
          //...do whatever you want it to do....

          //...but make sure to add animate()
          animate();
    }

    //This is where you will animate the Actor.
    private void animate()
    {
          if(frameNum == 0) {setImage(whateverImageYouUse);}

          else if(frameNum == 20) {setImage(whateverImageYouUse2);}

          //...and so on, until you get to the last image:
          else if(frameNum == 40) {
                 setImage(lastImageYouUse);
                 frameNum = 0;
          }

          //this is where you update the frameNum
          frame++;
          }
}
Change what frameNum has to equal to adjust the time between image changes. You can add as many frames to the animation a you want.
Duta Duta

2011/10/13

#
@Morran, that was a really good reply, seeing as it was very clear :) Its good that you've joined the community
BCC BCC

2011/10/30

#
    private void animate()
    {
            if(frameNum == 0)
        {
            setImage(image1);
        }
        else if(frameNum == 20)
        {
            setImage(image2);
        }
        else if(frameNum == 39)
        {
            setImage(image1);
            frameNum = 0;
        }
        frame ++;
    }
}
I also had the same problem as w1llis and tried out Morran's code, unfortunately it does not seem to work, because when I finally typed in "frame++;" it did not find the symbol. Why doesn't it work? I am almost sure that I haven't typed in anything wrong but why doesn't it work ?
danpost danpost

2011/10/30

#
I have a feeling 'frame++;' should be 'frameNum++;'.
Upupzealot Upupzealot

2011/10/31

#
Date dt= new Date(); Long time= dt.getTime();//this is the ms(1000ms = 1s) number beginfrom 1970_1_1_00:00:00_0ms I didn't try it, but if ti's OK.I think it would help.
You need to login to post a reply.