簡體   English   中英

從Android畫布獲取文本

[英]Getting text from canvas Android

在我的代碼中,我已歸檔了分數,該分數在畫布上以繪制文本的形式繪制,並將其放入其中。 現在,當我完成游戲時,我想從畫布上獲得此分數,並從他那里獲取文本,並將其作為額外內容添加到另一個活動(如intent.putExtra)中,並將其添加到arrayList中。

我的代碼: 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);
}
}

我認為這是不可能的。 您的運動中沒有ball.score嗎? 由於存儲了值,因此我認為從這里恢復將比嘗試從Canvas讀取值更簡單。 我認為為您的MovingBall添加吸氣劑就是您的解決方案。

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

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完成游戲后訪問它。 其值將被保留。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM