我对Android游戏开发完全陌生,并尝试使用AndEngine开发游戏。 我认为第一步很顺利,但现在我有点陷入困境。 最后的游戏应该像空气控制(https://play.google.com/store/apps/details?id=dk.logisoft.aircontrol)(除了我不是使用飞机,而是我的太空中的侄子;)
createPlayer()方法应该做什么:它应该在随机的屏幕外位置创建一个玩家,获得一个随机的屏幕外目标位置并绘制两点之间的路径。现在玩家应该使用PathModifier飞行。在播放器在屏幕上可见之前,应该有一个小箭头作为指示器,显示下一个玩家的来源。当播放器出现在屏幕上时,这个小指示器应该消失。
我正在使用一个简单的计时器产生当前的玩家。当我将定时器的间隔设置为10秒时,一切都按照我想要的方式工作。 当我将计时器设置为较小的时候,例如1秒,问题是指示器没有被移除。它只是停留在最后位置。
我认为这是因为已经有一个新玩家产生,而该玩家或者玩家指示器指向新创建的玩家。我不知道如何改变它。
我的错误在哪里?
这是我的createPlayer方法和播放器类:
createPlayer()方法
private void createPlayer() {
float[] startPos = getPlayerRandomStartPosition();
float[] targetPos = getPlayerRandomTargetPosition(startPos);
int playerNr = 0 + (int)(Math.random() * ((2 - 0) + 1));
int[] playerSpeed = { 25, 35, 45 };
float[] playerScale = { 0.3f, 0.25f, 0.2f };
String[] playerName = { "marvin", "noah", "liam" };
ITiledTextureRegion[] tex = { resourcesManager.p1_region, resourcesManager.p2_region, resourcesManager.p3_region };
float pointsListX[] = { startPos[0], targetPos[0] };
float pointsListY[] = { startPos[1], targetPos[1] };
Path path = new Path(pointsListX.length);
for (int j = 0; j < pointsListX.length; j++) {
path.to(pointsListX[j], pointsListY[j]);
}
player = new Player(startPos[0], startPos[1], tex[playerNr], vbom) {};
player.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() {}
@Override
public void onUpdate(float pSecondsElapsed) {
if (player.getPlayerIndicator() != null) {
if (player.getStartPos()[1] <= 0) {
player.getPlayerIndicator().setPosition(player.getX(), 0 + (resourcesManager.indicator_region.getHeight() / 2));
} else if (player.getStartPos()[1] >= 800) {
player.getPlayerIndicator().setPosition(player.getX(), 800 - (resourcesManager.indicator_region.getHeight() / 2));
} else if (player.getStartPos()[0] <= 0) {
player.getPlayerIndicator().setPosition(0 + (resourcesManager.indicator_region.getHeight() / 2), player.getY());
} else {
player.getPlayerIndicator().setPosition(480 - (resourcesManager.indicator_region.getHeight() / 2), player.getY());
}
}
if (camera.isEntityVisible(player)) {
if (player.getPlayerIndicator() != null) {
engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
final EngineLock engineLock = engine.getEngineLock();
engineLock.lock();
player.getPlayerIndicator().detachSelf();
player.getPlayerIndicator().dispose();
player.setPlayerIndicator(null);
engineLock.unlock();
}
});
}
}
}
});
player.registerEntityModifier(new PathModifier(playerSpeed[playerNr], path) {
@Override
protected void onModifierFinished(final IEntity pItem) {
engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
final EngineLock engineLock = engine.getEngineLock();
engineLock.lock();
pItem.detachSelf();
pItem.dispose();
engineLock.unlock();
addToScore();
}
});
super.onModifierFinished(pItem);
}
});
player.setHasBeenOnScreen(false);
player.setTargetPos(targetPos);
player.setStartPos(startPos);
player.setScale(playerScale[playerNr]);
player.setSpeed(playerSpeed[playerNr]);
player.setUserData(playerName[playerNr]);
player.setRotation(getTargetAngle(targetPos[0], targetPos[1], player.getX(), player.getY()));
PlayerIndicator playerIndicator;
if (startPos[1] <= 0) {
playerIndicator = new PlayerIndicator(startPos[0], 0 + (resourcesManager.indicator_region.getHeight() / 2), resourcesManager.indicator_region, vbom);
playerIndicator.setRotation(180);
} else if (startPos[1] >= 800) {
playerIndicator = new PlayerIndicator(startPos[0], 800 - (resourcesManager.indicator_region.getHeight() / 2), resourcesManager.indicator_region, vbom);
} else if (startPos[0] <= 0) {
playerIndicator = new PlayerIndicator(0 + (resourcesManager.indicator_region.getHeight() / 2), startPos[1], resourcesManager.indicator_region, vbom);
playerIndicator.setRotation(270);
} else {
playerIndicator = new PlayerIndicator(480 - (resourcesManager.indicator_region.getHeight() / 2), startPos[1], resourcesManager.indicator_region, vbom);
playerIndicator.setRotation(90);
}
playerIndicator.setAlpha(0.5f);
player.setPlayerIndicator(playerIndicator);
attachChild(playerIndicator);
attachChild(player);
}
玩家类
public class Player extends AnimatedSprite {
public Player(float pX, float pY, ITiledTextureRegion pTiledTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
long[] frameDurration = {300, 300, 300};
animate(frameDurration);
hasBeenOnScreen = false;
}
float[] startPos = { -100, -100 };
float[] targetPos;
int speed = 10;
PlayerIndicator playerIndicator;
boolean hasBeenOnScreen = false;
public PlayerIndicator getPlayerIndicator() {
return playerIndicator;
}
public void setPlayerIndicator(PlayerIndicator playerIndicator) {
this.playerIndicator = playerIndicator;
}
public boolean hasBeenOnScreen() {
return hasBeenOnScreen;
}
public void setHasBeenOnScreen(boolean hasBeenOnScreen) {
this.hasBeenOnScreen = hasBeenOnScreen;
}
public float[] getStartPos() {
return startPos;
}
public void setStartPos(float[] startPos) {
this.startPos = startPos;
}
public float[] getTargetPos() {
return targetPos;
}
public void setTargetPos(float[] targetPos) {
this.targetPos = targetPos;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}