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

2019/6/26

Change weapon void doesn't work

Jargy Jargy

2019/6/26

#
Hello. I'm sorry for my bad English. I have problem with my weapon_change function. It doesn't work. When i press space nothing happen. I can't understand why.. My code: public void act() { moveAround(); Change_weapon(); // if i comment then it can shoot if(standart_weapon == 1) { delay++; if(delay > 30) { Shoot(); delay = 0; } } else { Shoot(); } //GetLevel = ((MyWorld)getWorld()).level; } public void Change_weapon() { if("1".equals(Greenfoot.getKey())) { machine_gun = 0; bazzuka = 0; standart_weapon = 1; } if("2".equals(Greenfoot.getKey())) { machine_gun = 1; bazzuka = 0; standart_weapon = 0; } if("3".equals(Greenfoot.getKey())) { machine_gun = 0; bazzuka = 1; standart_weapon = 0; } } public void moveAround() { if(Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+5); } if(Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-5); } }
Super_Hippo Super_Hippo

2019/6/26

#
Try to do it with the "isKeyDown" method instead of "getKey". The "getKey" method "uses up" the key. So if you press "space", the first "Greenfoot.getKey" gets the "space" and all others won't. You don't need to have a variable for each weapon. You can have a "weaponType" variable, set it to 0 for the standard weapon, 1 for the machine gun and 2 for the bazooka. Then you only need a single variable no matter how many more weapons you will add.
danpost danpost

2019/6/26

#
If this method is the only place that you are using getKey, then you can placed the returned value in a local variable and check it amongst all the acceptable values:
String key = Greenfoot.getKey();
if (key == null) return;
if ("1".equals(key))
{
    ...
}
else if ("2'.equals(key))
{
    ...
}
...
Jargy Jargy

2019/6/28

#
oh thank you very much
You need to login to post a reply.