首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让某些精灵在触摸后消失- Cocos2d

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

Stack Overflow用户
提问于 2012-07-26 01:25:11
回答 1查看 770关注 0票数 0

我想知道如何在MainPlayer接触到星星后消失某个精灵(星星)。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-26 06:44:03

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

接下来,创建新方法:

代码语言:javascript
复制
- (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
    }

}

您最终可能想要利用的其他方法如下:

代码语言:javascript
复制
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

希望这能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11655242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档