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

2013/3/15

New Game

1
2
geekykid2013 geekykid2013

2013/3/16

#
is their a way to move my object left or right in the world and stop before it actually hits the edge of the world?
geekykid2013 geekykid2013

2013/3/16

#
for example the object stops 10 pixels before it reaches the edge
danpost danpost

2013/3/16

#
Your current 'atWorldEdge' method has the checks that you need, but is using 20 pixels as the buffer. Remove that method and put the appropriate check with each key detected movement using 10 instead of 20. The left direction check/movement code would be:
if (Greenfoot.isKeyDown("left") && getX()>10){
    setLocation(getX()-1, getY());
}
Add the appropriate check similarly for the right direction.
geekykid2013 geekykid2013

2013/3/16

#
i have implemented the above code into greenfoot but my object is still touching the edge of the world but is not stopping 10 px before. here is my code: public void move() { //System.out.println("move funtion called"); /*if (Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-1); } if (Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+1); }*/ if (Greenfoot.isKeyDown("left")&& getX()>10){ //System.out.println("left key pressed"); setLocation(getX()-1, getY()); } if (Greenfoot.isKeyDown("right")){ setLocation(getX()+1, getY()); //System.out.println( "right key pressed"); } } public void act() { move(); } // public boolean atWorldEdge() // How to make the new object appear automatically onto the stage // if (getX() < 10 || getX() > getWorld().getWidth() - 10) { // return true; //} //if (getY() < 10 || getY() > getWorld().getHeight() - 10) { // return true; //} //return false; //} }
danpost danpost

2013/3/16

#
It should be stopping 10 pixels before the left edge of the world. It may not appear that way because it is the center of the image that will stop at that point. You still have not added the extra check for going right and the right side of the world, so the image should go half out of the world on the right still.
danpost danpost

2013/3/16

#
You could test it by using the following for the left direction check/movement, which will keep the whole image off the left side of the world by 10 pixels:
if (Greenfoot.isKeyDown("left") && getX()>10+getImage().getWidth()/2){
    setLocation(getX()-1, getY());
}
geekykid2013 geekykid2013

2013/3/16

#
I have added the code to the right key movement, but when i try to move right from the edge of the world the object stops moving? here is my code public void move() { //System.out.println("move funtion called"); /*if (Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-1); } if (Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+1); }*/ if (Greenfoot.isKeyDown("left")&& getX()>10){ //System.out.println("left key pressed"); setLocation(getX()-1, getY()); } if (Greenfoot.isKeyDown("right")&& getX()>10){ setLocation(getX()+1, getY()); //System.out.println( "right key pressed"); } } public void act() { move(); } // public boolean atWorldEdge() // How to make the new object appear automatically onto the stage // if (getX() < 10 || getX() > getWorld().getWidth() - 10) { // return true; //} //if (getY() < 10 || getY() > getWorld().getHeight() - 10) { // return true; //} //return false; //} }
danpost danpost

2013/3/16

#
You did not add the appropriate check for the right direction. Look at the 'atWorldEdge' method code and determine the correct check to use. Once you have the right one in place, remove the whole 'atWorldEdge' method from the class code (you are not calling it from anywhere, anyway). EDIT: Oh, I see you at least commented it out.
geekykid2013 geekykid2013

2013/3/16

#
i think i have now used the right check for the right movement of my object. but now the object gets stuck on the right edge of world when i try to move left what am i doing wrong? here is my code public void move() { //System.out.println("move funtion called"); /*if (Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-1); } if (Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+1); }*/ if (Greenfoot.isKeyDown("left")&& getX()>10+getImage().getWidth()/2){ //System.out.println("left key pressed"); setLocation(getX()-1, getY()); } if (Greenfoot.isKeyDown("right")&& getX()>10-getImage().getWidth()/2){ setLocation(getX()+1, getY()); //System.out.println( "right key pressed"); } } public void act() { move(); } // public boolean atWorldEdge() // How to make the new object appear automatically onto the stage // if (getX() < 10 || getX() > getWorld().getWidth() - 10) { // return true; //} //if (getY() < 10 || getY() > getWorld().getHeight() - 10) { // return true; //} //return false; //} }
danpost danpost

2013/3/16

#
The check does not compare the objects location (returned from 'getX()') to any location, or x value, near the right side of the world. The right-most location in the world is at 'getWorld().getWidth()-1'. Your check should be:
if (Greenfoot.isKeyDown("right") && getX()<getWorld().getWidth()-getImage().getWidth()/2-10)
geekykid2013 geekykid2013

2013/3/16

#
also the object does not buffer 10 px from the edge of the world on the right side when with if(Greenfoot.isKeyDown("right") && getX()>10-getImage().getWidth()/2)
geekykid2013 geekykid2013

2013/3/16

#
thanks for your help
geekykid2013 geekykid2013

2013/3/16

#
how do i make my object automatically appear on the stage without have drag and drop?
geekykid2013 geekykid2013

2013/3/16

#
its okay i've found the solution
You need to login to post a reply.
1
2