0

我正在尝试在我的游戏中添加粒子效果。运行时没有错误但显示 logcat。下面是我的代码和logcat。

@Override
public void onLoadResources() {

    //prepare a container for particle
    mParticleBitmapTextureAtlas = new BitmapTextureAtlas(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

    // setting assets path for easy access
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    mParticleTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mParticleBitmapTextureAtlas, this, "particle_fire.png", 0,0);

    // loading textures in the engine
    mEngine.getTextureManager().loadTexture(this.mParticleBitmapTextureAtlas);
 }

@Override
public Scene onLoadScene() {
    mEngine.registerUpdateHandler(new FPSLogger());
            mMainScene = new Scene();
    mMainScene.setBackground(new ColorBackground(0f, 0f, 0f));

    //particle system 

    /* Left to right Particle System. */
    {
        final ParticleSystem particleSystem = new ParticleSystem(new PointParticleEmitter(0, CAMERA_HEIGHT), 6, 10, 70, this.mParticleTextureRegion);
        particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);

        particleSystem.addParticleInitializer(new VelocityInitializer(15, 22, -60, -90));
        particleSystem.addParticleInitializer(new AccelerationInitializer(5, 15));
        particleSystem.addParticleInitializer(new RotationInitializer(0.0f, 360.0f));
        particleSystem.addParticleInitializer(new ColorInitializer(1.0f, 0.0f, 0.0f));

        particleSystem.addParticleModifier(new ScaleModifier(0.5f, 2.0f, 0, 5));
        particleSystem.addParticleModifier(new ExpireModifier(11.5f));
        particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 2.5f, 3.5f));
        particleSystem.addParticleModifier(new AlphaModifier(0.0f, 1.0f, 3.5f, 4.5f));
        particleSystem.addParticleModifier(new ColorModifier(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 11.5f));
        particleSystem.addParticleModifier(new AlphaModifier(1.0f, 0.0f, 4.5f, 11.5f));

        this.mMainScene.getLastChild().attachChild(particleSystem);
    }
          return mMainScene;
         }

//////////////////logcat////////////////////

            06-11 18:51:59.588: E/AndroidRuntime(1539): FATAL EXCEPTION: main
06-11 18:51:59.588: E/AndroidRuntime(1539): java.lang.NullPointerException
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.example.AndEngineTest.AndEngineTest.onLoadScene(AndEngineTest.java:251)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:169)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:85)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2366)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.view.View.dispatchWindowFocusChanged(View.java:5735)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:851)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2557)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.os.Looper.loop(Looper.java:137)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at java.lang.reflect.Method.invokeNative(Native Method)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at java.lang.reflect.Method.invoke(Method.java:511)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-11 18:51:59.588: E/AndroidRuntime(1539):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

尝试this.mMainScene.attachChild(particleSystem);不使用.getLastChild().

于 2012-06-11T16:55:07.663 回答
1

getLastChild()null给定时返回Entity(在这种情况下Scene)没有孩子。请参阅实体源代码

Scene不包含任何孩子,并且您想将粒子效果附加到最后一个孩子,因此NullPointerException. 不要将粒子系统附加到最后一个孩子,而是mMainScene直接将其附加到:

this.mMainScene.attachChild(particleSystem);
于 2012-06-11T16:51:04.443 回答
0

发生在第NullPointerException251 行,您所说的是:

this.mMainScene.getLastChild().attachChild(particleSystem);

您之前访问过mMainScene没有问题,因此看起来好像getLastChild()正在返回null.

于 2012-06-11T14:00:54.050 回答