如何在Andengine中添加OnClickListener?

时间:2014-02-02 12:26:21

标签: android andengine

任何人都可以帮助我吗?我正在尝试实现一个OnClickListener,让我的代码转到主游戏。我不知道如何在AndEngine中对clicklistener进行编码。

我被困在我只能输出图像的部分,而不是可点击的图像。

更新: 即使在向其添加此代码后,播放图像/按钮似乎也不起作用。有人可以帮忙吗?

为GameActivity添加了代码:

public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {
         if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
        // you can implement your action
             sceneManager.loadGameResources();
                sceneManager.createGameScene();
          sceneManager.setCurrentScene(AllScenes.GAME);
}

     return onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);

 }

为SceneManager添加了代码:

public Scene createMenuScene()
{
    menuScene = new Scene();
    menuScene.setBackground(new Background (0,0,0));

    Sprite icon = new Sprite (0,0, menuTR, engine.getVertexBufferObjectManager());
    icon.setPosition((camera.getWidth() - icon.getWidth()) /2, (camera.getHeight() - icon.getHeight()) /2 );
    menuScene.attachChild(icon);


    Sprite play = new Sprite (180, 350, playTR, engine.getVertexBufferObjectManager());

    this.menuScene.attachChild(play);
    this.menuScene.registerTouchArea(play);

    Sprite quit = new Sprite (500,350, quitTR, engine.getVertexBufferObjectManager());

    menuScene.attachChild(quit);
    return menuScene;
}
public Scene createGameScene()
{

return gameScene;   
}

以下是我的GameActivity的代码:

public class GameActivity extends BaseGameActivity {

Scene scene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
PhysicsWorld physicsWorld;


SceneManager sceneManager;
Camera mCamera;

@Override
public EngineOptions onCreateEngineOptions() {

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

@Override
public void onCreateResources(
        OnCreateResourcesCallback pOnCreateResourcesCallback)
        throws Exception {


    sceneManager = new SceneManager(this, mEngine, mCamera);
    sceneManager.loadSplashResources();

    pOnCreateResourcesCallback.onCreateResourcesFinished();
}


@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
        throws Exception {

    pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());

}

@Override
public void onPopulateScene(Scene pScene,
        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {

    mEngine.registerUpdateHandler(new TimerHandler(3f, new ITimerCallback(){


        public void onTimePassed(TimerHandler pTimerHandler)
        {
            mEngine.unregisterUpdateHandler(pTimerHandler);
            // TODO Auto-generated method stub

            sceneManager.loadMenuResources();
            sceneManager.createMenuScene();
            sceneManager.setCurrentScene(AllScenes.MENU);
        }
    }));
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}



}

这是我的SceneManager代码:

public class SceneManager {
private AllScenes currentScene;
private BaseGameActivity activity; // Name of other activity GameActivity
private Engine engine;
private Camera camera;
private BitmapTextureAtlas splashTA, menuTA, playTA, quitTA;
private ITextureRegion splashTR, menuTR, playTR , quitTR;
private Scene splashScene, gameScene, menuScene;

public enum AllScenes 
{
    SPLASH, MENU, GAME
}

public SceneManager(BaseGameActivity act, Engine eng, Camera cam)
{
// TODO Auto-generated constructor stub
    this.activity = act;
    this.engine = eng;
    this.camera = cam;
} 
public AllScenes getCurrentScene()
{
    return currentScene;
}

public void setCurrentScene(AllScenes currentScene)
{
    this.currentScene = currentScene;


    switch(currentScene)
    {
    case SPLASH:

        break;
    case MENU:
        engine.setScene(menuScene);

        break;
    case GAME:

        engine.setScene(gameScene);
        break;

    default:
        break;
    }
}

public void loadSplashResources() 
{
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    splashTA = new BitmapTextureAtlas(this.activity.getTextureManager(), 256, 256);
    splashTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTA, this.activity, "splash.png", 0,0);
    splashTA.load();
}

public void loadGameResources()
{

}

public void loadMenuResources()
{
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    menuTA = new BitmapTextureAtlas(this.activity.getTextureManager(), 800, 480);
    menuTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTA, this.activity, "menu.png", 0, 0);
    menuTA.load();

