import com.genuts.gameui.*; /** * SpriteWrapper with the ability to move a sprite with a constant * speed. */ public class MovingWrapper extends MovingSpriteWrapper { /** * X speed */ private int xspeed; /** * Y speed */ private int yspeed; /** * Initializes the moving sprite with an action sprite * and a given speed * @param sprite Sprite for visualisation * @param vx Speed for X coordinates * @param vy Speed for Y coordinates */ public MovingWrapper(Sprite sprite, int vx, int vy) { super(sprite); xspeed = vx; yspeed = vy; setBackgroundSprite(true); } /** * Moves the sprite with the current speed * @param ticks Number of ticks from the playfield */ public void move(int ticks) { getFinalWrapper().setPosition(getX()+xspeed, getY()+yspeed); } /** * Receives the event that this sprite is collision * with another sprite * @param s Sprite with which the collision is */ public void collisionWith(Sprite s) { if (s == null) { if ((getX() < 0) || ((getX() + getWidth()) > getParent().getWidth())) { xspeed *= -1; getFinalWrapper().setPosition(getX() + xspeed, getY()); } if ((getY() < 0) || ((getY() + getHeight()) > getParent().getHeight())) { yspeed *= -1; getFinalWrapper().setPosition(getX(), getY() + yspeed); } } super.collisionWith(s); } }