2017-03-03 104 views
0

我不需要任何答案!更新畫布動畫

我創建了一個應用程序,應該有氣泡在屏幕上跳動。 一切正在工作,但他們沒有被生活繪製。

下面的代碼(不是所有的代碼,但重要的代碼):

的氣泡類別:

public class Bubble{ 

    private int ID; 

    private float coordX; 
    private float coordY; 
    private float velX; 
    private float velY; 
    private float radius; 

    private int color; 

    private boolean popped; 
    private boolean alive; 
    private boolean dead; 

    public Bubble(int coordX, int coordY, int velocityX, int velocityY, int ID, int color) { 
     this.coordX = coordX; 
     this.coordY = coordY; 
     this.velX = velocityX; 
     this.velY = velocityY; 
     this.radius = 30; 
     this.ID = ID; 
     this.color = color; 

     this.alive = true; 
    } 

    public void drawMe(Paint paint, Canvas canvas) { 
     int paintStartColor = paint.getColor(); 
     paint.setColor(this.color); 
     canvas.drawCircle(this.coordX, this.coordY, radius, paint); 
     paint.setColor(paintStartColor); 
     Log.d("Bubble","drawn bubble [" + ID + "] at (" + this.coordX + "|" + this.coordY + ")"); 
    } 

    public void update() { 

     //damit die bubble am rand anstößt und zurück prallt 
     if(this.coordX - this.radius <= 0) this.velX = - this.velX; 
     if(this.coordY - this.radius <= 0) this.velY = - this.velY; 
     if(this.coordX + this.radius >= MainActivity.getInstance().getScreenSize().x) this.velX = - this.velX; 
     if(this.coordY + this.radius >= MainActivity.getInstance().getScreenSize().y) this.velY = - this.velY; 

     coordX += velX; 
     coordY += velY; 
     updateRadius(); 
//  Log.d("Bubble", "CoordX: " + coordX + ", velX: " + velX); 
    } 
} 

的繪圖方法:

public void drawBubbles() { 
    Paint paint = new Paint(); 
    for(Bubble b : bubbles) { 
     b.drawMe(paint, MainActivity.getInstance().getCanvas()); 
    } 
} 

而且遊戲循環:

while(this.isRunning()) { 
     updateBubbles(); //update bubbles just calls the update() function in every bubble (for each loop) 
     drawBubbles(); 
     //...sleep... 
} 

繪製所有的氣泡,它的工作原理,但我只看到一個區別,當我離開(不接近!)的應用程序,然後回到它。我在循環中調用該方法,並且每秒調用60次,但爲什麼它不出現在屏幕上?我也無法使畫布無效。

+0

好吧我添加了Bubble類和遊戲循環,我希望它已經足夠了...讓我知道如果你需要更多的代碼 – Finni

回答

0

我剛剛濫用invalidate()方法,所以它修好後,我修好了。