4

我是新手AndEngine,正在尝试让精灵在触摸时增加尺寸。它有点工作,但有时会突然出现这个错误。有任何想法吗?在这一点上,我只需要足够的文字,所以 SO 会让我发布我的问题。如果有人需要更多信息,请告诉我,我会发布。我对想法非常开放。如果您有更好的方法或发现我的代码有问题,请告诉我。在这一点上,物理世界甚至不起作用。

06-17 22:44:25.809: E/AndroidRuntime(20526): FATAL EXCEPTION: UpdateThread
06-17 22:44:25.809: E/AndroidRuntime(20526): java.lang.NullPointerException
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.util.modifier.ModifierList.onUpdate(ModifierList.java:66)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1392)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.entity.Entity.onUpdate(Entity.java:1167)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.entity.Entity.onManagedUpdate(Entity.java:1402)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:284)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.entity.Entity.onUpdate(Entity.java:1167)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.engine.Engine.onUpdateScene(Engine.java:591)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.engine.Engine.onUpdate(Engine.java:586)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.engine.Engine.onTickUpdate(Engine.java:548)
06-17 22:44:25.809: E/AndroidRuntime(20526):    at org.andengine.engine.Engine$UpdateThread.run(Engine.java:820)

这是我的代码:

public class TestGFX2Activity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener {
/** Called when the activity is first created. */

    private static final int CAMERA_WIDTH = 720;
    private static final int CAMERA_HEIGHT = 480;
    private final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private TextureRegion circleTR;
    private Scene scene;
    boolean down=false;
    float scale;
    final float growRate = (float) .0001;

    private PhysicsWorld mPhysicsWorld;

    @Override
    public EngineOptions onCreateEngineOptions() {
        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    @Override
    public void onCreateResources() {

    /*Line 66*/ this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 128, TextureOptions.BILINEAR); //Line 66
        this.circleTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "circle.png", 0, 0); // 64x32

        this.mBitmapTextureAtlas.load();
    }

    @Override
    public Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());

        this.scene = new Scene();
        this.scene.setBackground(new Background(0, 1, 0));
        Log.e("Tag","Got to background");
        this.scene.setOnSceneTouchListener(this);

        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

        final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
        final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
        final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
        final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
        final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);

        final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

        this.scene.attachChild(ground);
        this.scene.attachChild(roof);
        this.scene.attachChild(left);
        this.scene.attachChild(right);

        this.scene.registerUpdateHandler(this.mPhysicsWorld);

        return this.scene;
    }

    @Override
    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
        Sprite face;
        Body body;
        scale = 1;

        if(this.mPhysicsWorld != null) {
            if(pSceneTouchEvent.isActionDown()) {
                //this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
                down = true;
                face = new Sprite(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), circleTR, this.getVertexBufferObjectManager());
                body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

                new Thread(new Grow(face)).start();

                return true;
            } else if(pSceneTouchEvent.isActionUp()){
                down = false;
            }
        }
        return false;
    }

    @Override
    public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {

    }

    @Override
    public void onAccelerationChanged(final AccelerationData pAccelerationData) {
        final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
        this.mPhysicsWorld.setGravity(gravity);
        Vector2Pool.recycle(gravity);
    }

    @Override
    public void onResumeGame() {
        super.onResumeGame();

        this.enableAccelerationSensor(this);
    }

    @Override
    public void onPauseGame() {
        super.onPauseGame();

        this.disableAccelerationSensor();
    }

    private class Grow implements Runnable{
        Sprite s;
        ScaleModifier mod;

        public Grow(Sprite sprite){
            s = sprite;
            scene.attachChild(s);
        }
        public void run(){
            while(down){
                //Log.e("Tag",Float.toString(scale));
                s.clearEntityModifiers();
                mod = new ScaleModifier((float) .1,scale,scale+growRate);
                scale+=growRate;
                s.registerEntityModifier(mod);
                /*s.detachSelf();
                s.setScale(scale);
                scale += growRate;
                try{
                    Thread.sleep(100);
                }catch(InterruptedException e){}
                scene.attachChild(s);*/
            }
        }
    }
}
4

2 回答 2

2

而不是在单独的线程中调用它,您可能应该调用它

new Grow(face)).start();

在 runOnUpdate() 块中,如

myActivity.runonUpdateThread(new Grow(face)));
于 2012-06-19T16:44:01.787 回答
0

just replace

new Thread(new Grow(face)).start();

with

TestGFX2Activity.runonUpdateThread(new Grow(face)));
于 2012-11-03T20:13:37.810 回答