敌人物体不动LibGDX
Enemy object doesn't move LibGDX
我有一个 .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;
}
}
}
}
但是当我 运行 这段代码时,只有我的一个敌人在移动(长方形的敌人)!我的代码有什么问题?
我知道怎么了。我只需要在 switch(t.hits(player1.getHitBox()))
之后删除 break
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
}
我有一个 .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;
}
}
}
}
但是当我 运行 这段代码时,只有我的一个敌人在移动(长方形的敌人)!我的代码有什么问题?
我知道怎么了。我只需要在 switch(t.hits(player1.getHitBox()))
之后删除 breakfor(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
}