sprite reset方法在更新线程中不起作用(andengine)

时间:2014-06-14 14:42:20

标签: android andengine sprite

我正在学习Android中的游戏开发,为此我使用的是andenigne box2d扩展。我创建了3个精灵阵列BarSet1,Barset2和Barset3每个尺寸8.我必须以圆形循环方式从右向左移动这些数组Barset3之后我又开始从Barset1开始移动了,我通过reset()方法重置了我的Barset1但是代码没有用。我花了差不多一个星期来处理这个小故障我不知道为什么它不起作用。这是我的代码:

GameActivity.java

package com.example.mygame;


import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.modifier.IModifier;
import org.andengine.util.modifier.LoopModifier;

import android.hardware.SensorManager;
import android.os.Looper;
import android.util.Log;
import android.view.Display;

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

public class GameActivity extends BaseGameActivity implements IAccelerationListener{

    Scene scene;
    Bars BarClassObject;
    protected static int CAMERA_HEIGHT;
    protected static int CAMERA_WIDTH;
    BitmapTextureAtlas  BallTexture;
    ITextureRegion BallTextureRegion;
    BitmapTextureAtlas BarsTexture;
    ITextureRegion[] BarRegion=new ITextureRegion[4];
    Sprite[] BarSet1=new Sprite[8];
    Sprite[] BarSet2=new Sprite[8];
    Sprite[] BarSet3=new Sprite[8];
    int[] BarToX=new int[8];
    int[] BarFromX=new int[8];
    int ENDPOINT,SPRITE_WIDTH;
    PhysicsWorld physicsworld;
    Sprite ball;
    Body body;
    int i,j;
    static boolean IfGameJustStarted;
    //Body RedBarBody;
    int SameBargGap,DifferentBarGap;
    final MoveXModifier[] barMove=new MoveXModifier[8];
    final MoveXModifier[] barMove2=new MoveXModifier[8];
    final MoveXModifier[] barMove3=new MoveXModifier[8];

