2013-02-15 2 views
0

J'ai une question à mon code. Je veux que mon bitmap change chaque fois que quelqu'un touche l'affichage.Modifier le bitmap onDraw lorsque vous touchez l'écran

protected boolean processTouch(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 

     player.setGraphic(BitmapFactory.decodeResource(Game.res, R.drawable.sprite2)); 

    } 
    return true; 
} 

Dans mon fichier player.java je mets:

public class Player extends Sprite 
{ 
public Player(int x, int y) 
{ 
    super(BitmapFactory.decodeResource(Game.res, R.drawable.sprite1), x-350, y, 50, 17, 5, 2); 

} 
public void update() 
{ 
getGraphic(); 
} 

public Bitmap getGraphic() 
{ 
    return bitmap; 
} 
public void setGraphic(Bitmap bitmap) 
{ 
    this.bitmap = bitmap; 
} 


private Bitmap bitmap;} 

Ma classe Sprite contient:

public Sprite(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount) 
{ 
    this.bitmap = bitmap; 

} 
public Bitmap getGraphic() 
{ 
    return bitmap; 
} 
private Bitmap bitmap; 

Et puis je veux afficher:

canvas.drawBitmap(s.getGraphic(), s.getSourceRect(), destRect, null); 

Cependant, lorsque j'essaie de démarrer le jeu, il se bloque. Comment puis-je résoudre ce problème?

EDIT:

le journal:

02-15 15:29:51.174: I/ActivityManager(18563): Displayed com.example.nic_beta/.Game: +488ms 
02-15 15:29:51.197: W/dalvikvm(29635): threadid=11: thread exiting with uncaught exception (group=0x419cb930) 
02-15 15:29:51.197: E/AndroidRuntime(29635): FATAL EXCEPTION: Thread-23004 
02-15 15:29:51.197: E/AndroidRuntime(29635): java.lang.NullPointerException 
02-15 15:29:51.197: E/AndroidRuntime(29635): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1025) 
02-15 15:29:51.197: E/AndroidRuntime(29635): at android.graphics.Canvas.drawBitmap(Canvas.java:1127) 
02-15 15:29:51.197: E/AndroidRuntime(29635): at com.example.nic_beta.Panel.onDraw(Panel.java:70) 
02-15 15:29:51.197: E/AndroidRuntime(29635): at com.example.nic_beta.RunnerThread.run(Panel.java:187) 
02-15 15:29:51.213: W/ActivityManager(18563): Force finishing activity com.example.nic_beta/.Game 
02-15 15:29:51.260: W/ActivityManager(18563): Force finishing activity com.example.nic_beta/.Menu 
+0

En cas de panne, affichez le logcat. De plus, décoder le bitmap à chaque toucher serait très coûteux, il serait préférable de le décoder une fois au début et de le placer sur le bitmap pré-décodé au toucher. –

Répondre

0

Pour l'erreur throwIfRecycled, this is a possible solution. Mais, comme l'a dit Gabe, vous devriez décoder votre bitmap une fois lorsque votre application est créée, puis dessiner quand vous le souhaitez.

Questions connexes