cocos2D for iPhone和触摸检测的问题 [英] Problem with cocos2D for iPhone and touch detection

查看:147
本文介绍了cocos2D for iPhone和触摸检测的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不明白。
我使用cocos2d开发一个小游戏的iPhone / Pod。框架是伟大的,但我没有在触摸检测。我读到,你只需要覆盖适当的函数(例如touchesBegan)在类的CocosNode子类的实现。但它不工作。我可以做什么错误?

I just don't get it. I use cocos2d for development of a small game on the iPhone/Pod. The framework is just great, but I fail at touch detection. I read that you just need to overwrite the proper functions (e.g. "touchesBegan" ) in the implementation of a class wich subclasses CocosNode. But it doesn't work. What could I do wrong?

功能:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(@"tickle, hihi!");}

我完全错了吗?

谢谢!

推荐答案

Layer是唯一可以触摸的cocos2d类。

Layer is the only cocos2d class which gets touches.

诀窍是Layer的所有实例都会传递触摸事件,

The trick is that ALL instances of Layer get passed the touch events, one after the other, so your code has to handle this.

我这样做:

-(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,指示是否处理该事件。

Note that the cocos2d library has a "ccTouchesEnded" implementation, rather than the Apple standard. It allows you to return a BOOL indicating whether or not you handled the event.

祝你好运!

这篇关于cocos2D for iPhone和触摸检测的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