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

2017/1/31

How to go up without causing a delay?

LakeOtis LakeOtis

2017/1/31

#
Here's my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class gem here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class gem extends Actor
{
    /**
     * Act - do whatever the gem wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int variable = 20;
        if(Greenfoot.isKeyDown("space"))
        {
            while(!isTouching(border.class))
            {
                while(variable < 40)
                {
                    setLocation(getX(), getY()-6);
                    Greenfoot.delay(1);
                    variable += 1;
                }
            }
        }
    }    
}
I think the 'Greenfoot.delay(1);' is causing the delay, but if I do anything else then it goes too fast and you can't even see it. How to make it cause a delay for only one actor?
Super_Hippo Super_Hippo

2017/1/31

#
Remove the Greenfoot.delay and try to use 'if' instead of 'while'.
LakeOtis LakeOtis

2017/2/1

#
Nothing happened.
Super_Hippo Super_Hippo

2017/2/1

#
With "nothing happened", do you mean that pressing the space bar doesn't have any effect anymore or that changing both 'while' to 'if' didn't make any difference?
LakeOtis LakeOtis

2017/2/3

#
No difference
Super_Hippo Super_Hippo

2017/2/3

#
Try to change the 6 in line 24 to a 1.
LakeOtis LakeOtis

2017/2/6

#
ok Edit: Finally had time to try it... didn't work
You need to login to post a reply.