【发布时间】:2014-02-24 18:01:57
【问题描述】:
我想知道如何在 MainPlayer 接触到星星后消失某些精灵(星星)。
谢谢。顺便说一句,我是 Cocos2d 的新手,我这样做只是为了好玩和教育目的。 谢谢。
【问题讨论】:
标签: cocos2d-iphone
我想知道如何在 MainPlayer 接触到星星后消失某些精灵(星星)。
谢谢。顺便说一句,我是 Cocos2d 的新手,我这样做只是为了好玩和教育目的。 谢谢。
【问题讨论】:
标签: cocos2d-iphone
如果你想在 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;
希望这会有所帮助。
【讨论】: