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

2014/2/20

Need delay without delaying game?

Marky3100 Marky3100

2014/2/20

#
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
28
29
30
31
32
33
34
35
public class Ring1 extends Notmoving
{
 
    private String imgName="ring";
 
    /**
     * Act - do whatever the Ring1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        turnRing();
    }
 
    public void turnRing(){
        int imgNumber = 1;
         
        while (imgNumber < 7)
        {
             
            imgNumber++;
            setSecondImage(imgNumber);
 
        }
        
 
    }
 
    public void setSecondImage(int imgNumber)
    {
        //Greenfoot.delay();
        setImage(imgName + imgNumber + ".png");
 
    }
}
As you can see there are 7 images of Ring.png named ring1.png till ring7.png. They need to alternate in order from 1 to 7 but there has to be a delay for 1 second between each image. I used Greenfoot.delay() but this delays the whole game anyone who got a solution for me?
danpost danpost

2014/2/20

#
What you need is a timer field. Usually 50 to 60 act cycles equates to about a second; so, you can use a range of 350 (50*7) or 420 (60*7) for the timer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private int timer;
 
public void act()
{
    updateImage();
    runTimer();
}
 
private void updateImage()
{
    if (timer%50 == 0) setImage("ring"+(timer/50+1)+".png");
}
 
private void runTimer()
{
    timer = (timer+1)%350;
}
Marky3100 Marky3100

2014/2/20

#
Thanks! Works fine. Another question witch i hope someone could help me. I got this code for getting to the next level.
1
2
3
4
5
6
7
public void nextLevel2()
    {
        if(canSee (Keyhole.class))
        {
            Greenfoot.setWorld(new Mollenworld2());
        }
    }
But I want need to check if all the Diamonds are taken. So if Diamond.class in world is 0 AND key canSee keyhole.class then set new world. Any ideas on how to do this?
danpost danpost

2014/2/20

#
if (getWorld().getObjects(Diamond.class).isEmpty() && canSee(Keyhole.class))
You need to login to post a reply.