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

2015/2/3

Controls

60sChallange 60sChallange

2015/2/3

#
setLocation(getX()+ dy,getY()); if (Greenfoot.isKeyDown("space")== true){ dy= 0; } if(Greenfoot.isKeyDown("left") == true){ dy= -10; } if (Greenfoot.isKeyDown("right") == true){ dy= 10; } How can l code.. that l can press "left" or "right" just once after l press "space" and then again and again) so it goes like this at the begging of game l can just press "space" when l press "space" l can just press "left" or "right" just once and when l press "left" or "right" l can just press space and then again and again
davmac davmac

2015/2/3

#
Please:
danpost danpost

2015/2/3

#
You might want to ask yourself 'What is different when waiting on the "space" key and when waiting on either the "left" or "right" key?'. The answer is: the current value of 'dy'. Use this fact to create additional conditions to limit when each action is taken.
60sChallange 60sChallange

2015/2/3

#
Does greenfoot have class for blockinf commands . L can't find it. Sorry l am new at programing and l an learning on net
danpost danpost

2015/2/3

#
Please elaborate. 'blockinf' is new to me. What do 'blockinf' commands do?
60sChallange 60sChallange

2015/2/3

#
Sorry l meant to wrote blocking... I meant if (dy=0){ "block "space"command}
danpost danpost

2015/2/3

#
The 'if' statement is, in essence, a blocking statement. If the condition is 'false', the code block will not execute. You would say 'if some set of conditions are true, execute the following code'. I would suggest using 'else' in this matter ( that is: 'if (dy == 0) {} else { run space command }' ), and, in fact, that is exactly what you could do. The code within the empty curly brackets ( '{}' ) could be 'run left/right command':
if (dy == 0)
{
    if (Greenfoot.isKeyDown("left")) {
        dy -= 10;
    }
    if (Greenfoot.isKeyDown("right")) {
        dy += 10;
    }
}
else // dy is not zero
{
    if (Greenfoot.isKeyDown("space")) {
        dy = 0;
    }
}
You need to login to post a reply.