Reputation: 5400
Animated sprite not performing animation with a physics handler:
I'm using AnalogOnScreenControl of the AndEngine GLES2 to move a sprite. The sprite is a person and so to show leg movement I've made it an animated sprite:
final AnimatedSprite person = new AnimatedSprite(personX, personY,
this.mPersonTextureRegion, vertexBufferObjectManager);
person.setScaleCenterY(this.mPersonTextureRegion.getHeight());
person.setScale(2);
For movement I'm creating a physics handler:
final PhysicsHandler physicsHandler = new PhysicsHandler(person);
person.registerUpdateHandler(physicsHandler);
scene.attachChild(person);
and this is the code of the screen control:
final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(
0, CAMERA_HEIGHT
- this.mOnScreenControlBaseTextureRegion.getHeight(),
this.mCamera, this.mOnScreenControlBaseTextureRegion,
this.mOnScreenControlKnobTextureRegion, 0.1f, 200,
this.getVertexBufferObjectManager(),
new IAnalogOnScreenControlListener() {
@Override
public void onControlChange(
final BaseOnScreenControl pBaseOnScreenControl,
final float pValueX, final float pValueY) {
physicsHandler
.setVelocity(pValueX * 100, pValueY * 100);
person.animate(new long[] { 200, 200, 200 }, 3, 5,
false);
}
The screen control works flawlessly for the animated sprite but when I create the physics handler it doesn't animate. But it animates when I don't create a physics handler. So why doesn't it animate when I create a physics handler?
Upvotes: 3
Views: 975
Reputation: 371
Your animation will only show the first tile, if you restart the animation all the time. Check if the animation is running with "isAnimationRunning()".
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY)
{
if(person!= null)
{
//Move your person
final Vector2 velocity = Vector2Pool.obtain(pValueX * 10, pValueY * 10);
person.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
//Check if person is moving. Don't start a new animation, when your animation is already running
if((pValueX != 0 || pValueY != 0) && person.isAnimationRunning() == false)
{
person.animate(new long[] { 200, 200, 200 },0,2,false);
}
//Stop animation, when there is no movement
else if(pValueX == 0 && pValueY == 0 && person.isAnimationRunning() == true)
{
person.stopAnimation();
}
}
}
Upvotes: 2
Reputation: 5400
Figured it out! Animation must started only if pValueX isn't 0, i.e., if sprite is moving.
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
if (pValueX > 0) {
//animate
}
else
//stop animation
physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
}
Upvotes: 1