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

2014/4/4

Enemies code

1
2
GamesGrinder1998 GamesGrinder1998

2014/4/4

#
HOW DO YOU MAKE ENEMIES SHOOT AT YOU, I'VE DESIGNED AN IMAGE FOR IT BUT I DON'T KNOW THE CODE...PLEASE HELP I'VE GOT AN ASSIGNMENT DUE NEXT WEEK...NEED ASAP!!!
davmac davmac

2014/4/4

#
Turn off your caps lock and please DO NOT SHOUT.
GamesGrinder1998 GamesGrinder1998

2014/4/4

#
Sorry about that...just stressed out a bit....how do you make enemies shoot at you, as stated already i designed an image for what the enemy has to shoot...i want it to shoot down because my starfighter is at the bottom...please help
Mutton Mutton

2014/4/4

#
I use the getWorld().addObjects() method, it lets your enemy drop an object straight into the world. If you were to name your bullet class bullet, then it would look something like this: getWorld().addObject(new bullet(), getX(), getY()); however, that sends the bullet travelling left, so if you wanted to shoot down it would look like: getWorld().addObject(new bullet(90), getX(), getY()); and in the bullet class you would put public bullet(int rotation) { setRotation(rotation); } at least, that's how I do it. Hope it helps!
GamesGrinder1998 GamesGrinder1998

2014/4/4

#
Mutton wrote...
I use the getWorld().addObjects() method, it lets your enemy drop an object straight into the world. If you were to name your bullet class bullet, then it would look something like this: getWorld().addObject(new bullet(), getX(), getY()); however, that sends the bullet travelling left, so if you wanted to shoot down it would look like: getWorld().addObject(new bullet(90), getX(), getY()); and in the bullet class you would put public bullet(int rotation) { setRotation(rotation); } at least, that's how I do it. Hope it helps!
i used the code and when i pressed run, it didn't go anywhere, it was just in front of the enemy
Mutton Mutton

2014/4/4

#
GamesGrinder1998 wrote...
Mutton wrote...
I use the getWorld().addObjects() method, it lets your enemy drop an object straight into the world. If you were to name your bullet class bullet, then it would look something like this: getWorld().addObject(new bullet(), getX(), getY()); however, that sends the bullet travelling left, so if you wanted to shoot down it would look like: getWorld().addObject(new bullet(90), getX(), getY()); and in the bullet class you would put public bullet(int rotation) { setRotation(rotation); } at least, that's how I do it. Hope it helps!
i used the code and when i pressed run, it didn't go anywhere, it was just in front of the enemy
you still need to tell the bullet to move forward in it's act method, what I gave you was just to get it to spawn. in the bullet's act method put move(5); and that should fix that problem
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
I did work but a lot of them came down at a time...i need it to only shoot one bullet but only have some enemies shoot at a time, not all of them shoot simeoutaneously...how do i fix that problem (do i use the get random method???)
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
Mutton wrote...
GamesGrinder1998 wrote...
Mutton wrote...
I use the getWorld().addObjects() method, it lets your enemy drop an object straight into the world. If you were to name your bullet class bullet, then it would look something like this: getWorld().addObject(new bullet(), getX(), getY()); however, that sends the bullet travelling left, so if you wanted to shoot down it would look like: getWorld().addObject(new bullet(90), getX(), getY()); and in the bullet class you would put public bullet(int rotation) { setRotation(rotation); } at least, that's how I do it. Hope it helps!
i used the code and when i pressed run, it didn't go anywhere, it was just in front of the enemy
you still need to tell the bullet to move forward in it's act method, what I gave you was just to get it to spawn. in the bullet's act method put move(5); and that should fix that problem
I did work but a lot of them came down at the same time...i need it to only shoot one bullet but only have some enemies shoot at one time, not all of them shoot simeoutaneously...how do i fix that problem (do i use the get random method???)
Mutton Mutton

2014/4/5

#
you would want to use a counter. a full shoot method would look something like this: public int shotDelay = 0; public void shoot() { if(shotDelay >= 25) { getWorld().addObject(new bullet(90), getX(), getY()); shotDelay = 0; } shotDelay++; }
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
what do you mean i use a counter
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
Mutton wrote...
you would want to use a counter. a full shoot method would look something like this: public int shotDelay = 0; public void shoot() { if(shotDelay >= 25) { getWorld().addObject(new bullet(90), getX(), getY()); shotDelay = 0; } shotDelay++; }
what do you mean i use a counter
Mutton Mutton

2014/4/5

#
public int shotDelay = 0; public void shoot() { if(shotDelay >= 25) { getWorld().addObject(new bullet(90), getX(), getY()); shotDelay = 0; } shotDelay++; } So what this does is add a delay between the firing of bullets. Every time the method is called, it checks if the int shotDelay is greater than or equal to 25, if it is it spawns a bullet and resets the int. If not, it adds one to shotDelay. Does that clear things up at all?
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
Mutton wrote...
public int shotDelay = 0; public void shoot() { if(shotDelay >= 25) { getWorld().addObject(new bullet(90), getX(), getY()); shotDelay = 0; } shotDelay++; } So what this does is add a delay between the firing of bullets. Every time the method is called, it checks if the int shotDelay is greater than or equal to 25, if it is it spawns a bullet and resets the int. If not, it adds one to shotDelay. Does that clear things up at all?
so i basically put this in a counter class
Mutton Mutton

2014/4/5

#
no, it goes in the enemy class It just tells the enemy to wait a little between shooting bullets
GamesGrinder1998 GamesGrinder1998

2014/4/5

#
Mutton wrote...
no, it goes in the enemy class It just tells the enemy to wait a little between shooting bullets
sorry if i'm bothering you with these questions but i put it in the enemy class but a line of it is coming, if it helps this is what i got in one of my enemy classes: public int shotDelay = 0; /** * Act - do whatever the BugEnemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { getWorld().addObject(new DarkPower(90), getX(), getY()); shoot(); } public void shoot() { if(shotDelay >= 25) { getWorld().addObject(new DarkPower(90), getX(), getY()); shotDelay = 5; } shotDelay++; }
There are more replies on the next page.
1
2