I am currently working on trying to make enemies shoot a projectile in a straight line at the player. The projectiles are not showing on the playstate.
public class Ghost {
private Texture topGhost, bottomGhost;
private Vector2 postopGhost;
private Vector2 posBotGhost;
private Random rand;
private static final int fluct = 130;
private int GhostGap;
public int lowopening;
public static int width;
private Texture bullet;
private Vector2 bulletpos;
private Vector2 botbulletpos;
public Ghost(float x) {
GhostGap = 120; // the gap between the top and bottom ghost
lowopening = 90; //
bullet = new Texture("Bird.png");
topGhost = new Texture("Bird.png");
// middletude = new Texture("sipkemiddle.png"); //spelling mistake
bottomGhost = new Texture("Bird.png");
rand = new Random();
width = topGhost.getWidth();
posBotGhost = new Vector2(x + 120, rand.nextInt(fluct));
postopGhost = new Vector2(x + 113, posBotGhost.y + bottomGhost.getHeight() + GhostGap - 50);
bulletpos = new Vector2(postopGhost);
botbulletpos = new Vector2(posBotGhost);
}
public void repostition(float x) {
postopGhost.set(x + 75, rand.nextInt(fluct) + 200);
posBotGhost.set(x + 75, postopGhost.y + GhostGap - bottomGhost.getHeight() - 247);
}
public void timer(float dt) {
int ticker = 0;
ticker += dt;
if(ticker > 5) {
ticker = 0;
shoot();
}
}
public void shoot(){
setBulletpos(postopGhost);
bulletpos.x = (bulletpos.x + 40);
bulletpos. y = bulletpos.y;
}
So far I had no luck with spawning bullets visually that move across the X-axis of my game. Any suggestions?
You are using timer to determine when the bullet is shot i.e the shot begins, but also in there is the continuing increment for the flight of the bullet. So whenever your timer triggers from delta, the bullet resets position to postopGhost.
i.e. the bullet doesn't have any method to proceed during flight. Try this maybe. Also you need to refer to dt (in some fashion as you like) against the 40 increment because you don't know how much time has elapsed since the last render.
public void timer(float dt) {
int ticker = 0;
ticker += dt;
if(ticker > 5) {
ticker = 0;
shoot();
} else {
processBulletFlight(dt);
}
}
public void shoot(){
setBulletpos(postopGhost);
}
public processBulletFlight(dt) {
bulletpos.x = (bulletpos.x + (40*dt));
}