我开始查看示例项目(与here相关的示例游戏特别有用),我看到处理触摸通常不是在CCSprite中完成的.相反,实例化CCSprites的cclayer对触摸事件做出反应,并遍历通过它创建的精灵来检测哪个CCSprite被触摸(如果有的话).
我想让CCSprites处理他们是否被感动了,并呼吁通知它被触动(如果需要的话).在/ tests / touchesTest下找到的Paddle类只是这样 – 它自己处理触摸.
所以,我的问题是:这是最好的做法?在中央位置处理触摸是否更好,并通过儿童迭代看到被触动的内容?还是应该每个孩子处理自己的触摸事件?还是没关系?
我更喜欢每个孩子处理自己的触摸事件,但我想遵循最佳做法(如果存在).谢谢!
解决方法 我认为这是一个偏好的问题,但我喜欢让sprite检测到它是否被子类化CCSprite所触动.我在我的CCSprite子类中创建一个getter方法,该子类从子类中检索状态变量,然后主程序可以相应地执行.以下是我的CCSprite子类“spubutton”的示例头文件:
#import "cocos2d.h" typedef enum tagbuttonState { kbuttonStatepressed,kbuttonStateNotpressed } buttonState; typedef enum tagbuttonStatus { kbuttonStatusEnabled,kbuttonStatusDisabled } buttonStatus; @interface spubutton : CCSprite <CCTargetedtouchDelegate> { @private buttonState state; CCTexture2D *buttonnormal; CCTexture2D *buttonlit; buttonStatus buttonStatus; } @property(nonatomic,Readonly) CGRect rect; + (ID)spubuttonWithTexture:(CCTexture2D *)normalTexture; - (voID)setnormalTexture:(CCTexture2D *)normalTexture; - (voID)setlitTexture:(CCTexture2D *)litTexture; - (BOol)ispressed; - (BOol)isNotpressed; @end
这里是.m文件的一个例子:
#import "spubutton.h" #import "cocos2d.h" @implementation spubutton - (CGRect)rect { CGSize s = [self.texture contentSize]; return CGRectMake(-s.wIDth / 2,-s.height / 2,s.wIDth,s.height); } + (ID)spubuttonWithTexture:(CCTexture2D *)normalTexture { return [[[self alloc] initWithTexture:normalTexture] autorelease]; } - (voID)setnormalTexture:(CCTexture2D *)normalTexture { buttonnormal = normalTexture; } - (voID)setlitTexture:(CCTexture2D *)litTexture { buttonlit = litTexture; } - (BOol)ispressed { if (state == kbuttonStateNotpressed) return NO; if (state == kbuttonStatepressed) return YES; return NO; } - (BOol)isNotpressed { if (state == kbuttonStateNotpressed) return YES; if (state == kbuttonStatepressed) return NO; return YES; } - (ID)initWithTexture:(CCTexture2D *)aTexture { if ((self = [super initWithTexture:aTexture]) ) { state = kbuttonStateNotpressed; } return self; } - (voID)onEnter { [[CCtouchdispatcher shareddispatcher] addTargetedDelegate:self priority:0 swallowstouches:YES]; [super onEnter]; } - (voID)onExit { [[CCtouchdispatcher shareddispatcher] removeDelegate:self]; [super onExit]; } - (BOol)containstouchLocation:(UItouch *)touch { return CGRectContainsPoint(self.rect,[self converttouchToNodeSpaceAR:touch]); } - (BOol)cctouchBegan:(UItouch *)touch withEvent:(UIEvent *)event { if (state == kbuttonStatepressed) return NO; if ( ![self containstouchLocation:touch] ) return NO; if (buttonStatus == kbuttonStatusDisabled) return NO; state = kbuttonStatepressed; [self setTexture:buttonlit]; return YES; } - (voID)cctouchmoved:(UItouch *)touch withEvent:(UIEvent *)event { // If it weren't for the touchdispatcher,you would need to keep a reference // to the touch from touchBegan and check that the current touch is the same // as that one. // Actually,it would be even more complicated since in the Cocos dispatcher // you get NSSets instead of 1 UItouch,so you'd need to loop through the set // in each touchXXX method. if ([self containstouchLocation:touch]) return; //if (buttonStatus == kbuttonStatusDisabled) return NO; state = kbuttonStateNotpressed; [self setTexture:buttonnormal]; } - (voID)cctouchended:(UItouch *)touch withEvent:(UIEvent *)event { state = kbuttonStateNotpressed; [self setTexture:buttonnormal]; }@end
希望这有助于快乐编码!
添加滴答法和解释(对于下面的Stephan的问题):
要检查按钮的状态,我有一个tick:方法,基本上触发每一帧,并检查所有按钮的状态.
-(voID)tick:(ccTime)dt {do my button checks here....}
我通过调用ispressed或isNotpressed函数来检查我的按钮的状态,这是我的spubutton类的一部分.
for (spubutton *abutton in _fourbuttonsArray) { if ([abutton isNotpressed]) continue; //this button is not pressed .....otherwise record that it is pressed.....}
然后我做同样的检查,看看它是否已经被释放并作出相应的响应.我这样做是因为我想要能够对多个按钮按钮进行反应加上我想要做某事,当它被按下,然后其他的东西,当它被释放.我使用cctouchBegan和cctouchended来更改纹理(sprite图像)并相应地更改状态变量.
总结以上是内存溢出为你收集整理的iphone – 使用cocos2d处理触摸CCSprite的最佳做法全部内容,希望文章能够帮你解决iphone – 使用cocos2d处理触摸CCSprite的最佳做法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)