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!!!
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
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 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
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 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???)
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???)
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++;
}
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++;
}
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?
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?
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++;
}