This site requires JavaScript, please enable it in your browser!
Greenfoot back
Prish950
Prish950 wrote ...

2013/2/23

BOMBERMAN HELP PLEASE FOR!!! int =??

Prish950 Prish950

2013/2/23

#
I´m creating a Bomberman game and i want that the player can just set 5 Bombs!!!!
if (Greenfoot.isKeyDown("q"))
       {
           for(int i=0; i<=5; i++)
         {
            if (i=5)
           {
            
           }
           else
           {
            bombSetzen();
           }
         }
        }
The problem is that i=5 is a syntax error !!!! PLEASE HELP!
Jonas234 Jonas234

2013/2/23

#
you need to write i ==5 if you want to compare them. with i = 5 you are assigning the value 5 to i . btw. this should do the same:
 for(int i=0; i<5; i++)  
         {  
            bombSetzen();  

         }  
Jonas
Prish950 Prish950

2013/2/23

#
OMG THX IT WORKS :D
Prish950 Prish950

2013/2/23

#
But i want that my player just can set 5 bombs anyway with this code he can sett more bombs???
Jonas234 Jonas234

2013/2/23

#
It is doing exactly the same as before. cause the for loop ist just running to 5 and i is a local variable. He is setting with both codes 5 bombs the moment you press q. If you want him to place 5 bombs overall and 1 bomb per keypress you should have sth. like:
int i = 0;
boolean keyPressed = false;
private void setBomb()
{
    if(Greenfoot.isKeyDown("space") & !keyPressed)
    {
        if(i<5 )
        {
            bombSetzen();
            i++;
         }    
        keyPressed = true;
    }
    else
    { 
        keyPressed = false;
    }
}
This way he will set a Bomb each time you press q till he has planted 5 Bombs. You need the keyPressed if you want him to plant 1 bomb per press otherwise he will place in each act a bomb if you hold q down. Jonas
danpost danpost

2013/2/23

#
The code provided so far will produce 5 bombs at one location at one instance of time and at each instance of time that the 'q' key is held down. I think what you are really asking is that you want to limit your bomberman to placing up to five bombs in the world at as many as five different locations at any instance. Or, in simpler terms, you want to limit the number of bombs in the world at any one time to 5. The code for this is quite simple: if (getWorld().getObjects(Bomb.class).size()<5) getWorld().addObject(new Bomb(), getX(), getY()); Still the problem with creating multiple bombs on a single keypress remains. To solve this, refer to this discussion which deals with keypresses/keystrokes. EDIT: the previous post was not up when this was being written.
danpost danpost

2013/2/23

#
@Jonas234, your code will only delay each successive bomb from being created by one act cycle. After a bomb is placed in the world, line 5 on the next act cycle will return false and the boolean will be changed back to false. Then, on the next act cycle a new bomb will again be placed in the world if the key is still being pressed (which it probably will be, as even the shortest of keypresses will last several act cycles).
Jonas234 Jonas234

2013/2/23

#
yeah you are right ^^ need to be an else if with (!Greenfoot.isKeyDown("space")) forgot something. Jonas
Prish950 Prish950

2013/2/23

#
THX SO MUCH DANPOST NOW IT WORKS
You need to login to post a reply.