如何解决粒子效应中的logcat

时间:2012-06-11 13:19:24

标签: android andengine particle-system

我正在尝试在游戏中添加粒子效果。没有错误,但在运行时显示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)

3 个答案:

答案 0 :(得分:1)

在给定getLastChild()(在这种情况下null)没有孩子时,

Entity会返回Scene。请参阅Entity source code

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

this.mMainScene.attachChild(particleSystem);

答案 1 :(得分:1)

在没有this.mMainScene.attachChild(particleSystem);的情况下试试.getLastChild()

答案 2 :(得分:0)

{251}正在第251行发生,您已经说过:

NullPointerException

您之前访问this.mMainScene.getLastChild().attachChild(particleSystem); 时没有遇到任何问题,因此看起来好像mMainScene正在返回getLastChild()