?? spritedemo01.java
字號:
package TestDemo04;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
public class SpriteDemo01 extends GameCanvas implements Runnable{
private Graphics g;
private Image image;
private int width,height;
private Sprite s1;
private Sprite s2;
private int x1=0;
private int y1=0;
protected SpriteDemo01() {
super(true);
try{
image=Image.createImage("/tu03.PNG");
}
catch(Exception e){
e.printStackTrace();
}
g=this.getGraphics();
g.setColor(0,0,0);
width=this.getWidth();
height=this.getHeight();
s1=new Sprite(image,147,112);
s2=new Sprite(image,147,112);
s1.setPosition(x1, y1);
s2.setPosition(width/2, height/2);
s1.paint(g);
s2.paint(g);
s1.defineCollisionRectangle(5, 25, 120, 62);//設置碰撞區域,5,25是以圖片左上角為原點來設置的碰撞區域,120與62指設置碰撞區域的寬,高
s2.defineCollisionRectangle(5, 25, 120, 62);
this.flushGraphics();
Thread t=new Thread(this);
t.start();
}
public void run(){
long start=0;
long end=0;
while(true){
start=System.currentTimeMillis();
this.keyInfo();
this.draw();
end=System.currentTimeMillis();
if((end-start)<500){
try{
Thread.sleep(500-(end-start));
}
catch(Exception e ){}
}
}
}
public void keyInfo(){
int keystate=getKeyStates();
if ((keystate & UP_PRESSED) != 0) {
y1 -= 10;
}
if ((keystate & DOWN_PRESSED) != 0) {
y1+=10;
}
if ((keystate & LEFT_PRESSED) != 0) {
x1-=10;
}
if ((keystate & RIGHT_PRESSED) != 0) {
x1+=10;
}
}
public void draw(){
if(x1<0){
width=0;
}
if(x1+147>width){
x1=width-147;
}
if(y1<0){
y1=0;
}
if(y1+112>height){
y1=height-112;
}
g.fillRect(0, 0, width, height);
s1.setPosition(x1, y1);
s1.paint(g);
s2.paint(g);
this.flushGraphics();
if(s1.collidesWith(s2, false)){
s2.setVisible(false);//這句的作用是,當s1碰撞到s2時,s2消失
}
}
public void start() {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -