前端之家收集整理的这篇文章主要介绍了
cocos2d从按钮扩展触摸区域,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
301_0@
我有一些ra
diobuttons但是toucharea是小的. toucharea取决于图像大小.有没有一种优雅的
方法来扩展触摸区域与cocos2d而不使用更大的图像或使用cgrect我自己的触摸区域?
setContentSize做我想要的.不幸的是,图像移动到
内容大小的左下角.设置anchorpoint会移动
内容大小,但图像保留在左下角.
CCMenuItem* pickEasy = [CCMenuItemImage itemFromNormalImage:@"radiobutton_off.png" selectedImage:@"radiobutton_on.png" target:self selector:@selector(pickEasyTapped:)];
pickEasy.position = ccp(ss.width * 0.40,ss.height * 0.78);
[pickEasy setContentSize:CGSizeMake(50,50)];
提前致谢.
您需要覆盖rectInPixels
方法
- (CGRect)rectInPixels
{
CGSize s = [self contentSize];
return CGRectMake(0,s.width,s.height);
}
- (BOOL)containsTouchLocation:(UITouch *)touch
{
CGPoint p = [self convertTouchToNodeSpace:touch];
CGRect r = [self rectInPixels];
return CGRectContainsPoint(r,p);
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
for (UITouch *aTouch in allTouches) {
if ( ![self containsTouchLocation:aTouch] ) return NO;
}
return YES;
}
这只是告诉精灵检查你改变的CGRect中的触摸lyes
编辑显示CCSprite子类—
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}