ZhouWeikuan / cocos2d

cocos2d for android, based on cocos2d-android-0.82, and now ported from cocos2d-iphone 0.99.4. The googlecode address is here: http://code.google.com/p/cocos2d-android-1/ . There are several demos to watch.
610 stars 291 forks source link

I can't get multi touch to work in ccTouchesMoved() #24

Closed leinardi closed 13 years ago

leinardi commented 13 years ago

I can't get multi touch to work in ccTouchesMoved(). It works correctly in ccTouchesBegan(), but in ccTouchesMoved() I get only event from ActionIndex=0.

It's my fault or is it a bug?

This is the code of my Layer:

public class HelloWorldLayer extends CCColorLayer {

    private static final String TAG = "Cocos2D";
    private CCSprite xRed;
    private CCSprite xGreen;

    protected HelloWorldLayer(ccColor4B color) {
        super(color);
        this.setIsTouchEnabled(true);

        xRed = CCSprite.sprite("x-red.png");
        xGreen = CCSprite.sprite("x-green.png");
    }

    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        int pointerIndex = event.getPointerId(event.getActionIndex());
        CGPoint location =  CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(pointerIndex),event.getY(pointerIndex)));
        Log.d(TAG, String.format("touch %d=(%f,%f)", event.getActionIndex(), location.x,location.y));

        switch (event.getActionIndex()) {
            case 0:
                xRed.setPosition(location);
                addChild(xRed);
                break;
            case 1:
                xGreen.setPosition(location);
                addChild(xGreen);
                break;
            default:
                break;
        }

        return super.ccTouchesBegan(event);
    }

    @Override
    public boolean ccTouchesMoved(MotionEvent event) {
        int pointerIndex = event.getPointerId(event.getActionIndex());
        CGPoint location =  CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(pointerIndex),event.getY(pointerIndex)));
        Log.d(TAG, String.format("move %d=(%f,%f)", event.getActionIndex(), location.x,location.y));

        switch (event.getActionIndex()) {
            case 0:
                xRed.setPosition(location);
                break;
            case 1:
                xGreen.setPosition(location);
                break;
            default:
                break;
        }

        return super.ccTouchesMoved(event);
    }

    @Override
    public boolean ccTouchesEnded(MotionEvent event) {
        Log.d(TAG, "ccTouchesEnded");
        int pointerIndex = event.getPointerId(event.getActionIndex());
        CGPoint location =  CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(pointerIndex),event.getY(pointerIndex)));
        Log.d(TAG, String.format("touch %d=(%f,%f)", event.getActionIndex(), location.x,location.y));     

        switch (event.getActionIndex()) {
            case 0:
                removeChild(xRed, true);
                break;
            case 1:
                removeChild(xGreen, true);
                break;
            default:
                break;
        }

        return super.ccTouchesEnded(event);
    }

    public static CCScene scene() {
        CCScene scene = CCScene.node();
        CCLayer layer = new HelloWorldLayer(ccColor4B.ccc4(255, 255, 255, 255));

        scene.addChild(layer);

        return scene;
    }
}
leinardi commented 13 years ago

Ok, solved: in the function ccTouchesMoved() you must handle an array of touch and not a single touch.

    @Override
    public boolean ccTouchesMoved(MotionEvent event) {
        int pointCnt = event.getPointerCount();
        for (int i = 0; i < pointCnt; i++) {
            CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(i),event.getY(i)));
            Log.d(TAG, String.format("move %d=(%f,%f)", i, location.x, location.y));
        }
        return super.ccTouchesMoved(event);
    }