Andengine (gles2) - ButtonSprite don't react on click

与世无争的帅哥 提交于 2019-12-08 05:22:46

问题


I have code like this:

mButton = new ButtonSprite(400  , 400, myTiledTextureRegion.getTextureRegion(0), myTiledTextureRegion.getTextureRegion(1), 
            activity.getVertexBufferObjectManager(), new OnClickListener() {

                @Override
                public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,
                        float pTouchAreaLocalY) {

                                 Toast.makeText(MenuScene.this.activity, "Clicked", Toast.LENGTH_LONG).show();


                }
            });

    this.attachChild(mButton);

My problem is that when I Click on my button i don' t get toast message (log the same) and aslo sprite doesn't change for myTiledTextureRegion.getTextureRegion(1). I don' t know why?

I can click on button, but there is no reaction. I tried use AnimatedSprite to test resources and it display animation correctly. Why my ButtonSprite OnClickListener and sprite change there don' work ?

Way i load resources:

BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    mBitmapTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 512, 256, TextureOptions.NEAREST);
    myTiledTextureRegion =  BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, activity, "btn.png", 1, 3);

    try {
        mBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
        mBitmapTextureAtlas.load();
    } catch (TextureAtlasBuilderException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I do everything on my Scene class, because i have only one activity (and many scenes). Thanks for help.


回答1:


see if this is the issue ... before you attach the button with this.attachChild(mButton); be sure and also register the touch area with this.registerTouchArea(mButton);




回答2:


Try, after adding the sprite into the scene, to use : scene.setTouchAreaBindingOnActionDownEnabled(true);



来源:https://stackoverflow.com/questions/12773861/andengine-gles2-buttonsprite-dont-react-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!