Android的AndEngine动画精灵不能正确动画 [英] Android AndEngine animated sprite not animating correctly
问题描述
我有一个动画精灵命名的球员,我改变了动画和使用onscreencontrol方向。动画似乎被卡住和/或重复的第一帧没有经过一个完整的动画周期曾经打算。我怀疑onscreencontrol updatehandler正在重置动画有时间去完成所有步骤之前?我不知道什么是错在这里,这似乎像它应该工作:
最后AnalogOnScreenControl velocityOnScreenControl =新AnalogOnScreenControl(X1,Y1,this.BoundChaseCamera,this.ScreenControlBaseTextureRegion,this.ScreenControlKnobTextureRegion,0.1F,新IAnalogOnScreenControlListener(){
@覆盖
公共无效onControlChange(
最后BaseOnScreenControl pBaseOnScreenControl,
最终浮动pValueX,最终浮动pValueY){
/ *球员的位置* /
浮spotX = player.getX()+ 1 * TILE_WIDTH * pValueX;
浮spotY = player.getY()+ 1 * TILE_HEIGHT * pValueY;
/ *如果地图的范围之内球员的位置* /
如果(spotX< TMXMapLayer.getWidth()
&功放;&安培; spotY< TMXMapLayer.getHeight()
&功放;&安培; spotX> = 0&放大器;&放大器; spotY> = 0){
/ *设置播放速度* /
最终Vector2速度= Vector2Pool.obtain(pValueX * 3,pValueY * 3);
PlayerBody.setLinearVelocity(速度);
Vector2Pool.recycle(速度);
/ *数学确定移动的方向* /
双dirDouble =(Math.atan2(pValueX,pValueY)
/(Math.PI / 2)+ 2);
浮动方向=(INT)Math.round(dirDouble)
%DirectionState;
/ *如果运动是N,E,S或W,做动画* /
如果(方向== 0){//如果往北走
player.animate(新长[] {200,200,200},0,2,TRUE); // 北 }否则如果(方向== 1){//如果去西部
player.animate(新长[] {200,200,200},9,11,真正的); //西 }否则如果(方向== 2){//如果南归
player.animate(新长[] {200,200,200},6,8,TRUE); // 南 }否则如果(方向== 3){//如果正东
player.animate(新长[] {200,200,200},3,5,TRUE); // 东
}
}
}
您正在重新与此code每次更新动画
如果(方向== 0){//如果往北走
player.animate(新长[] {200,200,200},0,2,TRUE); // 北}否则如果(方向== 1){//如果去西部
player.animate(新长[] {200,200,200},9,11,真正的); //西}否则如果(方向== 2){//如果南归
player.animate(新长[] {200,200,200},6,8,TRUE); // 南}否则如果(方向== 3){//如果正东
player.animate(新长[] {200,200,200},3,5,TRUE); // 东
}
你不验证,如果方向发生了变化,以纠正这个你应该添加一个外部,如果检查的方向发生了变化。您code应该是这样
如果(方向!= lastDirection){
lastDirection =方向;
如果(方向== 0){//如果往北走
player.animate(新长[] {200,200,200},0,2,TRUE); // 北 }否则如果(方向== 1){//如果去西部
player.animate(新长[] {200,200,200},9,11,真正的); //西 }否则如果(方向== 2){//如果南归
player.animate(新长[] {200,200,200},6,8,TRUE); // 南 }否则如果(方向== 3){//如果正东
player.animate(新长[] {200,200,200},3,5,TRUE); // 东
}
}
你onControlChange范围变量lastDirection应该是外部
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
}
}
}
You're resetting the animation on every update with this code
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
}
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
if(direction != lastDirection){
lastDirection = direction;
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
}
}
the variable lastDirection should be external to you onControlChange scope
这篇关于Android的AndEngine动画精灵不能正确动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!