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

2020/6/5

how do i limit my scenario so my object cant pass it?

lilshopa lilshopa

2020/6/5

#
so my character can move up and down but i want to limit its going up so it dont reach the "sky" of the scenario
Kostya20052011 Kostya20052011

2020/6/5

#
Add another condition, for example: if(Greenfoot.isKeyDown ("W") && y>getWorld().getHeight()/2). Instead of getWorld().getHeight()/2 you can substitute any value.
lilshopa lilshopa

2020/6/5

#
i have to create a variable with the y? cause its not recognizing the "y" alone
danpost danpost

2020/6/5

#
lilshopa wrote...
i have to create a variable with the y? cause its not recognizing the "y" alone
y is a hidden variable provided by the Actor class which keeps track of where the actor is in the world along the vertical (its y-coordinate). You can just use getY() to get its value.
Kostya20052011 Kostya20052011

2020/6/5

#
I usually enter the variable y and write x=getX(); as the first line in the object code(x I also create) y=getY(); and then I already use variables, because it is more convenient for me.
lilshopa lilshopa

2020/6/5

#
i'm a little lost right now my code look like this im not very good at progamming im sorry how do i set the height of the world? public class buzz extends Actor { private int dir = 3; public void act() { if(Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+dir); } if(Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-dir); if (getY()<getWorld().getHeight()/2); { move(0); } } } }
RcCookie RcCookie

2020/6/5

#
Try this:
//At the class beginning
private final int maxHeight = 0;
//In your act method
if(Greenfoot.isKeyDown(„up“)&&getY()<maxHeight){
    serLocation(getX(), getY - dir);
}
danpost danpost

2020/6/5

#
RcCookie wrote...
Try this: << Code Omitted >>
Line 4 should be more like:
if (Greenfoot.isKeyDown("up") && getY() > maxHeight)
and maxHeight should probably be set to something between 50 and 200 on line 2.
RcCookie RcCookie

2020/6/6

#
danpost wrote...
RcCookie wrote...
Try this: << Code Omitted >>
Line 4 should be more like:
if (Greenfoot.isKeyDown("up") && getY() > maxHeight)
and maxHeight should probably be set to something between 50 and 200 on line 2.
Depends on how far up the object should be allowed to move
danpost danpost

2020/6/6

#
RcCookie wrote...
Depends on how far up the object should be allowed to move
lilshopa wrote...
i want to limit its going up so it dont reach the "sky" of the scenario
A major correction was the inequality sign, however.
Kostya20052011 Kostya20052011

2020/6/6

#
Change your code this way: public class buzz extends Actor { private int dir = 3; public void act() { if(Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+dir); } if(Greenfoot.isKeyDown("up") && getY()>getWorld().getHeight()/2) { setLocation(getX(), getY()-dir); } } }
lilshopa lilshopa

2020/6/6

#
thank you guys! i got it
You need to login to post a reply.