我有一些radiobuttons但是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)];
提前致谢。
答案 0 :(得分:11)
采用原始答案代码......
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)];
...你只需将图像设置在正确的位置......
[[[pickEasy children] objectAtIndex:0] setAnchorPoint:ccp(0.5,0.5)];
[[[pickEasy children] objectAtIndex:1] setAnchorPoint:ccp(0.5,0.5)];
[[[pickEasy children] objectAtIndex:0] setPosition:ccp(pickEasy.contentSize.width/2,pickEasy.contentSize.height/2)];
[[[pickEasy children] objectAtIndex:1] setPosition:ccp(pickEasy.contentSize.width/2,pickEasy.contentSize.height/2)];
...只有4行代码!玩得开心!
答案 1 :(得分:4)
此外,您可以更改CCMenuItem的activeArea属性。 (cocos2d 2.x)
CGRect active = [someMenuItem activeArea];
[someMenuItem setActiveArea:CGRectMake(active.origin.x - active.size.width * 2.f, active.origin.y - active.size.height * 2.5f, active.size.width * 2.f, active.size.height * 2.f)];
[someMenu addChild:someMenuItem];
答案 2 :(得分:2)
您需要覆盖rectInPixels方法
- (CGRect)rectInPixels
{
CGSize s = [self contentSize];
return CGRectMake(0, 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];
}
答案 3 :(得分:2)
我通过覆盖CCMenu的-(CCMenuItem*) itemForTouch:(UITouch *)touch
来解决了这个问题。
-(CCMenuItem*) itemForTouch:(UITouch *)touch { CGPoint touchLocation = [touch locationInView:[touch view]]; touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; CCMenuItem* item; CCARRAY_FOREACH(children_, item) { if ([item visible] && [item isEnabled]) { CGPoint local = [item convertToNodeSpace:touchLocation]; CGRect r = [item rect]; r.origin = CGPointZero; // increase rect by * 2 // a rect at bottom left of the image CGRect bigR = CGRectMake(r.origin.x - r.size.width, r.origin.y - r.size.height, r.size.width * 2, r.size.width * 2); // a rect at top right of the image CGRect bigR2 = CGRectMake(0, 0, r.size.width * 2, r.size.width * 2); if (CGRectContainsPoint(bigR, local) || CGRectContainsPoint(bigR2, local)) { return item; } } } return nil; }
将图像中间的矩形居中放置