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

2015/2/23

Game Problem: Spawining Bullets

NoobCoder NoobCoder

2015/2/23

#
  public void shoots()
    {
        counter++;
       if (counter >= 100) {  // when counter reaches 100 counts fire new bullet
           Bullet bullet = new Bullet();
           int X = Greenfoot.getRandomNumber(800); //get width of screen
           int Y = Greenfoot.getRandomNumber(600); // get height of screen
           addObject(bullet, X, Y); 
           counter =0;
        }
    
    }
    
Hello, the objective of my game is to spawn random bullets to shoot from the outside world boundary, for which my character has to dodge. However when I run this, there is no syntax error but the bullets are not fired.
Super_Hippo Super_Hippo

2015/2/23

#
This method will spawn a bullet somewhere in the world. Did you select an image for your bullets? Do you call the method from the act method?
NoobCoder NoobCoder

2015/2/23

#
Ooooooooooooh I didn't call it from the act method, thxs bro it works now.
NoobCoder NoobCoder

2015/2/23

#
Sorry I got happy a bit to soon.... I have another problem where my bullet spawns but are not coming from outside my world but from random coordinates such as the middle which I don't want. Is there a way make my bullet fire into the world starting of the game world?
NoobCoder NoobCoder

2015/2/23

#
out of the game world *
fejfo fejfo

2015/2/23

#
I don't get what u want ? please show your act code of bullet
lordhershey lordhershey

2015/2/23

#
You have to pick a wall, if it is the top or the bottom you only need a random number for the X Coordinate, and for the sides only a random Y coordinate - something kind of like:
public void shoots()
{
    counter++;
    if (counter >= 100) {  // when counter reaches 100 counts fire new bullet
        Bullet bullet = new Bullet();

        int X = 0;
        int Y = 0;

        switch(Greenfoot.getRandomNumber(4))
        {
        case 0:
                Y = 600;
        case 1:
                X = Greenfoot.getRandomNumber(800);
        break;
        case 2:
                X = 800;
        default:
                Y = Greenfoot.getRandomNumber(600); 
        break;
        } 

        addObject(bullet, X, Y); 
        counter =0;
        }
}
Super_Hippo Super_Hippo

2015/2/23

#
Make sure you create your world with 'super(width,height,cellsize,false)'. The 'false' will make the world unbounded, so object can be outside the world boundaries. Then you could use that to create a bullet outside the world:
int x=0,y=0;
do {x = Greenfoot.getRandomNumber(getWidth()+200)-100;} while (x>-5 && x<getWidth()+5);
do {y = Greenfoot.getRandomNumber(getHeigth()+200)-100;} while (y>-5 && y<getHeight()+5);
addObject(new Bullet(), x, y);
counter = 0;
Note: Bullets appear outside the world. If you don't rotate them to somewhere inside the world, they can fly (if they fly straight) to nowhere. So you should code it that they will pass the world once and remove it when they are outside again or you just spawn enough and remove them when they are outside the world +- 150 or something like that.
You need to login to post a reply.