在cocos2d-android中为精灵触摸事件

时间:2013-06-29 07:25:55

标签: cocoa-touch cocos2d-iphone ccsprite cocos2d-android

为了让精灵(canon)触摸并对cocos2d-android中的触摸做出反应(触发子弹),我在构造函数中设置了this.setIsTouchEnabled(true);,并在ccToouchesBegan()中添加了以下代码

@Override
public boolean ccTouchesBegan(MotionEvent event)    
{  

    canon1 = CCSprite.sprite("android.png");
     canon1.setPosition(CGPoint.ccp(10, 60));
     canon1.setScale(1f);
    addChild(canon1);

    CGRect canon1Rect = CGRect.make(canon1.getPosition().x - (canon1.getContentSize().width/2),
            canon1.getPosition().y - (canon1.getContentSize().height/2),
            canon1.getContentSize().width,
            canon1.getContentSize().height);

    // Choose one of the touches to work with
    CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(10,60));

return;
}

因此当触及精灵(加农炮)时,子弹必须开火,但这里触摸不起作用。

1 个答案:

答案 0 :(得分:0)

我希望这会对你有帮助。

public boolean ccTouchesEnded(MotionEvent event) {
        CGPoint location = CCDirector.sharedDirector().convertToGL(
                CGPoint.ccp(event.getX(), event.getY()));
        if (CGRect.containsPoint((newGame1.getBoundingBox()), location)) {

            newGame();

        }

        return super.ccTouchesEnded(event);
    }