    @SuppressWarnings("deprecation")
    @Override
    public EngineOptions onCreateEngineOptions() {
        final Display display=getWindowManager().getDefaultDisplay();
        CAMERA_HEIGHT=display.getHeight();
        CAMERA_WIDTH=display.getWidth();

        Camera gameCamera=new Camera(0, 0, CAMERA_WIDTH,CAMERA_HEIGHT);
        EngineOptions options=new EngineOptions(true,ScreenOrientation.PORTRAIT_FIXED,new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),gameCamera);
        return options;
    }

    @Override
    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback)throws Exception 
    {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        BallTexture=new BitmapTextureAtlas(this.getTextureManager(),64,64,TextureOptions.BILINEAR);
        BallTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BallTexture, getAssets(),"ball.png",0,0 );
        BarsTexture=new BitmapTextureAtlas(this.getTextureManager(), 64, 128);
        BarRegion[0]=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BarsTexture, getAssets(), "red.png",0,0);
        BarRegion[1]=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BarsTexture, getAssets(), "green.png",14,0);
        BarRegion[2]=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BarsTexture, getAssets(), "yellow.png",28,0);
        BarRegion[3]=BitmapTextureAtlasTextureRegionFactory.createFromAsset(BarsTexture, getAssets(), "violet.png",42,0);
        BallTexture.load();
        BarsTexture.load();
        ENDPOINT=CAMERA_WIDTH;
        SPRITE_WIDTH=(int)BarRegion[0].getWidth();
        SameBargGap=(int)((int)(BallTextureRegion.getWidth())*2);
        DifferentBarGap=10;
        IfGameJustStarted=true;
        BarClassObject=new Bars();
        BarSet1=BarClassObject.CreateBarSet1Sprite(BarRegion,
                   ENDPOINT,CAMERA_HEIGHT,
                   this.mEngine.getVertexBufferObjectManager(),
                   SameBargGap,
                   DifferentBarGap,
                   SPRITE_WIDTH);
        BarSet2=BarClassObject.CreateBarSet2Sprite(BarRegion,
                   ENDPOINT,CAMERA_HEIGHT,
                   this.mEngine.getVertexBufferObjectManager(),
                   SameBargGap,
                   DifferentBarGap,
                   SPRITE_WIDTH); 
        BarSet3=BarClassObject.CreateBarSet3Sprite(BarRegion,
                   ENDPOINT,CAMERA_HEIGHT,
                   this.mEngine.getVertexBufferObjectManager(),
                   SameBargGap,
                   DifferentBarGap,
                   SPRITE_WIDTH);

        BarToX=BarClassObject.getBarToX(ENDPOINT,SameBargGap,DifferentBarGap,SPRITE_WIDTH);
        BarFromX=BarClassObject.getBarFromX(ENDPOINT,SameBargGap,DifferentBarGap,SPRITE_WIDTH);
        for(i=0;i<=7;i++)
        {
            barMove[i]=new MoveXModifier(18.0f, BarFromX[i], (BarToX[i]+100),new IEntityModifierListener() {

                @Override
                public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
                    for(int j=0;j<=7;j++)
                    {
                        BarSet1[j].reset();
                    }
                    // TODO Auto-generated method stub
                }

            });
            barMove2[i]=new MoveXModifier(18.0f, BarFromX[i], BarToX[i]);

            barMove3[i]=new MoveXModifier(18.0f, BarFromX[i], BarToX[i]);
            //barMove[i].setAutoUnregisterWhenFinished(true);
            //barMove2[i].setAutoUnregisterWhenFinished(true);
            //barMove3[i].setAutoUnregisterWhenFinished(true);
        }
        for(i=0;i<=7;i++)
        {

            BarSet1[i].registerEntityModifier(barMove[i]);
            //BarSet2[i].registerEntityModifier(barMove[i]);
            //BarSet2[i].registerEntityModifier(barMove2[i]);
            //BarSet3[i].registerEntityModifier(barMove3[i]);
        }

        this.enableAccelerationSensor(this);
        pOnCreateResourcesCallback.onCreateResourcesFinished();
    }

    @Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)throws Exception 
    {
        scene=new Scene();  
        physicsworld=new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), true);    
        this.scene.setBackground(new Background(255, 23, 23));


        pOnCreateSceneCallback.onCreateSceneFinished(this.scene);

    }

    @Override
    public void onPopulateScene(Scene pScene,OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception
    {
        ball=new Sprite(CAMERA_WIDTH/2,10,BallTextureRegion,this.mEngine.getVertexBufferObjectManager());

        final FixtureDef BallFixture=PhysicsFactory.createFixtureDef(10.0f, 0.2f,0.0f);

        body=PhysicsFactory.createCircleBody(this.physicsworld,ball, BodyType.DynamicBody, BallFixture);
        this.scene.attachChild(ball);
        physicsworld.registerPhysicsConnector(new PhysicsConnector(ball, body, true, true));
        this.scene.registerUpdateHandler(this.physicsworld);





        this.scene.registerUpdateHandler(new IUpdateHandler()
{

    @Override
    public void onUpdate(float pSecondsElapsed) {


        // TODO Auto-generated method stub
        runOnUpdateThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if((int)BarSet1[7].getX()==ENDPOINT)
                {
                    if(IfGameJustStarted)
                    {
                        for(int x=0;x<=7;x++)
                        {


                            BarSet2[x].registerEntityModifier(barMove2[x]);
                            //scene.attachChild(BarSet2[x]);
                        }


                    }
                    else
                    {


                        for(int i=0;i<=7;i++)
                        {

                            BarSet2[i].reset();


                        }
                    }
                }
                else if((int)BarSet2[7].getX()==ENDPOINT)
                {
                    if(IfGameJustStarted)
                    {

                        for(int y=0;y<=7;y++)
                        {


                                BarSet3[y].registerEntityModifier(barMove3[y]);
                                //scene.attachChild(BarSet3[y]);


                        }
                    }
                        else
                        {

                            for(int i=0;i<=7;i++)
                            {
                                BarSet3[i].reset();

                            }
                        }

                }
                else if((int)BarSet3[7].getX()==ENDPOINT)
                {
                    if(IfGameJustStarted)
                        IfGameJustStarted=false;


                            for(int z=0;z<=7;z++)
                            {

                                BarSet1[z].reset();



                            }






                }
                else{}
                /*End ENDPOINT LOGIC*/

            }
        });
    }

    @Override
    public void reset() {
        // TODO Auto-generated method stub

    }});

        for(int i=0;i<=7;i++)
         {
             scene.attachChild(BarSet1[i]);scene.attachChild(BarSet2[i]);scene.attachChild(BarSet3[i]);


         }



        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }



    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub
        body.applyForce(new Vector2(pAccelerationData.getX()*70, 0),body.getWorldCenter());
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub


    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

    }
}

Bars.java

package com.example.mygame;

import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;

import android.content.Context;
import android.service.wallpaper.WallpaperService.Engine;

public class Bars {

    int RedBar,GreenBar,YellowBar,VoiletBar;

    Sprite[] barset1=new Sprite[8];
    Sprite[] barset2=new Sprite[8];
    Sprite[] barset3=new Sprite[8];
    int[] barToX=new int[8];
    int[] barFromX=new int[8];
    public Bars()
    {


    }

