Java ME Canvas Klasse Gameloop und Funktion

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.

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();
     
        


    }



}
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
M Wie in einer anderen "nicht canvas" klasse zeichne Android & Cross-Platform Mobile Apps 5
Arif Android Canvas wird nicht gezeichnet? Android & Cross-Platform Mobile Apps 0
Dechasa Android Android - Canvas NullpointException Android & Cross-Platform Mobile Apps 2
Z Canvas onDraw() Variablen muss in der Methode initialisiert werden Android & Cross-Platform Mobile Apps 1
F Mit Canvas zeichen und durch einen Timer aktualiesieren Android & Cross-Platform Mobile Apps 1
J Canvas mit dem Accelorometersensor bewegen Android & Cross-Platform Mobile Apps 0
E Android Zeichnen auf Canvas - Teilbereich als Bild speichern Android & Cross-Platform Mobile Apps 3
B onDraw Canvas jede Millisekunde aufrufen? Android & Cross-Platform Mobile Apps 12
G canvas in view anzeigen Android & Cross-Platform Mobile Apps 10
S Android Canvas - drawText - Update Text in ActionListener Android & Cross-Platform Mobile Apps 8
B Java ME Canvas Android & Cross-Platform Mobile Apps 2
B Java ME Leere Canvas Seite Android & Cross-Platform Mobile Apps 21
P Java ME Texteingabe Canvas Android & Cross-Platform Mobile Apps 2
D wie geht Klassenvererbung von Canvas Klassen Android & Cross-Platform Mobile Apps 2
U Probleme mit der drawString Methode bei Canvas Android & Cross-Platform Mobile Apps 8
H Canvas kann nichts auf Graphics drawen Android & Cross-Platform Mobile Apps 3
M Textfeld in Canvas, implementation der keys aendern Android & Cross-Platform Mobile Apps 8
M Text in Canvas scrollen lassen Android & Cross-Platform Mobile Apps 13
U ein texteingabefeld auf einem Canvas . Android & Cross-Platform Mobile Apps 2
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
F Canvas Stringlänge Android & Cross-Platform Mobile Apps 2
B Was fehlt mir hier für diese Klasse? ich habe es mit (>>> ... <<<) markiert. Android & Cross-Platform Mobile Apps 4
W aus Fragmente in andere Klasse wechseln mit Button Android & Cross-Platform Mobile Apps 3
J Android Datenbank Klasse nur für CRUD-Operation, oder auch mehr ? Android & Cross-Platform Mobile Apps 8
H Android Aufrufen von "setContentView" in einer anderen Klasse Android & Cross-Platform Mobile Apps 3
ms_cikar Thread / Intent als externe klasse Android & Cross-Platform Mobile Apps 1
S Android CheckBox Status von anderer Klasse aus ändern Android & Cross-Platform Mobile Apps 4
M Android Switch-Klasse stoppt Android App / java.lang.NullPointerException Android & Cross-Platform Mobile Apps 5
C Auf innere (Thread)Klasse zugreifen, von anderer Klasse aus Android & Cross-Platform Mobile Apps 3
G Klasse im selben Package aufrufen Android & Cross-Platform Mobile Apps 18
A Fehler beim Starten eines Intents - alles in einer Klasse funktioniert... Android & Cross-Platform Mobile Apps 4
A Kann nicht in TextView einer anderen Klasse schreiben - wieso? Android & Cross-Platform Mobile Apps 9
B Android GameLoop ruckelt? Android & Cross-Platform Mobile Apps 4

Ähnliche Java Themen

Neue Themen


Oben