我是 cocos2d 的新手,请原谅我的无知,但我想知道如何检测 Sprite 何时被触摸并在被触摸时调用方法。
我已经像这样定义并添加了我的 Sprite :
CCSprite *infoButton = [CCSprite spriteWithFile: @"info.png"];
[infoButton setPosition:CGPointMake(450, 290)];
[menuBG addChild:infoButton];
我已经关注了各种资源,但它们非常模糊,其中大部分 Sprite 都设置在它自己的类中。
提前致谢。
最佳答案
在常规 Cocos2D 中:
-(void) ccTouchesBegan:(NSSet*)touches withEvent:(id)event
{
CCDirector* director = [CCDirector sharedDirector];
UITouch* touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:director.openGLView];
CGPoint locationGL = [director convertToGL:touchLocation];
CGPoint locationInNodeSpace = [infoButton convertToNodeSpace:locationGL];
CGRect bbox = CGRectMake(0, 0,
infoButton.contentSize.width,
infoButton.contentSize.height);
if (CGRectContainsPoint(bbox, locationInNodeSpace))
{
// code for when user touched infoButton sprite goes here ...
}
}
要演示多少Kobold2D通过 Cocos2D 的方法简化了这一点:
-(void) update:(ccTime)delta
{
KKInput* input = [KKInput sharedInput];
if ([input isAnyTouchOnNode:infoButton touchPhase:KKTouchPhaseBegan])
{
// code for when user touched infoButton sprite goes here ...
}
}
关于ios - 在 CCSprite 上检测触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883670/