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

2013/5/3

NEED HELP SHOOTING

SWAG SWAG

2013/5/3

#
My character currently can shoot, but I want him to shoot when he gets then gun. Can someone help create a code the will not allow my character to shoot until he touches the gun.
Gevater_Tod4711 Gevater_Tod4711

2013/5/3

#
You just have to use a boolean that is set true if your character tuches the gun:
private boolean gunTuched = false;

public void act() {
    if (getOneIntersectingObject(Gun.class) != null)) {
        gunTuched = true;
    }
    if (gunTuched /*and probably more conditions you can add here*/) {
        shoot();
    }
}
If your gun class isn't called Gun you have to change the name in line 4. And if your character should just be able to shoot if he is still tuching the gun you need to change it like this:
//you don't need the boolean anymore;
public void act() {
    if (getOneIntersectingObject(Gun.class) != null) {
        //the other conditions for shooting;
        shoot();
    }
}
danpost danpost

2013/5/3

#
// if you currently have in your character act method
shoot(); 
// then qualify it with
if (getOneIntersectingObject(Gun.class) != null) shoot();
SWAG SWAG

2013/5/3

#
Gevater_Tod4711 wrote...
You just have to use a boolean that is set true if your character tuches the gun:
private boolean gunTuched = false;

public void act() {
    if (getOneIntersectingObject(Gun.class) != null)) {
        gunTuched = true;
    }
    if (gunTuched /*and probably more conditions you can add here*/) {
        shoot();
    }
}
If your gun class isn't called Gun you have to change the name in line 4. And if your character should just be able to shoot if he is still tuching the gun you need to change it like this:
//you don't need the boolean anymore;
public void act() {
    if (getOneIntersectingObject(Gun.class) != null) {
        //the other conditions for shooting;
        shoot();
    }
}
I want the gun to disappear when we touches it and be able to continue to shoot. The gun wont be with him. Basically the gun enable him to shoot once he touches it once.
Gevater_Tod4711 Gevater_Tod4711

2013/5/3

#
In this case you have to use the first peace of code I gave you. And you should make the gun dispear after your character has set activated his gun.
SWAG SWAG

2013/5/3

#
Gevater_Tod4711 wrote...
In this case you have to use the first peace of code I gave you. And you should make the gun dispear after your character has set activated his gun.
If I place your code then my character can only shoot when he touching the gun. I want it to be able to shoot once to touches the gun only once, then gun disappears.
Gevater_Tod4711 Gevater_Tod4711

2013/5/4

#
Can you post your code? I'm not shure why this occurs.
Kartoffelbrot Kartoffelbrot

2013/5/4

#
public boolean gunTouched()
{
Actor gun = getOneIntersectingObject(Gun.class);
if(a!=null)
{
getWorld().removeObject(gun);
return true;
}
else
return false;
}
If you put this into the act-method it will return false in the next act, after returning true.
Kartoffelbrot Kartoffelbrot

2013/5/4

#
Or you do it like this:
boolean shootAllowed = false;

public void gunTouched()  
{  
Actor gun = getOneIntersectingObject(Gun.class);  if(a!=null)  
{ 
getWorld().removeObject(gun);  
shootAllowed = true; 
}  
else  
shootAllowed = false;  
}  

And then write it in the act method like this:
public void act()
{
gunTouched();
if(shootAllowed && Greenfoot.isKeyDown("space")) 
//or how you want make the player to shoot
{
shoot();
shootAllowed=false;
}
}
If you make it like this way, the player has infinite time to shoot, but after shooting he can't shoot again until he finds the next weapon to pick up or how it should work later.
SWAG SWAG

2013/5/4

#
thanks for your help, but I got it to work.
You need to login to post a reply.