为什么多点触控无法使用动态GLES-2 SimpleBaseGameActivity?

时间:2013-01-27 04:29:52

标签: android andengine multi-touch

嗨我正在尝试制作一个双人乒乓球游戏,但是它的多点触控部分不起作用我知道我正在使用的设备是启用多点触控,所以我的代码中的某些内容必定是错误的。现在我有两个专用的背景部分,触摸时设置了拨片位置,但是如果某个手指在屏幕上,它将无法识别第二次触摸。

public class TwoPlayerMode extends SimpleBaseGameActivity {

private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private ITextureRegion mField, mField1, mField2, mField3, mPaddle;
private Sprite bottomPaddleSprite, topPaddleSprite, backgroundSpriteP2, backgroundSpriteP1, backgroundSpriteP3;
private String homelife, awaylife;
private int homeCount, awayCount;
Font mFont;
Text scores, lives;
SharedPreferences prefs;
MultiTouchController mtc;

@Override
public EngineOptions onCreateEngineOptions() {
    mtc = new MultiTouchController();
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    final FixedStepEngine engine = new FixedStepEngine(new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
                    CAMERA_WIDTH, CAMERA_HEIGHT), camera),60);
    if (MultiTouch.isSupported(this)) {
        Toast.makeText(this, "Multitouch", Toast.LENGTH_LONG).show();
        engine.setTouchController(mtc);
    }

    return engine.getEngineOptions();

}



@Override
protected void onCreateResources() {
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    try {

        ITexture paddle = new BitmapTexture(this.getTextureManager(),
                new IInputStreamOpener() {
                    @Override
                    public InputStream open() throws IOException {
                        boolean light = prefs.getBoolean("theme", false);
                        if (light) {
                            return getAssets().open("gfx/paddle2.png");
                        } else {
                            return getAssets().open("gfx/paddle.png");
                        }
                    }
                });
        ITexture fieldmid = new BitmapTexture(this.getTextureManager(),
                new IInputStreamOpener() {
                    @Override
                    public InputStream open() throws IOException {
                        boolean light = prefs.getBoolean("theme", false);
                        if (light) {
                            return getAssets().open("gfx/fieldmid2.png");
                        } else {
                            return getAssets().open("gfx/fieldmid.png");
                        }

                    }
                });
        ITexture fieldend = new BitmapTexture(this.getTextureManager(),
                new IInputStreamOpener() {
                    @Override
                    public InputStream open() throws IOException {
                        boolean light = prefs.getBoolean("theme", false);
                        if (light) {
                            return getAssets().open("gfx/fieldend2.png");
                        } else {
                            return getAssets().open("gfx/fieldend.png");
                        }

                    }
                });

        // 2 - Load bitmap textures into VRAM

        fieldend.load();
        fieldmid.load();
        paddle.load();

        // 3 - Set up texture regions


        this.mField1 = TextureRegionFactory.extractFromTexture(fieldend);
        this.mField2 = TextureRegionFactory.extractFromTexture(fieldmid);
        this.mField3 = TextureRegionFactory.extractFromTexture(fieldend);
        this.mPaddle = TextureRegionFactory.extractFromTexture(paddle);
        loadFonts();
    } catch (IOException e) {
        Debug.e(e);
    }

}

