Hallo,
ich eröffne hier nur einen Thread weil ich wirklich nicht mehr weiter weiß. Habe mit dem WTK von JAVA ein Midlet und eine Canvas Klasse als Leinwand geschrieben. In der Canvas Klasse wird die moveReflect Methode aufgerufen. Diese soll ein Bild über das Display bewegen und wenn es an eine grenze stößt wieder reflektieren. Leider funktioniert es nicht wirklich. Das Bild bewegt sich nach rechts und wenn es die Grenze erreicht, dann bleibt es stehen. Eine statische Variable "xo" soll die Richtung des Bildes vorgeben(true -> nach rechts; false -> links). Leider wird die variable nicht richtig von true auf false gesetzt und umgekehrt. Weiß leider nicht mehr weiter. Ich habe grade ein geistiges Loch im Kopf. Hoffe ihr könnt mir helfen. Danke.
ich eröffne hier nur einen Thread weil ich wirklich nicht mehr weiter weiß. Habe mit dem WTK von JAVA ein Midlet und eine Canvas Klasse als Leinwand geschrieben. In der Canvas Klasse wird die moveReflect Methode aufgerufen. Diese soll ein Bild über das Display bewegen und wenn es an eine grenze stößt wieder reflektieren. Leider funktioniert es nicht wirklich. Das Bild bewegt sich nach rechts und wenn es die Grenze erreicht, dann bleibt es stehen. Eine statische Variable "xo" soll die Richtung des Bildes vorgeben(true -> nach rechts; false -> links). Leider wird die variable nicht richtig von true auf false gesetzt und umgekehrt. Weiß leider nicht mehr weiter. Ich habe grade ein geistiges Loch im Kopf. Hoffe ihr könnt mir helfen. Danke.
Java:
package hello;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class ActionCanvas extends Canvas implements Runnable{
static int w,h;
static byte anchor=Graphics.TOP|Graphics.LEFT;
static final int MAX_GAME_SPEED=33;
static int fps;
static int x=0;
static int y=20;
static int xUfo=10;
static int yUfo=50;
static Image ufoPNG;
static int speedUfo = 1;
static boolean up=false, down=true, left=false, right=false;
static int i=0;
static int velocity=1;
static double a;
static boolean xo = true;
ActionCanvas(){
setFullScreenMode(true);
w=this.getWidth();
h=this.getHeight();
new Thread(this).start(); //Kick-Off Game Loop
}
public void run(){
try {
ufoPNG = Image.createImage("/ufo.png");
while(true){ //Game-Loop
long startTime = System.currentTimeMillis();
repaint(); //->paint()
serviceRepaints();
Thread.yield();
long executionTime = System.currentTimeMillis()-startTime;
if(executionTime<MAX_GAME_SPEED){
Thread.sleep(MAX_GAME_SPEED-(int)executionTime);
fps=1000/MAX_GAME_SPEED;
}else fps=(int)(1000/executionTime);
}
}catch(Exception e){
}
}
public void moveUfo(){
//xUfo+=speedUfo;
//yUfo+=speedUfo;
if (up)yUfo+=speedUfo;
if (down)yUfo-=speedUfo;
if(left)xUfo-=speedUfo;
if(right)xUfo+=speedUfo;
}
static public void moveSin(){
a+=0.15;
yUfo++;
xUfo=(int) (100*Math.sin(a)+w/2);
if(yUfo>h)yUfo=0;
}
public void moveRect(){
i++;
speedUfo+=velocity;
switch(i){
case 15: down=false;right=true; velocity=-1;
break;
case 30: right=false;up=true; velocity=1;
break;
case 45: up=false;left=true; velocity=-1;
break;
case 60: left=false;down=true;i=0; velocity=1;
break;
}
}
public void moveReflect(){
if (xo = true){
if (xUfo<=10){
xUfo=xUfo+1;
System.out.println("xo = true: "+xo);
}else {xo = false;
System.out.println("xo = true + else: "+xo);}
}
if (xo = false){
if (xUfo>= 5){
xUfo=xUfo-1;
System.out.println("xo = false"+xUfo);
}else {xo = true;
System.out.println("xo = false + else"+xUfo);}
}
}
protected void paint(Graphics g) {
//Screen Hintergrund
g.setColor(255,255,255); //Weiß
g.fillRect(0, 0, w, h);
if (ufoPNG!=null) g.drawImage(ufoPNG, xUfo, yUfo,anchor);
g.setColor(0,0,0);
g.drawString("xo= "+xo, 10, 10, anchor);
g.drawString("x= "+xUfo, 10, 20, anchor);
//moveUfo();
// moveRect();
//moveSin();
moveReflect();
}
}