开发者

Clearing the screen of all sprites for a reset

开发者 https://www.devze.com 2023-03-11 02:26 出处:网络
I can not seem to get the reset to work properly. I tried numerous things and finally the most recent thing I tried was dual for loops to go through and search the screen for a sprite and remove it. T

I can not seem to get the reset to work properly. I tried numerous things and finally the most recent thing I tried was dual for loops to go through and search the screen for a sprite and remove it. This is causing a force close though. If anyone has a better easier method to make it reset the screen of all sprites (just make them off the screen the timers are resetting fine). The reset works with the exception of the added for loops.

public void resetGame() {


    for(float w=0; w<width; w+=1){
        for(float h=0; h<height; h+=1){
            if (zom1.isCollision(w, h))
                normZombie.remove(zom1);
        }
    }

}

Here is the collisio开发者_运维技巧n from the zombies class.

public boolean isCollision(float x2, float y2) {

    return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
}

Here is the logcat

06-05 17:49:36.345: ERROR/AndroidRuntime(5390): FATAL EXCEPTION: Thread-11 06-05 17:49:36.345: ERROR/AndroidRuntime(5390): java.lang.NullPointerException 06-05 17:49:36.345: ERROR/AndroidRuntime(5390): at com.game.Game.resetGame(Game.java:269) 06-05 17:49:36.345: ERROR/AndroidRuntime(5390): at com.game.Game.gameOver(Game.java:312) 06-05 17:49:36.345: ERROR/AndroidRuntime(5390): at com.game.Game.run(Game.java:218) 06-05 17:49:36.345: ERROR/AndroidRuntime(5390): at com.game.GameLoopView$GameLoopThread.run(GameLoopView.java:56) 06-05 17:54:37.853: ERROR/yahoo.push.service(1492): mail_38_mail Exception while talking to server! However we are already in disconnect state ignore it. 06-05 17:55:00.009: ERROR/Tethering(1086): attempting to remove unknown iface (usb0), ignoring

Thanks


Null pointer Eception:

I would suggest adding a

if(zom1 != null && zom1.isCollision(W,H)){
   if(normZombie != null && normZombie.remove(zom1)

(sorry not sure which is line 269 in Game.java

Regarding you removing problem I'm nt sure what your trying to accomplish with the code above

if normZombie is a List why not do

normZombie.removeAll();

if your problem is you don't want to diplsay them anymore then in your redering loop:

paintOrRender(...){
if(displayZombiesBoolean){
   for(Zombies z : normZombies){
      z.show(...);
   }
}

hope this helps

Jason

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号