    public Sprite[] CreateBarSet1Sprite(ITextureRegion Bars[],int ENDPOINT,int CAMERA_HEIGHT,VertexBufferObjectManager vbo,int SameBarGap,int DifferentBarGap,int SPRITE_WIDTH)
    {

    barset1[0]=new Sprite(ENDPOINT+SPRITE_WIDTH+DifferentBarGap, CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[0],vbo);
    barset1[1]=new Sprite(((int)barset1[0].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[0],vbo);
    barset1[2]=new Sprite(((int)barset1[1].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[1],vbo);
    barset1[3]=new Sprite((int)(barset1[2].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[1],vbo);
    barset1[4]=new Sprite((int)(barset1[3].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[2],vbo);
    barset1[5]=new Sprite((int)(barset1[4].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[2],vbo);
    barset1[6]=new Sprite((int)(barset1[5].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[3],vbo);
    barset1[7]=new Sprite((int)(barset1[6].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[3],vbo);
    //Sprite[   
        return barset1;
    }
    public Sprite[] CreateBarSet2Sprite(ITextureRegion Bars[],int ENDPOINT,int CAMERA_HEIGHT,VertexBufferObjectManager vbo,int SameBarGap,int DifferentBarGap,int SPRITE_WIDTH)
    {

    barset2[0]=new Sprite(ENDPOINT+SPRITE_WIDTH+DifferentBarGap, CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[0],vbo);
    barset2[1]=new Sprite(((int)barset2[0].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[0],vbo);
    barset2[2]=new Sprite(((int)barset2[1].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[1],vbo);
    barset2[3]=new Sprite((int)(barset2[2].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[1],vbo);
    barset2[4]=new Sprite((int)(barset2[3].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[2],vbo);
    barset2[5]=new Sprite((int)(barset2[4].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[2],vbo);
    barset2[6]=new Sprite((int)(barset2[5].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[3],vbo);
    barset2[7]=new Sprite((int)(barset2[6].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[3],vbo);

    //Sprite[   
        return barset2;
    }
    public Sprite[] CreateBarSet3Sprite(ITextureRegion Bars[],int ENDPOINT,int CAMERA_HEIGHT,VertexBufferObjectManager vbo,int SameBarGap,int DifferentBarGap,int SPRITE_WIDTH)
    {

    barset3[0]=new Sprite(ENDPOINT+SPRITE_WIDTH+DifferentBarGap, CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[0],vbo);
    barset3[1]=new Sprite(((int)barset3[0].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[0],vbo);
    barset3[2]=new Sprite(((int)barset3[1].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[1],vbo);
    barset3[3]=new Sprite((int)(barset3[2].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[1],vbo);
    barset3[4]=new Sprite((int)(barset3[3].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[2],vbo);
    barset3[5]=new Sprite((int)(barset3[4].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[2],vbo);
    barset3[6]=new Sprite((int)(barset3[5].getX()+SPRITE_WIDTH+DifferentBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[3],vbo);
    barset3[7]=new Sprite((int)(barset3[6].getX()+SPRITE_WIDTH+SameBarGap), CAMERA_HEIGHT-((int)Bars[0].getHeight()), Bars[3],vbo);
    //Sprite[   
        return barset3;
    }
    public void resetBar(Sprite Barset[],int ENDPOINT,int SameBarGap,int DifferentBarGap,int SPRITE_WIDTH)
    {
        Barset[0].setX(ENDPOINT+SPRITE_WIDTH+DifferentBarGap);
        Barset[1].setX(Barset[0].getX()+SPRITE_WIDTH+SameBarGap);
        Barset[2].setX(Barset[1].getX()+SPRITE_WIDTH+DifferentBarGap);
        Barset[3].setX(Barset[2].getX()+SPRITE_WIDTH+SameBarGap);
        Barset[4].setX(Barset[3].getX()+SPRITE_WIDTH+DifferentBarGap);
        Barset[5].setX(Barset[4].getX()+SPRITE_WIDTH+SameBarGap);
        Barset[6].setX(Barset[5].getX()+SPRITE_WIDTH+DifferentBarGap);
        Barset[7].setX(Barset[6].getX()+SPRITE_WIDTH+DifferentBarGap);
    }
    public int[] getBarToX(int ENDPOINT,int SameBarGap,int DifferentBarGap,int SPRITE_WIDTH )
    {
        barToX[7]=0-SPRITE_WIDTH;
        barToX[6]=barToX[7]-SameBarGap-SPRITE_WIDTH;
        barToX[5]=barToX[6]-DifferentBarGap-SPRITE_WIDTH;
        barToX[4]=barToX[5]-SameBarGap-SPRITE_WIDTH;
        barToX[3]=barToX[4]-DifferentBarGap-SPRITE_WIDTH;
        barToX[2]=barToX[3]-SameBarGap-SPRITE_WIDTH;
        barToX[1]=barToX[2]-DifferentBarGap-SPRITE_WIDTH;
        barToX[0]=barToX[1]-SameBarGap-SPRITE_WIDTH;
        return barToX;

    }

    public int[] getBarFromX(int ENDPOINT,int SameBarGap,int DifferentBarGap,int SPRITE_WIDTH )
    {
        barFromX[0]=ENDPOINT+SPRITE_WIDTH+DifferentBarGap;
        barFromX[1]=barFromX[0]+SPRITE_WIDTH+SameBarGap;
        barFromX[2]=barFromX[1]+SPRITE_WIDTH+DifferentBarGap;
        barFromX[3]=barFromX[2]+SPRITE_WIDTH+SameBarGap;
        barFromX[4]=barFromX[3]+SPRITE_WIDTH+DifferentBarGap;
        barFromX[5]=barFromX[4]+SPRITE_WIDTH+SameBarGap;
        barFromX[6]=barFromX[5]+SPRITE_WIDTH+DifferentBarGap;
        barFromX[7]=barFromX[6]+SPRITE_WIDTH+SameBarGap;
        return barFromX;

    }

}

0 个答案:

没有答案