/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.Sprite; /** * * @author Сергей */ public class Ball extends Sprite { private int speedX,speedY; //скорость по осям координат public Ball(Image image) { super(image); setPosition(0,0); speedX=2; speedY=2; } public void update(Sprite labyrint) { move(speedX,speedY); if (collidesWith(labyrint,true)) { if (speedX<0) { speedX=-speedX; move(speedX,0); } if (speedX>0) { speedX=-speedX; move(speedX,0); } if (speedY<0) { speedY=-speedY; move(0,speedY); } if (speedY>0) { speedY=-speedY; move(0,speedY); } } //обработка столкновений с границами экрана if (getX()>=MyCanvas.link.getWidth()-getWidth() && speedX>0) speedX=-speedX; else if (getX()<=0 && speedX<0) speedX=-speedX; if (getY()>=MyCanvas.link.getHeight()-getHeight() && speedY>0) speedY=-speedY; else if (getY()<=0 && speedY<0) speedY=-speedY; } public void incrementSpeeds(int _speedX,int _speedY) { speedX+=_speedX; speedY+=_speedY; } }