public void loadFonts() {
    FontFactory.setAssetBasePath("font/");
    final ITexture fontTexture = new BitmapTextureAtlas(
            getTextureManager(), 256, 256,
            TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    boolean light = prefs.getBoolean("theme", false);
    if (light) {
        mFont = FontFactory.createFromAsset(getFontManager(), fontTexture,
                getAssets(), "plaincre.ttf", 40f, true,
                Color.rgb(0, 221, 255));

    } else {
        mFont = FontFactory.createFromAsset(getFontManager(), fontTexture,
                getAssets(), "plaincre.ttf", 40f, true,
                Color.rgb(0, 153, 204));

    }
    mFont.load();

}

@Override
protected Scene onCreateScene() {

    final Scene scene = new Scene();

    topPaddleSprite = new Sprite(0, 195, this.mPaddle,
            getVertexBufferObjectManager());
    bottomPaddleSprite = new Sprite(785, 195, this.mPaddle,
            getVertexBufferObjectManager());
    // my spirtes and background
    /*
     * Sprite backgroundSprite = new Sprite(0, 0, this.mField,
     * getVertexBufferObjectManager()); scene.attachChild(backgroundSprite);
     */
    backgroundSpriteP1 = new Sprite(0, 0, this.mField1,
            getVertexBufferObjectManager())  {
        boolean mGrabbed = false;
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            switch(pSceneTouchEvent.getAction()) {
                        case TouchEvent.ACTION_DOWN:
                            if (pSceneTouchEvent.isActionMove()
                                    || pSceneTouchEvent.isActionDown()) {
                                topPaddleSprite.setPosition(0f,
                                        pSceneTouchEvent.getY() - topPaddleSprite.getHeight() / 2);
                            }
                                this.mGrabbed = true;
                                break;
                        case TouchEvent.ACTION_MOVE:
                                if(this.mGrabbed) {
                                    if (pSceneTouchEvent.isActionMove()
                                            || pSceneTouchEvent.isActionDown()) {
                                        topPaddleSprite.setPosition(0f,
                                                pSceneTouchEvent.getY() - topPaddleSprite.getHeight() / 2);
                                    } }
                                break;
                        case TouchEvent.ACTION_UP:
                                if(this.mGrabbed) {
                                        this.mGrabbed = false;
                                        if (pSceneTouchEvent.isActionMove()
                                                || pSceneTouchEvent.isActionDown()) {
                                            topPaddleSprite.setPosition(0f,
                                                    pSceneTouchEvent.getY() - topPaddleSprite.getHeight() / 2);
                                        }
                                }
                                break;
                }
                return true;
        }
};
    backgroundSpriteP2 = new Sprite(backgroundSpriteP1.getWidth(), 0,
            this.mField2, getVertexBufferObjectManager());
    backgroundSpriteP3 = new Sprite(backgroundSpriteP1.getWidth()
            + backgroundSpriteP2.getWidth(), 0, this.mField3,
            getVertexBufferObjectManager()) {
        boolean mGrabbed = false;
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            switch(pSceneTouchEvent.getAction()) {
                        case TouchEvent.ACTION_DOWN:

                                bottomPaddleSprite.setPosition(785f,
                                        pTouchAreaLocalY - bottomPaddleSprite.getHeight() / 2);

                                this.mGrabbed = true;
                                break;
                        case TouchEvent.ACTION_MOVE:
                                if(this.mGrabbed) {

                                        bottomPaddleSprite.setPosition(785f,
                                                pSceneTouchEvent.getY() - bottomPaddleSprite.getHeight() / 2);
                                     }
                                break;
                        case TouchEvent.ACTION_UP:
                                if(this.mGrabbed) {
                                        this.mGrabbed = false;

                                            bottomPaddleSprite.setPosition(785f,
                                                    pSceneTouchEvent.getY() - bottomPaddleSprite.getHeight() / 2);

                                }
                                break; 
                }
                return true;
        }
};


    scene.registerTouchArea(backgroundSpriteP1);
    scene.registerTouchArea(backgroundSpriteP3);
    scene.setTouchAreaBindingOnActionMoveEnabled(true);
    scene.setTouchAreaBindingOnActionDownEnabled(true);
    scene.attachChild(backgroundSpriteP1);
    scene.attachChild(backgroundSpriteP2);
    scene.attachChild(backgroundSpriteP3);
    scene.attachChild(topPaddleSprite);
    scene.attachChild(bottomPaddleSprite);


    awayCount = 3;
    awaylife = "3";
    homeCount = 3;
    homelife = "3";
    scores = new Text(50, 50, mFont, awaylife,
            getVertexBufferObjectManager());
    lives = new Text(650, 50, mFont, homelife,
            getVertexBufferObjectManager());
    scene.attachChild(lives);
    scene.attachChild(scores);

    return scene;
}

}

这是截图 screen shot

1 个答案:

答案 0 :(得分:1)

看起来问题是here

在发布的代码中没有引用TouchOptions。