Can't redraw my canvas bitmap with invalidate();

993 Views Asked by At

I need help with my game! I am drawing my playermodel with a bitmap and I want to change the model with an ontouch event, but it is not working and I don't know, what I'm doing wrong: ( First of all here is my playerclass were I'm creating the bitmap:

public class Player extends GameObject{
private Bitmap spritesheet;
private String scoreText;
public int score;
private double dya;
private boolean up;
private boolean playing;
private Animation animation = new Animation();
private long startTime;
public boolean jumpable = false;
public boolean jumphigh;
public boolean falldown;
private int boden;
private boolean sliden;

public Player(Bitmap res, int w, int h, int numFrames) {

    x = 100;
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 +26 ;
    //y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 100;
    dy = 0;
    score = 0;
    height = h;
    width = w;
    boden = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -100;

    Bitmap[] image = new Bitmap[numFrames];
    spritesheet = res;

    for (int i = 0; i < image.length; i++)
    {
        image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
    }
    animation.setFrames(image);
    animation.setDelay(40);
    startTime = System.nanoTime();
}

public void setUp(boolean b){up = b;}

public void update()
{
    //score
    long elapsed = (System.nanoTime()-startTime)/1000000;
    if(elapsed>100)
    {
        score = score+7;
        startTime = System.nanoTime();
        scoreText = ""+score ;
    }
    animation.update();
    if(up == true && jumpable == true){
        jumphigh = true;
      //  dy = -5;
       // System.out.println(up);
    }else{
        jumphigh = false;

    }
    if (jumphigh == true){
        dy = -5;
    }
    if (falldown == true){
        dy = 5;
    }

    if(dy>14)dy = 14;
    if(dy<-14)dy = -14;
    y += dy*2;

    if (y >= boden){
        y = boden;
        jumpable = true;
        falldown = false;
        dy = 0;
    }else if (y <= 60){
        y= 60;
        up = false ;
        jumphigh = false;
        falldown = true;
        dy = 0;

    }
    if (y < GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -100){
        jumpable = false;

    }
}
public  void restart(){
    x = 100;
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 100;
    dy = 0;
    score = 0;

}
public void draw(Canvas canvas)
{
    canvas.drawBitmap(animation.getImage(),x,y,null);

    Paint paint = new Paint();
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF,Typeface.NORMAL));
    paint.setColor(Color.BLACK);
    paint.setTextSize(50);
    canvas.drawText(scoreText, 50 , 50 , paint);

}

public void setY(Canvas canvas){
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -20 ;
    boden = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -20 ;
    draw(canvas);
}
public int getScore(){return score;}
public void setSliden() {sliden = true; }
public boolean getPlaying(){return playing;}
public void setPlaying(boolean b){playing = b;}
public void resetP(){ x = 100;
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 100;
    dy = 0;}
public void resetDYA(){dya = 0;}
public void resetScore(){score = 0;}

And here is my gamepanel code where I'm calling my bitmap ( I give you only the parts you need for this question):

private Player player;

private int PlayerSprite = R.drawable.test;;
private int Playerhight = 120;
private int Playerwight = 115;
private int PlayerFrames = 8;

@Override
public void surfaceCreated(SurfaceHolder holder) {
    player = new Player(BitmapFactory.decodeResource(getResources(), PlayerSprite), Playerwight, Playerhight, PlayerFrames);

    invalidate();
    thread.setRunning(true);
    thread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(event.getAction()==MotionEvent.ACTION_DOWN){

            PlayerSprite = R.drawable.sliden;
            Playerhight = 52;
            Playerwight = 130;
            PlayerFrames  = 1;

        return true;
    }
    if(event.getAction()==MotionEvent.ACTION_UP)
    {
        player.setUp(false);
        PlayerSprite = R.drawable.test;
        Playerhight = 120;
        Playerwight = 115;
        PlayerFrames  = 8;
        return true;
    }

    return super.onTouchEvent(event);
}

public void update() {
    if (player.getPlaying() == true && State == STATE.INGAME) {
        player.update();

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    final float scaleFactorX = getWidth() / (WIDTH*1.f);
    final float scaleFactorY = getHeight() / (HEIGHT*1.f);
    if (canvas != null && State == STATE.INGAME) {
        final int savedState = canvas.save();
        canvas.scale(scaleFactorX, scaleFactorY);
        player.draw(canvas);
        canvas.restoreToCount(savedState);
        invalidate();
    }
}

I want just want to change the image of the player with a onTouch event and its just not working.

1

There are 1 best solutions below

0
On

Just call invalidate() in onTouchEvent(MotionEvent event)() and see the change.

Do not call invalidate in onDraw(), it will cause the permanent loop and reduce the performance of your app.