【发布时间】:2013-09-24 03:28:17
【问题描述】:
返回游戏场景后,我的玩家的移动出现问题。
我可以通过控件来控制我的播放器。我已经使用屏幕控制和触摸区域精灵完成了它,但是当我返回我的游戏场景时两者都失败了。两个控件仍会获得输入,但不会再移动我的播放器。
这一切都在我扩展 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);
}
我知道当我返回我的场景时,我的控制器仍会收到输入,但角色不会移动。
任何帮助将不胜感激
【问题讨论】:
-
你必须对你的玩家身体施加线速度
-
@Siddharth 我将我的播放器(我的动画精灵)连接到我的 mPlayerBody(物理对象)。所以当我移动我的物理对象时,我的玩家也会随之移动。我这样做是为了解决我无法正常工作的 TMX 碰撞,所以我在 Collidable TMX Tiles 顶部的屏幕上添加了物理对象。所以行 mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);确实可以正确移动玩家,直到我更改场景并回到我的游戏场景
-
基本的经验法则是,如果您使用的是物理世界,那么您只能使用与物理相关的任务来移动对象,否则您在屏幕上看不到任何效果。这已经发生在你身上。
-
@Siddharth 感谢您的回复,但最初移动对象不是问题。当我开始我的游戏场景时,一切都正确加载,我的角色按预期移动,但是当我改变场景并回到我的游戏场景时,我无法再移动玩家。控制器仍然可以正常输入,但它不会再移动玩家。
-
抱歉最后两个 cmets,我没有看到你的速度声明。如果您使用的是物理世界,则不需要物理处理程序。此外,当您当时更改场景时,您必须在那时清除与物理相关的东西,以便下次您来创建所有新对象。
标签: controller box2d andengine game-physics