Android AndEngine 动画精灵动画不正确
我有一个名为播放器的动画精灵,我可以使用屏幕控件更改动画和方向。动画似乎卡住和/或重复第一帧,而没有经历完整的动画周期。我怀疑屏幕控制更新处理程序在有时间完成所有步骤之前正在重置动画?我不确定这里出了什么问题,这似乎应该有效:
final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.BoundChaseCamera,this.ScreenControlBaseTextureRegion,this.ScreenControlKnobTextureRegion, 0.1f,new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(
final BaseOnScreenControl pBaseOnScreenControl,
final float pValueX, final float pValueY) {
/* player position */
float spotX = player.getX() + 1 * TILE_WIDTH * pValueX;
float spotY = player.getY() + 1 * TILE_HEIGHT * pValueY;
/* if player position within bounds of map */
if (spotX < TMXMapLayer.getWidth()
&& spotY < TMXMapLayer.getHeight()
&& spotX >= 0 && spotY >= 0) {
/* Set player velocity */
final Vector2 velocity = Vector2Pool.obtain(pValueX * 3, pValueY * 3);
PlayerBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
/* Math to determine the direction of movement */
double dirDouble = (Math.atan2(pValueX, pValueY)
/ (Math.PI / 2) + 2);
float direction = (int) Math.round(dirDouble)
% DirectionState;
/* if movement is N,E,S or W, do animation */
if (direction == 0) { // If Go North
player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north
} else if (direction == 1) { // If Go West
player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west
} else if (direction == 2) { // If Go South
player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south
} else if (direction == 3) { // If Go East
player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east
}
}
}
I've got an animated sprite named player that I change the animation and direction of using an onscreencontrol. The animation seems to get stuck and/or repeat the first frame without ever going through a complete animation cycle. I suspect the onscreencontrol updatehandler is resetting the animation before it has time to go through all the steps? I'm not sure whats wrong here, this seems like it should work:
final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.BoundChaseCamera,this.ScreenControlBaseTextureRegion,this.ScreenControlKnobTextureRegion, 0.1f,new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(
final BaseOnScreenControl pBaseOnScreenControl,
final float pValueX, final float pValueY) {
/* player position */
float spotX = player.getX() + 1 * TILE_WIDTH * pValueX;
float spotY = player.getY() + 1 * TILE_HEIGHT * pValueY;
/* if player position within bounds of map */
if (spotX < TMXMapLayer.getWidth()
&& spotY < TMXMapLayer.getHeight()
&& spotX >= 0 && spotY >= 0) {
/* Set player velocity */
final Vector2 velocity = Vector2Pool.obtain(pValueX * 3, pValueY * 3);
PlayerBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
/* Math to determine the direction of movement */
double dirDouble = (Math.atan2(pValueX, pValueY)
/ (Math.PI / 2) + 2);
float direction = (int) Math.round(dirDouble)
% DirectionState;
/* if movement is N,E,S or W, do animation */
if (direction == 0) { // If Go North
player.animate(new long[] { 200, 200, 200 }, 0,2, true); // north
} else if (direction == 1) { // If Go West
player.animate(new long[] { 200, 200, 200 }, 9,11, true); // west
} else if (direction == 2) { // If Go South
player.animate(new long[] { 200, 200, 200 }, 6,8, true); // south
} else if (direction == 3) { // If Go East
player.animate(new long[] { 200, 200, 200 }, 3,5, true); // east
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用此代码在每次更新时重置动画,
您没有验证方向是否已更改,要纠正此问题,您应该添加一个外部 if 来检查方向是否已更改。您的代码应该像
变量lastDirection应该在您的onControlChange范围之外
You're resetting the animation on every update with this code
you're not verifying if the direction has changed, to correct this you should add an external if to check if the direction has changed. Your code should be like
the variable lastDirection should be external to you onControlChange scope