我有一个.txt文件
Block 128 0
Block 192 0
Block 256 0
Block 320 0
...
...
...
Enemy 1000 64 500 500
Enemy 1474 64 290 250
这是我的阅读方法(敌人和列表是全局变量)
ArrayList<GameObject> enemies = ArrayList<GameObject>();
ArrayList<GameObject> list= ArrayList<GameObject>();
FileHandle file = Gdx.files.internal("data/" + level + ".txt");
StringTokenizer tokens = new StringTokenizer(file.readString());
while(tokens.hasMoreTokens()){
if(type.equals("Block")){
list.add(new Brick(Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken())));
}else if(type.equals("Enemy")){
enemies.add(new Enemy(Float.parseFloat(tokens.nextToken()), Float.parseFloat(tokens.nextToken()), Integer.parseInt(tokens.nextToken()), Integer.parseInt(tokens.nextToken())));
}
}
这里我如何绘制我的敌人(用我的绘制方法)
for(GameObject t : enemies){
t.draw(batch);
}
在这里我设置我的敌人精灵移动方向
for(GameObject t : enemies){
int direction = 0;
for(GameObject t2 : list){
if(t2 instanceof Brick){
if(t.hits2(t2.getHitBox()) == 2){
direction = 2;
}else if(t.hits2(t2.getHitBox()) == 3){
direction = 1;
}
}
}
t.update(Gdx.graphics.getDeltaTime(), direction);
switch(t.hits(player1.getHitBox())){
case 4:
SoundManager.kill.play(.03f);
deleteEnemy.add(t);
break;
case 5:
player1.setPosition(0, 400);
dead++;
SoundManager.dead.play(0.06f);
gameState = 4;
break;
}
break;
}
这是我的更新方法
public class Enemy extends GameObject{
private Rectangle bottom, left, right, top, full;
...
...
...
...
public void update(float delta, int number){
moveEnemy(delta, number);
}
public void moveEnemy(float delta, int number){
if(sk1 > 0 && test && (number == 1 || number == 0)){
if(letRotate){
sprite.flip(true, false);
letRotate = false;
}
sk1 -= (speed * delta);
full.x -= (speed * delta);
top.x -= (speed * delta);
left.x -= (speed * delta);
right.x -= (speed * delta);
sprite.setPosition(full.x, full.y);
}else{
test = false;
if((full.x + x) < sk2 && !test && (number == 2 || number == 0)){
if(!letRotate){
sprite.flip(true, false);
letRotate = true;
}
sk1 += (speed * delta);
full.x += (speed * delta);
top.x += (speed * delta);
left.x += (speed * delta);
right.x += (speed * delta);
sprite.setPosition(full.x, full.y);
}else{
test = true;
}
}
}
}
但是,当我运行此代码时,我的一个敌人正在移动(带有矩形的敌人)!我的代码出了什么问题?
答案 0 :(得分:2)
我知道什么是错的。我只需要在切换后删除 break (t.hits(player1.getHitBox()))
for(GameObject t : enemies){
int direction = 0;
for(GameObject t2 : list){
if(t2 instanceof Brick){
if(t.hits2(t2.getHitBox()) == 2){
direction = 2;
}else if(t.hits2(t2.getHitBox()) == 3){
direction = 1;
}
}
}
t.update(Gdx.graphics.getDeltaTime(), direction);
switch(t.hits(player1.getHitBox())){
case 4:
SoundManager.kill.play(.03f);
deleteEnemy.add(t);
break;
case 5:
player1.setPosition(0, 400);
dead++;
SoundManager.dead.play(0.06f);
gameState = 4;
break;
}
//break; Remove this
}