    playTA = new BitmapTextureAtlas(this.activity.getTextureManager(), 140, 42);
    playTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playTA, this.activity, "play.png", 0, 0);
    playTA.load();

    quitTA = new BitmapTextureAtlas(this.activity.getTextureManager(), 140, 42);
    quitTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(quitTA, this.activity, "quit.png", 0, 0);
    quitTA.load();
}

public Scene createSplashScene()
{
    splashScene = new Scene();
    splashScene.setBackground(new Background(233,242,237));

    Sprite icon = new Sprite(0,0, splashTR, engine.getVertexBufferObjectManager());
    icon.setPosition((camera.getWidth()-icon.getWidth())/2, (camera.getHeight() - icon.getHeight()) /2 );
    splashScene.attachChild(icon);
    return splashScene;
}

public Scene createMenuScene()
{
    menuScene = new Scene();
    menuScene.setBackground(new Background (0,0,0));

    Sprite icon = new Sprite (0,0, menuTR, engine.getVertexBufferObjectManager());
    icon.setPosition((camera.getWidth() - icon.getWidth()) /2, (camera.getHeight() - icon.getHeight()) /2 );
    menuScene.attachChild(icon);


    Sprite play = new Sprite (0,0, playTR, engine.getVertexBufferObjectManager());
    play.setPosition(180, 350 );
    menuScene.attachChild(play);

    Sprite quit = new Sprite (0,0, quitTR, engine.getVertexBufferObjectManager());
    quit.setPosition(500, 350);
    menuScene.attachChild(quit);
    return menuScene;
}


}

4 个答案:

答案 0 :(得分:1)

我没有得到你想要的东西。但是你可以按如下方式实现onclick for sprite:

ButtonSprite mSoundButton  = new ButtonSprite(30, 30,
                                         ResourceManager.getSoundButtonTR(),
                                         ResourceManager.getEngine().getVertexBufferObjectManager(),
                                         new ButtonSprite.OnClickListener() {
                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {


                    }
        });

答案 1 :(得分:1)

如果您想将Click监听器添加到Scene it self,您可以执行以下操作:

public class MenuScrollerActivity extends SimpleBaseGameActivity implements  IClickDetectorListener{
  private ClickDetector mClickDetector;

MenuScrollerActivity(){
this.mClickDetector = new ClickDetector(this);
}

@Override
    public void onClick(ClickDetector arg0, int arg1, float arg2, float arg3) {
        // TODO Auto-generated method stub

    }

 @Override
        public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
                this.mClickDetector.onTouchEvent(pSceneTouchEvent);

                return true;
        }

} 

答案 2 :(得分:1)

您也可以通过这种方式实现这些功能:

      Sprite playSprite = new Sprite(0, CAMERA_HEIGHT/2, mFruitTiledTextureRegion3, this.getVertexBufferObjectManager()){
                    @Override
                    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                            float pTouchAreaLocalX, float pTouchAreaLocalY) {
                            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
                           // you can implement your action
                                      sceneManager.setCurrentScene(AllScenes.GAME);
}

                        return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
                    }
                };

                this.mScene.registerTouchArea(playSprite);
                this.mScene.attachChild(playSprite);

答案 3 :(得分:1)

public Scene createMenuScene()
{
    menuScene = new Scene();
    menuScene.setBackground(new Background (0,0,0));

    Sprite icon = new Sprite (0,0, menuTR, engine.getVertexBufferObjectManager());
    icon.setPosition((camera.getWidth() - icon.getWidth()) /2, (camera.getHeight() - icon.getHeight()) /2 );
    menuScene.attachChild(icon);


  Sprite playSprite = new Sprite(0, 0, playTR, this.getVertexBufferObjectManager()){
                    @Override
                    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                            float pTouchAreaLocalX, float pTouchAreaLocalY) {
                            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
                           // you can implement your action
                                      sceneManager.setCurrentScene(AllScenes.GAME);
}

                        return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
                    }
                };

                menuScene.registerTouchArea(playSprite);
              play.setPosition(180, 350 );
               menuScene.attachChild(playSprite);




  Sprite quitSprite = new Sprite(0, 0, quitTR, this.getVertexBufferObjectManager()){
                    @Override
                    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                            float pTouchAreaLocalX, float pTouchAreaLocalY) {
                            if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){
                           // you can implement your action to quit

}

                        return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX, pTouchAreaLocalY);
                    }
                };
                 quitSprite.setPosition(500, 350);
                menuScene.registerTouchArea(quitSprite);
               menuScene.attachChild(quitSprite);
}