?? moveablesprite.java
字號:
/*
* Author: Huang ye(www.hyweb.net)
* 代碼開源, 引用請注明出處
*
* 創建日期 2005-2-26
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
package net.hyweb;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
* @author user
*
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
public class MoveableSprite extends Sprite implements SubObject {
private final int frameChangeTick; //可以做幀畫面變化的次數
//每個幀畫面在一下區域變化
private int xBound;
private int yBound;
private int wBound;
private int hBound;
private int vx;
private int vy;
private int ticksSinceLastChangeX = 0;
private int ticksSinceLastChangeY = 0;
private LayerManager layerManager;
/** 可移動動畫精靈(Sprite)
* @param image 傳入圖像
* @param frameWidth 目標幀單元寬度
* @param frameHeight 目標幀單元高度
* @param frameChangeTick 幀圖片數量
*
* @param x 圖層橫坐標 初始化類圖層橫坐標加上橫向位移
* @param y 圖層縱坐標 初始化類圖層縱坐標加上縱向位移
* @param vx 圖層移動x軸速度
* @param vy 圖層移動y軸速度
* @param xBound 圖層運動區域 -- 起點x坐標
* @param yBound 圖層運動區域 -- 起點y坐標
* @param wBound 圖層運動區域 -- 終點x坐標
* @param hBound 圖層運動區域 -- 終點y坐標
* @param layerManager 圖層管理調用器
*/
public MoveableSprite(Image image, int frameWidth, int frameHeight, int frameChangeTick, int x, int y,
int vx, int vy, int xBound, int yBound, int wBound, int hBound, LayerManager layerManager){
super(image, frameWidth, frameHeight);
this.frameChangeTick = frameChangeTick;
//圖層定位及定向
this.setPosition(x, y);
if(vx < 0){
this.setTransform(Sprite.TRANS_MIRROR);
}else{
this.setTransform(Sprite.TRANS_NONE);
}
//設置速度和運動邊界
setSpeed(vx, vy);
setBound(xBound, yBound, wBound, hBound);
this.layerManager = layerManager;
}
/** 設置速度
* @param vx
* @param vy
*/
public void setSpeed(int vx, int vy){
this.vx = vx;
this.vy = vy;
}
/** 設置幀圖層運動區域
* @param xBound
* @param yBound
* @param wBound
* @param hBound
*/
public void setBound(int xBound, int yBound, int wBound, int hBound){
this.xBound = xBound;
this.yBound = yBound;
this.wBound = wBound;
this.hBound = hBound;
}
/** 根據傳入的參數設定時間, 觸發Sprite的內部幀轉換(動畫效果)
* 本方法每運行一次, 畫面幀改變一次, 同時位置也改變一次
* @param tickCount
*/
public void tick(int tickCount){
if((tickCount % this.frameChangeTick) == 0){
this.nextFrame();
}
//畫面移動
this.movePosition();
}
/**
* 沒有傳入參數的movePosition, 表明沒有從外界接受移動指令
* 而是自身自動移動
*/
public void movePosition(){
this.ticksSinceLastChangeX++;
this.ticksSinceLastChangeY++;
//獲取當前位置
int x = getX();
int y = getY();
//有的時候改變一下速度vx 及 vy
if((this.ticksSinceLastChangeX > 20) && SubMIDlet.createRandom(20) == 0){
//vx置為-1 或者 1
this.vx = SubMIDlet.createRandom(2) * 2 - 1;
this.ticksSinceLastChangeX = 0;
}
if((this.ticksSinceLastChangeY > 20) && SubMIDlet.createRandom(20) == 0){
//vx置為-1, 0 或者 1
this.vy = SubMIDlet.createRandom(3) - 1;
this.ticksSinceLastChangeY = 0;
}
//當觸壁時, 速度反向, 速度變化累加器置為0
if(((vx < 0) && (x + vx < xBound)) || (vx > 0) && (x + getWidth() > (xBound + wBound))){
vx = -vx;
this.ticksSinceLastChangeX = 0;
}
if(((vy < 0) && (y + vy < yBound)) || (vy > 0) && (y + getHeight() > (yBound + hBound))){
//此時y軸速度既可能反向, 也可能停止
if(SubMIDlet.createRandom(2) == 0){
vy = -vy;
}else{
vy = 0;
}
this.ticksSinceLastChangeY = 0;
}
//當x軸速度反向時, 圖形反向
if(vx < 0){
this.setTransform(Sprite.TRANS_MIRROR);
}else{
this.setTransform(Sprite.TRANS_NONE);
}
//重置圖形位置
x = x + vx;
y = y + vy;
//由于是類圖層運動先重新定位, 有可能造成在類圖層定位后
//某個Sprite的坐標被拋出了setBound()重定義后的, 自身運動區域,就需要做輕微調整
if(x < xBound){
x = xBound;
}
if(x > xBound + wBound){
x = xBound + wBound - 1;
}
if(y < yBound){
y = yBound;
}
if(y > yBound + hBound){
y = yBound + hBound - 1;
}
this.setPosition(x, y);
}
/**
* 接口方法
*/
public void collideStuff() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -