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

2013/6/3

Help with movement

programmer274 programmer274

2013/6/3

#
I need help with getting an object to move up and down in my world and once it hits the top edge turns 180 and starts to move down and once it hits the bottom edge of the world turn 180 again and move up. I have tried many different things to solve this problem but I haven't been successful. Does anyone have any idea how to code this?
programmer274 programmer274

2013/6/3

#
Here is my code I am just trying to figure out how to get the object to start moving up in the first place and have the if statements still workimport greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Zone here. * * @author (your name) * @version (a version number or a date) */ public class Zone extends Actor { private int uSpeed = -8; private int dSpeed = 8; private boolean atEdge; /** * Act - do whatever the Zone wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); atWorldEdge(); } private void move() { if(getY() <= 300 && atWorldEdge()) { turn(180); setLocation ( getX(), getY() + dSpeed); } if(atWorldEdge() && getY() > 300) { turn(180); setLocation ( getX(), getY() + uSpeed); } } public boolean atWorldEdge() { if(getX() < 1 || getX() > getWorld().getWidth() - 1) return true; if(getY() < 1 || getY() > getWorld().getHeight() - 1) return true; else return false; } }
You need to login to post a reply.