cocos2d 从按钮扩展触摸区域

发布于 2024-10-31 17:26:22 字数 487 浏览 1 评论 0原文

我有一些单选按钮,但触摸区域太小。触摸区域取决于图像尺寸。有没有一种优雅的方法可以使用 cocos2d 扩展触摸区域,而无需使用更大的图像或使用 cgrect 制作自己的触摸区域? setContentSize 做我想要的。不幸的是,图像移动到内容大小的左下角。设置锚点会移动内容大小,但图像保留在左下角。

    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)];

提前致谢。

I got some radiobuttons but the toucharea is to small. The toucharea depends on the image size. Is there an elegant way to extend the touch area with cocos2d without using a bigger image or make my own touch areas with cgrect?
setContentSize do what I want. Unfortunately the image moves to the left bottom corner of the contentsize. Set the anchorpoint moves the contentsize around but the image stays in the left bottom corner.

    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)];

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

几度春秋 2024-11-07 17:26:22

获取原始答案代码...

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行代码!玩得开心!

Taking the original answer code...

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)];

... you only have to set the image in the correct position...

[[[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)];

...only with 4 lines of code! Have fun!

美男兮 2024-11-07 17:26:22

另外,您可以更改 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];

Also, you can change activeArea property of CCMenuItem. (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];
胡渣熟男 2024-11-07 17:26:22

您需要重写 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

编辑中的触摸碱液以显示 CCSprite 子类 ---

- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}

- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}   

You need to override the rectInPixels method

- (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;
}

This just tells the sprite to check that the touch lyes within your altered CGRect

Edit to show CCSprite subclass ---

- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}

- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}   
凡尘雨 2024-11-07 17:26:22

我通过覆盖 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;
}

将矩形居中于图像中间不起作用

I made a workaround by overriding -(CCMenuItem*) itemForTouch:(UITouch *)touch from CCMenu.

-(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;
}

Center the rect in the middle of the image didnt worked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文