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

2013/7/2

Shooting

RahulUK RahulUK

2013/7/2

#
I have a space ship and I want to make it fire at enemy ships. How do I do this?
Zamoht Zamoht

2013/7/2

#
What do you mean by shooting? Please be more specific. Is it like space invader, automatic like in this scenario or a mix (not automatic but still able to shoot in different directions)?
eddietheboss27 eddietheboss27

2013/7/2

#
.
RahulUK RahulUK

2013/7/4

#
By shooting i mean like space invaders ship.
Zamoht Zamoht

2013/7/4

#
Create a bullet/projectile class and set it's act method to something like this:
public void act()
{
     setLocation(getX(), getY() - 1); //this will move the bullet up.
    //(getX(), getY() + 1) is down
    //(getX() + 1, getY()) is right
    //(getX() - 1, getY()) is left
}
Then in your spaceship class add this shoot method:
public void shoot()
{
     getWorld().addObject(new Bullet(), getX, getY); //this will add a new Bullet object (change the name to whatever you want to call the bullet/projectile class) to the world at the spaceships location.
}
public void act()
{
     if (Greenfoot.isKeyDown("space"))
     {
          shoot();
     }
}
Haven't tested the code, but it should work. This should add a ton of bullets to the world, when spacebar is pressed. To fix this you should add a counter.
RahulUK RahulUK

2013/7/9

#
ok Thanks i will try it out Zamoht
You need to login to post a reply.