在《libgdx》中,我该如何创造一个在一定时间后向玩家射击的敌人?



我目前正致力于让敌人以直线向玩家发射炮弹。弹丸未显示在播放状态

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;
}

到目前为止,我还没有创造出能够在游戏x轴上移动的刷出子弹。有什么建议吗?

你正在使用计时器来确定子弹何时被射击,即射击开始,但也有持续的增量为子弹的飞行。所以当你的计时器从delta触发时,子弹会将位置重置为poststopghost。

。子弹在飞行过程中没有任何前进的方法。不妨试试这个。此外,您需要参考dt(以您喜欢的某种方式)来对抗40增量,因为您不知道自上次渲染以来已经过了多少时间。

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));    
}

最新更新