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

2013/6/4

Help with bouncing from top to bottom

programmer274 programmer274

2013/6/4

#
I am trying to get an instance of my wall class to move from the top to the bottom of the screen and repeat. I know this is easy but I am struggling with how to program it and would like some help. Here is my code: import 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 cats; /** * 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()) { cats = false; } if(atWorldEdge() || getY() > 300) { cats = true; } if(cats) { setLocation ( getX(), getY() + dSpeed); } if(!cats) { 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; } } Currently it moves to the top of the screen and gets stuck I am trying to figure out how i can get it to keep changing a value so i can move it from the top to the bottom and back. Thanks in advance.
programmer274 programmer274

2013/6/5

#
If anyone could reply that would be great.
You need to login to post a reply.