0

내 게임에 도움이 필요합니다. 비트 맵을 사용하여 내 playermodel을 그리는 중입니다. ontouch 이벤트로 모델을 변경하고 싶지만 작동하지 않으며 잘 모르겠습니다. 내가 잘못하고있는 것은 무엇입니까? ( 우선, 내 playerclass는 내가 비트 맵 만드는거야 :invalidate();로 캔버스 비트 맵을 다시 그릴 수 없습니다.

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

내가 원하는 :

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

그리고 여기가 내 비트 맵을 호출하고있어 내 GamePanel에 코드를 (내가 당신에게 당신이이 질문에 필요한 부분 만 제공) 단지 onTouch 이벤트로 플레이어의 이미지를 바꾸고 그냥 작동하지 않기를 원합니다.

+0

향후 코드 포맷을 정리할 수 있습니까? –

답변

0

onTouchEvent(MotionEvent event)()invalidate()으로 전화하여 변경 사항을 확인하십시오.

onDraw()에서 invalidate를 호출하지 않으면 영구 루프가 발생하고 앱 성능이 저하됩니다.

관련 문제