我只是不明白 我使用cocos2d来开发iPhone / Pod上的小游戏.框架只是很棒,但是触摸检测失败.我读到你只需要覆盖适当的功能(例如“touchesBegan”)来实现一个类CocosNode的类.但它不行.我该怎么
我使用cocos2d来开发iPhone / Pod上的小游戏.框架只是很棒,但是触摸检测失败.我读到你只需要覆盖适当的功能(例如“touchesBegan”)来实现一个类CocosNode的类.但它不行.我该怎么做错了?
功能:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(@"tickle, hihi!");}
我完全错了吗
谢谢!
层是唯一接触的cocos2d类.诀窍是,所有层的实例都会通过触摸事件,一个接一个,所以你的代码必须处理这个.
我这样做:
-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint cLoc = [[Director sharedDirector] convertCoordinate: location];
float labelX = self.position.x - HALF_WIDTH;
float labelY = self.position.y - HALF_WIDTH;
float labelXWidth = labelX + WIDTH;
float labelYHeight = labelY + WIDTH;
if( labelX < cLoc.x &&
labelY < cLoc.y &&
labelXWidth > cLoc.x &&
labelYHeight > cLoc.y){
NSLog(@"WE ARE TOUCHED AND I AM A %@", self.labelString);
return kEventHandled;
} else {
return kEventIgnored;
}
}
请注意,cocos2d库具有“ccTouchesEnded”实现,而不是Apple标准.它允许您返回一个BOOL,指示是否处理了该事件.
祝你好运!
