0

我已经建立了一个单独的类来扩展 AnimatedSprite,称为“Enemy”。我在场景中创建了几个 Enemy 实例。每个都应该有自己的 TimerHandler,它控制动画。然而,这些计时器似乎都是链接在一起的,每个精灵同时更改其 TileIndex。这是代码:

package com.tahakki.folkprequel;

import java.util.Random;

import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class Enemy extends AnimatedSprite{

private Body physB;
private PhysicsWorld physW = MainActivity.mPhysicsWorld;
private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0, 0);
private TimerHandler timer;
private Random rand = new Random();

public Enemy(float pX, float pY, TiledTextureRegion pTiledTextureRegion) {
    super(pX, pY, pTiledTextureRegion);

    this.setCurrentTileIndex(rand.nextInt(3));
    this.setScale(0.6f);
    physB = PhysicsFactory.createBoxBody(physW, this, BodyType.DynamicBody, FIXTURE_DEF);
    physB.setFixedRotation(true);
    physW.registerPhysicsConnector(new PhysicsConnector(this, physB));

    timer = new TimerHandler(2 + (2*rand.nextFloat()), true, new ITimerCallback(){
        @Override
        public void onTimePassed(TimerHandler pTimerHandler) {
            int random = rand.nextInt(3);
            Enemy.this.setCurrentTileIndex(random);
        }
    });
    this.registerUpdateHandler(timer);
}

}

有谁知道如何让每个对象都有自己独立的计时器?谢谢!

4

1 回答 1

3

我猜你对每个敌人使用相同的 TextureRegion。如果您不复制它们,请尝试

Enemy e = new Enemy(x,y,yourTextureregion.deepCopy());
于 2012-05-23T10:44:57.827 回答