Cocos2d iphone 如何让某些精灵在触摸后消失-Cocos2d

Cocos2d iphone 如何让某些精灵在触摸后消失-Cocos2d,cocos2d-iphone,Cocos2d Iphone,我想知道如何在主玩家触摸到某个精灵(星星)后消失 谢谢。顺便说一句,我是Cocos2d新手,我这么做只是为了好玩和教育。 谢谢。如果您想在cocos2d中检测触摸,您需要在init方法中将isTouchEnabled属性设置为YES。您现在可以利用触摸事件 接下来,创建新方法: - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //This method gets triggered every ti

我想知道如何在主玩家触摸到某个精灵(星星)后消失

谢谢。顺便说一句,我是Cocos2d新手,我这么做只是为了好玩和教育。
谢谢。

如果您想在cocos2d中检测触摸,您需要在init方法中将
isTouchEnabled
属性设置为
YES
。您现在可以利用触摸事件

接下来,创建新方法:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //This method gets triggered every time a tap is detected
    NSSet *allTouches = [event allTouches]; //Get all the current touches in the event
    UITouch *aTouch = [allTouches anyObject]; //Get one of the touches, multitouch is disabled, so there is only always going to be one.
    CGPoint pos = [aTouch locationInView:touch.view]; //Get the location of the touch
    CGPoint ccPos = [[CCDirector sharedDirector] convertToGL:pos]; //Convert that location to something cocos2d can use
    if (CGRectContainsPoint(yourSprite.boundingBox, ccPos)) //Method to check if a rectangle contains a point
    {
        yourSprite.visible = NO; //Make your sprite invisible
    }

}
您最终可能希望利用的其他方法如下:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
希望这有帮助