📄 movingsprite.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;public class MovingSprite extends Sprite { private int xSpeed, ySpeed; private int action; private Canvas canvas; public static final int BA_HIDE = 1; public static final int BA_WRAP = 2; public static final int BA_BOUNCE = 3; public static final int BA_STOP = 4; public MovingSprite(Image image, int xMoveSpeed, int yMoveSpeed, int boundsAction, Canvas parentCanvas) { super(image); // Set the XY speed xSpeed = xMoveSpeed; ySpeed = yMoveSpeed; // Set the bounds action action = boundsAction; // Set the parent canvas canvas = parentCanvas; } public MovingSprite(Image image, int frameWidth, int frameHeight, int xMoveSpeed, int yMoveSpeed, int boundsAction, Canvas parentCanvas) { super(image, frameWidth, frameHeight); // Set the XY speed xSpeed = xMoveSpeed; ySpeed = yMoveSpeed; // Set the bounds action action = boundsAction; // Set the parent canvas canvas = parentCanvas; } public void update() { // Move the sprite based on its speed move(xSpeed, ySpeed); // Check for a collision with the screen boundary checkBounds(); } public int getXSpeed() { return xSpeed; } public int getYSpeed() { return ySpeed; } public void setXSpeed(int xMoveSpeed) { xSpeed = xMoveSpeed; } public void setYSpeed(int yMoveSpeed) { ySpeed = yMoveSpeed; } private void checkBounds() { // Hide the sprite if necessary if (action == BA_HIDE) { if (getX() < 0 || getX() > (canvas.getWidth() - getWidth()) || getY() < 0 || getY() > (canvas.getHeight() - getHeight())) setVisible(false); } // Wrap the sprite if necessary else if (action == BA_WRAP) { // Wrap the sprite around the edges of the screen if (getX() < -getWidth()) setPosition(canvas.getWidth(), getY()); else if (getX() > canvas.getWidth()) setPosition(-getWidth(), getY()); if (getY() < -getHeight()) setPosition(getX(), canvas.getHeight()); else if (getY() > canvas.getHeight()) setPosition(getX(), -getHeight()); } // Bounce the sprite if necessary else if (action == BA_BOUNCE) { // Bounce the sprite at the edges of the screen if (getX() < 0 || getX() > (canvas.getWidth() - getWidth())) xSpeed = -xSpeed; if (getY() < 0 || getY() > (canvas.getHeight() - getHeight())) ySpeed = -ySpeed; } // Stop the sprite if necessary else { if (getX() < 0) setPosition(0, getY()); else if (getX() > (canvas.getWidth() - getWidth())) setPosition(canvas.getWidth() - getWidth(), getY()); if (getY() < 0) setPosition(getX(), 0); else if (getY() > (canvas.getHeight() - getHeight())) setPosition(getX(), canvas.getHeight() - getHeight()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -