Slick Forums

Discuss the Slick 2D Library
It is currently Tue May 21, 2013 2:20 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Array reading
PostPosted: Mon Aug 13, 2012 12:42 pm 
Offline

Joined: Mon Aug 13, 2012 12:41 pm
Posts: 3
Hello, I'm creating 2D FPS game with Slick 2D game engine. Where user need to shoot zombies. I made, that zombies would appear every 5 seconds. But I have I bug. If there are more than one zombie currently on the screen, user, can shoot only that, who appeared in the screen earliest. I will make example :
5 seconds...
Zombie 1 appears
I shoot him
5 seconds...
Zombie 2 appears
5 seconds
Zombie 3 appears

In this situation, I can shoot only zombie 2(he is the earlieast), and zombie 3 is untouchable.

This is code, which checks if user hits zombie:

Code:
   Input in = gc.getInput();
// Check every array element
            for(int a=0; a<=counter; a++){
               if(zombies[a] != null){
                  // Increasing zombie
                  zombies[a].setWidth(getSpeed()*delta);
                  zombies[a].setHeight(getSpeed()*delta);
                  // Checking if user hit zombie
                  if(((posX > zombies[a].getZombieX()) && (posX < zombies[a].getZombieX()+zombies[a].getWidth()))&&((posY > 600-zombies[a].getHeight()-zombies[a].getZombieY())&&(posY < 600-zombies[a].getZombieY()))){
                     if(in.isMousePressed(0)){
                        System.out.println(counter);
                        System.out.println("Zombie "+a);
                        if(currentWeapon.getAmmo() > 0){
                           zombies[a].shooted(currentWeapon.getDamage());
                           currentWeapon.shooted();
                           currentWeapon.shoot();
                           
                        }else{
                           
                        }
                     }
                     if(zombies[a].isAlive() == false){
                        zombies[a] = null;
                        countingDeads++;
                        addPoints(50);
                     }
                  }
}


I realised, that this loop only checks earlieast array element, which is not null. But I need to check every element. (Counter variable, which is used in loop header, increases every 5 seconds, until he reaches 9).
I added some println statements in the code, and realised, that my theory is true.
So, how I can fix this? (Make loop to check all existing elements not only earliest)?


Top
 Profile  
 
 Post subject: Re: Array reading
PostPosted: Tue Aug 14, 2012 8:05 am 
Offline
User avatar

Joined: Wed Jun 20, 2012 8:10 pm
Posts: 54
You're coding in Java, why would you use array? :shock:

If you were to use a List (ArrayList for example), you could do something like :

list.add(newZombie);

and then to check do :

Code:
for (Zombie zombie : list){

if (myshoot.x > zombie.getX() && myshoot.x > zombie.getX() + zombie.getWidth() .....){
... do treatment ...
}

}


Here we don't know how your zombie are added, and the : zombie[a] = null; is just ... wow! Of course it will NOT work, this is not the way to code this at all!
This is basic OO programming... This is not a slick issue...

_________________
Seventeen


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group