返回游戏场景后,我的播放器移动出现问题。
我可以通过控件来控制我的播放器。我已经使用屏幕控制和触摸区域精灵完成了它,但是当我返回我的游戏场景时两者都失败了。两个控件仍然会获得输入,但不会再移动我的播放器。
这一切都在我扩展 BaseScene 的 GameScene 中完成
以下是我设置动画精灵和物理世界的方法:
//////////////////////////////////////////////////////////////////////
//Creates and Registers Physics World
//////////////////////////////////////////////////////////////////////
this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
this.registerUpdateHandler(this.mPhysicsWorld);
//////////////////////////////////////////////////////////////////////
//Creates and Adds the Animated Sprite, Sets Camera Chase Entity
//////////////////////////////////////////////////////////////////////
player = new AnimatedSprite(18 * 32, 43 * 32, ResourceManager.getInstance().mPlayerTextureRegion, ((DragonsReignActivity)activity).getVertexBufferObjectManager());
camera.setChaseEntity(player);
final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, playerFixtureDef);
//////////////////////////////////////////////////////////////////////
//Register Physics Connector
//////////////////////////////////////////////////////////////////////
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false)
{
//////////////////////////////////////////////////////////////////////
//Updates Chase Entity
//////////////////////////////////////////////////////////////////////
@Override
public void onUpdate(float pSecondsElapsed)
{
super.onUpdate(pSecondsElapsed);
camera.updateChaseEntity();
}
});
physicsHandler = new PhysicsHandler(player);
player.registerUpdateHandler(physicsHandler);
attachChild(player);
player.setZIndex(10);
以下是我当前设置控件的方式:
public void attachControls()
{
mDigitalOnScreenControl = new DigitalOnScreenControl(0, ((DragonsReignActivity)activity).CAMERA_HEIGHT - ResourceManager.getInstance().DPADBacking.getHeight(), this.camera, ResourceManager.getInstance().DPADBacking, ResourceManager.getInstance().DPADKnob, 0.1f, ((DragonsReignActivity)activity).getVertexBufferObjectManager(), new IOnScreenControlListener()
{
@Override
public void onControlChange(BaseOnScreenControl pBaseOnScreenControl, float pValueX, float pValueY)
{
if (pValueY == 1){
// Up
if (playerDirection != PlayerDirection.UP)
{
player.animate(new long[]{200, 200, 200}, 0, 2, true);
playerDirection = PlayerDirection.UP;
}
}else if (pValueY == -1){
// Down
if (playerDirection != PlayerDirection.DOWN)
{
player.animate(new long[]{200, 200, 200}, 3, 5, true);
playerDirection = PlayerDirection.DOWN;
}
}else if (pValueX == -1)
{
// Left
if (playerDirection != PlayerDirection.LEFT)
{
player.animate(new long[]{200, 200, 200}, 6, 8, true);
playerDirection = PlayerDirection.LEFT;
}
}else if (pValueX == 1)
{
// Right
if (playerDirection != PlayerDirection.RIGHT)
{
player.animate(new long[]{200, 200, 200}, 9, 11, true);
playerDirection = PlayerDirection.RIGHT;
}
}else{
if (player.isAnimationRunning())
{
player.stopAnimation();
playerDirection = PlayerDirection.NONE;
}
}
if(player.getX() >= 576 && player.getX() <= 736 && player.getY() <= 672 && player.getY() >= 576)
{
PLAYER_VELOCITY = 4;
Log.e("Collision Box", "You are in the Box. Player Velcocity = " + PLAYER_VELOCITY);
}
mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
}
});
this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
this.mDigitalOnScreenControl.refreshControlKnobPosition();
}
最后,这就是我回到游戏场景的方式,这一切都在:
@Override
public void onBackKeyPressed()
{
SceneManager.getInstance().setScene(SceneManager.SceneType.SCENE_GAME);
camera.setChaseEntity(GameScene.player);
}
我知道当我返回我的场景时,我的控制器仍然会收到输入,但角色不会移动。
任何帮助将不胜感激