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

2012/12/17

Shooting problem

Hawx_ Hawx_

2012/12/17

#
I am trying to make my character shoot by adding a new "Bullet".
  public void Shoot()
    {
     if (Greenfoot.isKeyDown("space"))
 
         getWorld().addObject(Bullet, getX(), getY());
    }
    
This is in my main character code. When I compile it says "Cannot find symbol varible bullet", the class name is called Bullet. I need it to place the "Bullet" where my rocket is , which is why I have getX() and GetY(). I don't understand why this isn't working. Please help.
vonmeth vonmeth

2012/12/17

#
You need to create your bullet before you can add it to the world.
public void Shoot()  
  {  
   if (Greenfoot.isKeyDown("space"))  {
       Bullet bullet = new Bullet(getRotation());
       getWorld().addObject(bullet, getX(), getY());  
      }
  }  
Hawx_ Hawx_

2012/12/17

#
ok so , we've go this
public void Shoot()    
      {    
       if (Greenfoot.isKeyDown("space"))  {  
           Bullet bullet = new Bullet();  
           getWorld().addObject(bullet, getX(), getY());    
          }  
      }    
    
but it is producing several bullets , but I only want 1 producing. help!? please
danpost danpost

2012/12/17

#
The problem is the usage of 'isKeyDown', which will be 'true' every act cycles that executes with the key being pressed. You can instead use 'getKey' as follows:
public void Shoot()
{
    if ("space".equals(Greenfoot.getKey()))
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
    }
}
Or, as an alternative, you can add a boolean field to the instance called 'spaceDown' and use the following
// using the instance field
private boolean spaceDown;
// the Shoot method
public void Shoot()
{
    if (!spaceDown && Greenfoot.isKeyDown("space")) 
    {
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
        spaceDown=true;
    }
    if(spaceDown && !Greenfoot.isKeyDown("space")) spaceDown=false;
}
Hawx_ Hawx_

2012/12/17

#
thanks! :) but I need a code so that when the bullet gets to the world edge it disappears. I've tried if(atWorldEdge()) but it doesn't like that, please help.
danpost danpost

2012/12/17

#
I will need (at least) the following information to help (1) the 'super' call are you using to create the world (2) the code in your 'atWorldEdge' method (3) 'Bullet' class code Thanks.
Hawx_ Hawx_

2012/12/17

#
Super code
 public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }
Bullet code (this is all I have in there at the moment) :
   public void act() 

    {
        move(3);


    }
}
2) 'atWorldEdge' method - ??? I don't think I have one.. Thanks
danpost danpost

2012/12/17

#
OK, the 'atWorldEdge' method is a method in the 'Animal' class that can be imported into a scenario; however, that is not really quite what you need. What you need is a method that checks to see if the location (returned with the 'getX' and 'getY' methods) is at an edge and returns a boolean indicating that state.
private boolean atWorldEdge()
{
    return (getX()==0 ||
            getY()==0 ||
            getX()==getWorld().getWidth()-1 ||
            getY()==getWorld().getHeight()-1);
}
Then in the act method, add the following:
if(atWorldEdge())
{
    getWorld().removeObject(this);
    return;
}
The method check all four edges (left, top, right and bottom) and returns 'true' if the bullet resides at any of the edges. The 'return' statement exits the 'act' method immediately, as once the bullet is removed, no more action is required of it (and trying to access the world or the location (or anything requiring its location) of the object in the world after it is removed will cause a run-time error).
Hawx_ Hawx_

2012/12/17

#
Thanks :) Great help :D Hawx
AmericanaStorm AmericanaStorm

2015/2/1

#
This might be weird but would be the coding for the bullet to shoot in the actual bullet class and not the shooter class. Thanks
danpost danpost

2015/2/1

#
AmericanaStorm wrote...
This might be weird but would be the coding for the bullet to shoot in the actual bullet class and not the shooter class. Thanks
It sure does sound weird. It is not the behavior of a bullet to shoot a bullet. That behavior belongs to that of the shooter. The usual behavior of the bullet is to fly through the air until a target is reached or until it goes 'beyond' the end of the world (rather, reaches the edge of the world). Sometimes, instead of the bullet having the behavior to 'reach a target', the target will consume the bullet and lose health or die. The interaction between the bullet and any target type should only be done once (in one of the two classes -- never in both).
You need to login to post a reply.