简体   繁体   中英

Getting text from canvas Android

On my code I have score filed which is drawing on canvas as drawtext and put the score into it. now when I finish the game I want to get this score filed from canvas and take the text from him and put it as extra to another activity (like intent.putExtra) and to add it to arrayList.

my code: MovingBall class

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 class:

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

I don't think it's possible. Is your ball.score unavailable in your activity? Since the value is stored I think it would be simpler to recover from here instead of trying to read it from a Canvas. I think just adding a getter for your MovingBall is your solution.

First, this is your code cleaned up a bit:

Brick[] bricks; will declare array of Brick variables (indexing starts from 0). To create new instance call 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);
}
}

You cannot read back things you have painted on canvas because painting turns characters and numbers to bunch of pixels. What you can do is to store score in additional variable and then use that variable to pass data to another activity with intent. Something like following:

public class GameView extends View{

public int score;
...

Then you can update score in your clash loop:

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

or in your draw code:

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;
}

Actually, since your MovingBall class score variable is public and static you can just access it when game is finished with MovingBall.score in your other activity. Its value will be preserved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM