0

为了在 cocos2d-android 中让 sprite(canon) 触摸并对触摸做出反应(发射子弹),我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;
}

所以当触摸精灵(大炮)时,子弹必须发射出去,但在这里触摸不起作用。

4

1 回答 1

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);
    }
于 2013-08-04T16:35:15.120 回答