2015-05-05 70 views
1

在我的代碼中,我將得分檔案作爲drawtext繪製在畫布上,並將分數放入其中。 現在,當我完成遊戲時,我想從canvas獲取此分數,並從他處獲取文本,並將其作爲額外內容添加到另一個活動(如intent.putExtra)並將其添加到arrayList。從畫布中獲取文本Android

我的代碼: MovingBall類

package com.example.brickbreaker; 

// מחלקה שמזיזה את הכדור במשחק 
public class MovingBall extends Thread { 

private Ball ball; // משתנה מסוג כדור 
private Racket racket; // משתנה מסוג מחבט 

// משתנה מסוג בלוק , 30 בלוקים 
private Brick brick1; 
private Brick brick2; 
private Brick brick3; 
private Brick brick4; 
private Brick brick5; 
private Brick brick6; 
private Brick brick7; 
private Brick brick8; 
private Brick brick9; 
private Brick brick10; 
private Brick brick11; 
private Brick brick12; 
private Brick brick13; 
private Brick brick14; 
private Brick brick15; 
private Brick brick16; 
private Brick brick17; 
private Brick brick18; 
private Brick brick19; 
private Brick brick20; 
private Brick brick21; 
private Brick brick22; 
private Brick brick23; 
private Brick brick24; 
private Brick brick25; 
private Brick brick26; 
private Brick brick27; 
private Brick brick28; 
private Brick brick29; 
private Brick brick30; 

private GameView gameView; // משתנה מסוג הצגת המשחק 

public static int score; // ניקוד המשחק 
public static int life; // החיים של השחקן במשחק 

// מתודה בונה של המחלקה 
public MovingBall(Ball b,Racket r,Brick b1,Brick b2,Brick b3,Brick b4,Brick b5,Brick b6,Brick b7, 
     Brick b8,Brick b9,Brick b10, Brick b11,Brick b12,Brick b13,Brick b14,Brick b15, 
     Brick b16,Brick b17,Brick b18,Brick b19,Brick b20,Brick b21,Brick b22,Brick b23, 
     Brick b24,Brick b25,Brick b26,Brick b27,Brick b28,Brick b29,Brick b30,GameView gv) 
{ 
    life=3; 
    score = 0; 
    this.ball = b; 
    this.racket=r; 
    this.brick1=b1; 
    this.brick2=b2; 
    this.brick3=b3; 
    this.brick4=b4; 
    this.brick5=b5; 
    this.brick6=b6; 
    this.brick7=b7; 
    this.brick8=b8; 
    this.brick9=b9; 
    this.brick10=b10; 
    this.brick11=b11; 
    this.brick12=b12; 
    this.brick13=b13; 
    this.brick14=b14; 
    this.brick15=b15; 
    this.brick16=b16; 
    this.brick17=b17; 
    this.brick18=b18; 
    this.brick19=b19; 
    this.brick20=b20; 
    this.brick21=b21; 
    this.brick22=b22; 
    this.brick23=b23; 
    this.brick24=b24; 
    this.brick25=b25; 
    this.brick26=b26; 
    this.brick27=b27; 
    this.brick28=b28; 
    this.brick29=b29; 
    this.brick30=b30; 
    this.gameView = gv; 
} 

// פעולה שמפעילה את הטרד וכאשר היא פועלת היא מוזיזה את הכדור ומציגה אותו על המשחק 
public void run() 
{ 
    while(true) 
    { 
     this.ball.MoveBall(); // הזזת הכדור 
     checkClash(); // בדיקת התנגשות 
     this.gameView.postInvalidate(); // כאשר יש תזוזה של הכדור היא מציירת אותו במשחק מחדש 

     try{ 
      sleep(1); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

// פעולה שבודקת האם יש התנגשות בין הכדור למחבט וגם בין הכדור לבלוקים ומשמיעה סאונד מסוים 
public void checkClash() 
{ 
    // התנגשות כדור במחבט 
    if(ball.Bottom()>=racket.Top()) 
    { 
     //אם הכדור נמצא בטווח האופקי של המחבט אז להקפיץ אחרת להתחיל מהתחלה 
     if(ball.Left()<=racket.Right() && ball.Right()>=racket.Left()) 
     { 
      ball.Clash(); 
      gameView.sounds.play(gameView.soundRacket, 1.0f, 1.0f, 0, 0, 1.5f); 
     } 
     else 
     { 
      ball.Restart(); 
      gameView.sounds.play(gameView.soundLife, 1.0f, 1.0f, 0, 0, 1.5f); 
      life--; 

      if(life==0) 
      { 
       System.exit(0); 
      } 

     } 
    } 

    //-------------- התנגשות הכדור בבלוקים ---------------- //  

    if(ball.Top()==brick1.Bottom()) 
    { 
     if(ball.Left()<=brick1.Right() && ball.Right() >= brick1.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick1.Clash(); 

     } 
    } 

    if(ball.Top()==brick2.Bottom()) 
    { 
     if(ball.Left()<=brick2.Right() && ball.Right() >= brick2.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick2.Clash(); 

     } 
    } 

    if(ball.Top()==brick3.Bottom()) 
    { 
     if(ball.Left()<=brick3.Right() && ball.Right() >= brick3.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick3.Clash(); 


     } 
    } 

    if(ball.Top()==brick4.Bottom()) 
    { 
     if(ball.Left()<=brick4.Right() && ball.Right() >= brick4.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick4.Clash(); 

     } 
    } 

    if(ball.Top()==brick5.Bottom()) 
    { 
     if(ball.Left()<=brick5.Right() && ball.Right() >= brick5.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick5.Clash(); 

     } 
    } 

    if(ball.Top()==brick6.Bottom()) 
    { 
     if(ball.Left()<=brick6.Right() && ball.Right() >= brick6.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick6.Clash(); 

     } 
    } 

    if(ball.Top()==brick7.Bottom()) 
    { 
     if(ball.Left()<=brick7.Right() && ball.Right() >= brick7.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick7.Clash(); 

     } 
    } 

    if(ball.Top()==brick8.Bottom()) 
    { 
     if(ball.Left()<=brick8.Right() && ball.Right() >= brick8.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick8.Clash(); 

     } 
    } 

    if(ball.Top()==brick9.Bottom()) 
    { 
     if(ball.Left()<=brick9.Right() && ball.Right() >=brick9.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick9.Clash(); 

     } 
    } 

    if(ball.Top()==brick10.Bottom()) 
    { 
     if(ball.Left()<=brick10.Right() && ball.Right() >= brick10.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick10.Clash(); 

     } 
    } 

    if(ball.Top()==brick11.Bottom()) 
    { 
     if(ball.Left()<=brick11.Right() && ball.Right() >= brick11.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick11.Clash(); 

     } 
    } 

    if(ball.Top()==brick12.Bottom()) 
    { 
     if(ball.Left()<=brick12.Right() && ball.Right() >= brick12.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick12.Clash(); 

     } 
    } 

    if(ball.Top()==brick13.Bottom()) 
    { 
     if(ball.Left()<=brick13.Right() && ball.Right() >= brick13.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick13.Clash(); 

     } 
    } 

    if(ball.Top()==brick14.Bottom()) 
    { 
     if(ball.Left()<=brick14.Right() && ball.Right() >= brick14.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick14.Clash(); 

     } 
    } 

    if(ball.Top()==brick15.Bottom()) 
    { 
     if(ball.Left()<=brick15.Right() && ball.Right() >= brick15.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick15.Clash(); 

     } 
    } 

    if(ball.Top()==brick16.Bottom()) 
    { 
     if(ball.Left()<=brick16.Right() && ball.Right() >= brick16.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick16.Clash(); 

     } 
    } 

    if(ball.Top()==brick17.Bottom()) 
    { 
     if(ball.Left()<=brick17.Right() && ball.Right() >= brick17.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick17.Clash(); 

     } 
    } 

    if(ball.Top()==brick18.Bottom()) 
    { 
     if(ball.Left()<=brick18.Right() && ball.Right() >= brick18.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick18.Clash(); 

     } 
    } 

    if(ball.Top()==brick19.Bottom()) 
    { 
     if(ball.Left()<=brick19.Right() && ball.Right() >= brick19.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick19.Clash(); 

     } 
    } 

    if(ball.Top()==brick20.Bottom()) 
    { 
     if(ball.Left()<=brick20.Right() && ball.Right() >= brick20.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick20.Clash(); 

     } 
    } 

    if(ball.Top()==brick21.Bottom()) 
    { 
     if(ball.Left()<=brick21.Right() && ball.Right() >= brick21.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick21.Clash(); 

     } 
    } 

    if(ball.Top()==brick22.Bottom()) 
    { 
     if(ball.Left()<=brick22.Right() && ball.Right() >= brick22.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick22.Clash(); 

     } 
    } 

    if(ball.Top()==brick23.Bottom()) 
    { 
     if(ball.Left()<=brick23.Right() && ball.Right() >= brick23.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick23.Clash(); 

     } 
    } 

    if(ball.Top()==brick24.Bottom()) 
    { 
     if(ball.Left()<=brick24.Right() && ball.Right() >= brick24.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick24.Clash(); 

     } 
    } 

    if(ball.Top()==brick25.Bottom()) 
    { 
     if(ball.Left()<=brick25.Right() && ball.Right() >= brick25.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick25.Clash(); 


     } 
    } 

    if(ball.Top()==brick26.Bottom()) 
    { 
     if(ball.Left()<=brick26.Right() && ball.Right() >= brick26.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick26.Clash(); 

     } 
    } 

    if(ball.Top()==brick27.Bottom()) 
    { 
     if(ball.Left()<=brick27.Right() && ball.Right() >= brick27.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick27.Clash(); 

     } 
    } 

    if(ball.Top()==brick28.Bottom()) 
    { 
     if(ball.Left()<=brick28.Right() && ball.Right() >= brick28.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick28.Clash(); 

     } 
    } 

    if(ball.Top()==brick29.Bottom()) 
    { 
     if(ball.Left()<=brick29.Right() && ball.Right() >= brick29.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick29.Clash(); 

     } 
    } 

    if(ball.Top()==brick30.Bottom()) 
    { 
     if(ball.Left()<=brick30.Right() && ball.Right() >= brick30.Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=1; 
      score+=10; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick30.Clash(); 

     } 
    } 
} 

} 

GameView類:

package com.example.brickbreaker; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Message; 
import android.view.View; 

// מחלקה שמציגה את המשחק 
public class GameView extends View{ 

private BallView ballView; // משתנה מסוג הצגת הכדור 
private RacketView racketView; // משתנה מסוג הצגת המחבט 
// משתנה מסוג הצגת הבלוק , 30 בלוקים 
private BrickView brick1; 
private BrickView brick2; 
private BrickView brick3; 
private BrickView brick4; 
private BrickView brick5; 
private BrickView brick6; 
private BrickView brick7; 
private BrickView brick8; 
private BrickView brick9; 
private BrickView brick10; 
private BrickView brick11; 
private BrickView brick12; 
private BrickView brick13; 
private BrickView brick14; 
private BrickView brick15; 
private BrickView brick16; 
private BrickView brick17; 
private BrickView brick18; 
private BrickView brick19; 
private BrickView brick20; 
private BrickView brick21; 
private BrickView brick22; 
private BrickView brick23; 
private BrickView brick24; 
private BrickView brick25; 
private BrickView brick26; 
private BrickView brick27; 
private BrickView brick28; 
private BrickView brick29; 
private BrickView brick30; 

public SoundPool sounds; // משתנה מסוג סאונד פול 
public int soundRacket; // סאונד פגיעה במחבט 
public int soundBrick; // סאונד פגיעה בבלוק 
public int soundLife; // סאונד כאשר יורד חיים 

public Paint paint; // משתנה מסוג צייר 
private MovingBall ball; // משתנה מסוג הזזת הכדור 

// מתודה בונה של המחלקה 
public GameView(Context context) { 
    super(context); 
    paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(70); 
    paint.setStrokeWidth(10); 
    paint.setAntiAlias(true); 
    paint.setFakeBoldText(true); 
    sounds = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    soundRacket = sounds.load(context, R.raw.soundracket,1); 
    soundBrick = sounds.load(context, R.raw.soundbrick,1); 
    soundLife = sounds.load(context, R.raw.soundlife,3); 

} 

// פעולה שמשנה את המשתנים במשחק את הכדור , המחבט והבלוקים 
public void setViews(BallView bv ,RacketView rv,BrickView b1,BrickView b2,BrickView b3,BrickView b4,BrickView b5,BrickView b6,BrickView b7, 
     BrickView b8,BrickView b9,BrickView b10, BrickView b11,BrickView b12,BrickView b13,BrickView b14,BrickView b15, 
     BrickView b16,BrickView b17,BrickView b18,BrickView b19,BrickView b20,BrickView b21,BrickView b22,BrickView b23, 
     BrickView b24,BrickView b25,BrickView b26,BrickView b27,BrickView b28,BrickView b29,BrickView b30) 
{ 
    this.ballView = bv; 
    this.racketView = rv; 
    this.brick1=b1; 
    this.brick2=b2; 
    this.brick3=b3; 
    this.brick4=b4; 
    this.brick5=b5; 
    this.brick6=b6; 
    this.brick7=b7; 
    this.brick8=b8; 
    this.brick9=b9; 
    this.brick10=b10; 
    this.brick11=b11; 
    this.brick12=b12; 
    this.brick13=b13; 
    this.brick14=b14; 
    this.brick15=b15; 
    this.brick16=b16; 
    this.brick17=b17; 
    this.brick18=b18; 
    this.brick19=b19; 
    this.brick20=b20; 
    this.brick21=b21; 
    this.brick22=b22; 
    this.brick23=b23; 
    this.brick24=b24; 
    this.brick25=b25; 
    this.brick26=b26; 
    this.brick27=b27; 
    this.brick28=b28; 
    this.brick29=b29; 
    this.brick30=b30; 
} 

// פעולה שמציירת את המשתנים במשחק כולל ניקוד וחיים 
public void onDraw(Canvas canvas) 
{ 
    canvas.drawColor(Color.WHITE); 
    ballView.Draw(canvas); 
    racketView.Draw(canvas); 
    brick1.Draw(canvas); 
    brick2.Draw(canvas); 
    brick3.Draw(canvas); 
    brick4.Draw(canvas); 
    brick5.Draw(canvas); 
    brick6.Draw(canvas); 
    brick7.Draw(canvas); 
    brick8.Draw(canvas); 
    brick9.Draw(canvas); 
    brick10.Draw(canvas); 
    brick11.Draw(canvas); 
    brick12.Draw(canvas); 
    brick13.Draw(canvas); 
    brick14.Draw(canvas); 
    brick15.Draw(canvas); 
    brick16.Draw(canvas); 
    brick17.Draw(canvas); 
    brick18.Draw(canvas); 
    brick19.Draw(canvas); 
    brick20.Draw(canvas); 
    brick21.Draw(canvas); 
    brick22.Draw(canvas); 
    brick23.Draw(canvas); 
    brick24.Draw(canvas); 
    brick25.Draw(canvas); 
    brick26.Draw(canvas); 
    brick27.Draw(canvas); 
    brick28.Draw(canvas); 
    brick29.Draw(canvas); 
    brick30.Draw(canvas); 
    canvas.drawText("Score :" + " " + ball.score, 20, 100, paint); 
    canvas.drawText("Life :" + " " + ball.life, 1200, 100, paint); 
} 
} 
+2

我知道這與您的問題無關,但作爲開發人員,我很難看到您對brick1,brick2等所做的工作。請考慮用「磚塊」數組替換此代碼,而不是每個可能磚塊的成員。 –

回答

1

我不認爲這是可能的。你的ball.score在你的活動中不可用嗎?由於存儲值,我認爲從這裏恢復會更簡單,而不是嘗試從Canvas讀取它。我認爲爲您的MovingBall添加一個吸氣劑就是您的解決方案。

+0

會嘗試並更新 – Adi

1

首先,這是你的代碼清理了一下:

Brick[] bricks;將宣佈Brick變量(索引從0開始)的陣列。要創建新實例調用bricks = new Brick[30];

package com.example.brickbreaker; 

// מחלקה שמזיזה את הכדור במשחק 
public class MovingBall extends Thread { 

private Ball ball; // משתנה מסוג כדור 
private Racket racket; // משתנה מסוג מחבט 

// משתנה מסוג בלוק , 30 בלוקים 
private Brick[] bricks; 

private GameView gameView; // משתנה מסוג הצגת המשחק 

public static int score; // ניקוד המשחק 
public static int life; // החיים של השחקן במשחק 

// מתודה בונה של המחלקה 
public MovingBall(Ball b,Racket r,Brick[] bricks,GameView gv) 
{ 
    life=3; 
    score = 0; 
    this.ball = b; 
    this.racket=r; 
    this.bricks=bricks; 
    this.gameView = gv; 
} 

// פעולה שמפעילה את הטרד וכאשר היא פועלת היא מוזיזה את הכדור ומציגה אותו על המשחק 
public void run() 
{ 
    while(true) 
    { 
     this.ball.MoveBall(); // הזזת הכדור 
     checkClash(); // בדיקת התנגשות 
     this.gameView.postInvalidate(); // כאשר יש תזוזה של הכדור היא מציירת אותו במשחק מחדש 

     try{ 
      sleep(1); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

// פעולה שבודקת האם יש התנגשות בין הכדור למחבט וגם בין הכדור לבלוקים ומשמיעה סאונד מסוים 
public void checkClash() 
{ 
    // התנגשות כדור במחבט 
    if(ball.Bottom()>=racket.Top()) 
    { 
     //אם הכדור נמצא בטווח האופקי של המחבט אז להקפיץ אחרת להתחיל מהתחלה 
     if(ball.Left()<=racket.Right() && ball.Right()>=racket.Left()) 
     { 
      ball.Clash(); 
      gameView.sounds.play(gameView.soundRacket, 1.0f, 1.0f, 0, 0, 1.5f); 
     } 
     else 
     { 
      ball.Restart(); 
      gameView.sounds.play(gameView.soundLife, 1.0f, 1.0f, 0, 0, 1.5f); 
      life--; 

      if(life==0) 
      { 
       System.exit(0); 
      } 

     } 
    } 

    //-------------- התנגשות הכדור בבלוקים ---------------- //  


    for (int i = 0; i < bricks.length; i++) 
    { 

    if(ball.Top()==brick[i].Bottom()) 
     { 
      if(ball.Left()<=brick[i].Right() && ball.Right() >= brick[i].Left()) 
      { 
       ball.verticalDirection=1; 
       ball.horizontalDirection=-1; 
       score+=10; 
       gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
       brick[i].Clash(); 
      } 
     } 
    } 

} 

} 

GameView class: 

import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Message; 
import android.view.View; 

// מחלקה שמציגה את המשחק 
public class GameView extends View{ 

private BallView ballView; // משתנה מסוג הצגת הכדור 
private RacketView racketView; // משתנה מסוג הצגת המחבט 
// משתנה מסוג הצגת הבלוק , 30 בלוקים 
private BrickView[] bricks; 

public SoundPool sounds; // משתנה מסוג סאונד פול 
public int soundRacket; // סאונד פגיעה במחבט 
public int soundBrick; // סאונד פגיעה בבלוק 
public int soundLife; // סאונד כאשר יורד חיים 

public Paint paint; // משתנה מסוג צייר 
private MovingBall ball; // משתנה מסוג הזזת הכדור 

// מתודה בונה של המחלקה 
public GameView(Context context) { 
    super(context); 
    paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(70); 
    paint.setStrokeWidth(10); 
    paint.setAntiAlias(true); 
    paint.setFakeBoldText(true); 
    sounds = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    soundRacket = sounds.load(context, R.raw.soundracket,1); 
    soundBrick = sounds.load(context, R.raw.soundbrick,1); 
    soundLife = sounds.load(context, R.raw.soundlife,3); 

} 

// פעולה שמשנה את המשתנים במשחק את הכדור , המחבט והבלוקים 
public void setViews(BallView bv ,RacketView rv,BrickView[] bricks) 
{ 
    this.ballView = bv; 
    this.racketView = rv; 
    this.bricks=bricks; 
} 

// פעולה שמציירת את המשתנים במשחק כולל ניקוד וחיים 
public void onDraw(Canvas canvas) 
{ 
    canvas.drawColor(Color.WHITE); 
    ballView.Draw(canvas); 
    racketView.Draw(canvas); 
    for (int i = 0; i < bricks.length; i++) 
    { 
     bricks[i].Draw(canvas); 
    } 
    canvas.drawText("Score :" + " " + ball.score, 20, 100, paint); 
    canvas.drawText("Life :" + " " + ball.life, 1200, 100, paint); 
} 
} 

你無法讀回你在畫布上畫油畫,因爲原來的字符和數字到組像素的事情。你可以做的是將score存儲在附加變量中,然後使用該變量將數據傳遞給另一個活動。類似以下內容:

public class GameView extends View{ 

public int score; 
... 

然後你就可以在你的衝突循環更新得分:

 if(ball.Left()<=brick[i].Right() && ball.Right() >= brick[i].Left()) 
     { 
      ball.verticalDirection=1; 
      ball.horizontalDirection=-1; 
      score+=10; 
      gameView.score = score; 
      gameView.sounds.play(gameView.soundBrick, 1.0f, 1.0f, 0, 0, 1.5f); 
      brick[i].Clash(); 
     } 

或在您的抽獎代碼:

public void onDraw(Canvas canvas) 
{ 
    canvas.drawColor(Color.WHITE); 
    ballView.Draw(canvas); 
    racketView.Draw(canvas); 
    for (int i = 0; i < bricks.length; i++) 
    { 
     bricks[i].Draw(canvas); 
    } 
    canvas.drawText("Score :" + " " + ball.score, 20, 100, paint); 
    canvas.drawText("Life :" + " " + ball.life, 1200, 100, paint); 
    score = ball.score; 
} 

實際上,因爲你MovingBallscore變量publicstatic當你在其他活動中使用MovingBall.score完成遊戲時,你可以訪問它。它的價值將被保留。