Cocos2d:CGRect、UITouch - 对象/类未检测到其边界内的位置

发布于 2024-11-29 11:21:43 字数 2027 浏览 2 评论 0原文

我正在类(路径)中创建一个 CGRect。我已经验证该类正在创建矩形。现在,我在类中构建了一个方法,理论上,当询问触摸位置是否在矩形内时,该方法应该返回 true。问题是,它总是返回 false。我已经验证该方法正在根据需要进行触摸。相关代码如下:

Path.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Path : CCSprite {
    CGPoint startP;
    CGPoint endP;
    CGRect pathRect;
    CGPoint touchP;
}

-(id)initWithPoints:(CGPoint)sP :(CGPoint)eP;
-(bool)touchWithinBounds:(CGPoint)touch;

@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, assign) CGPoint endP;
@property (nonatomic, assign) CGRect pathRect;
@end

Path.mm

#import "Path.h"

@implementation Path

@synthesize startP;
@synthesize endP;
@synthesize pathRect;

-(id) initWithPoints:(CGPoint)sP :(CGPoint)eP {
    if ((self = [super init])) {
        startP = sP;
        endP = eP;
        pathRect = CGRectMake(startP.x-2, endP.y, 5, 480);
    }
    return self;
}

-(bool)touchWithinBounds:(CGPoint)touch {
    touchP = touch;
    if(CGRectContainsPoint([self pathRect], touch)) {
        return true;
    } else {
        return false;
    }
}

-(void) draw {
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    glLineWidth(5.0f);
    ccDrawLine(startP, endP);
}

-(void) dealloc {
    [super dealloc];
}

@end

在我的主游戏场景中,我初始化一个路径并将其添加到可变数组中:

paths = [[NSMutableArray alloc] init];
        Path *path1 = [[[Path alloc] initWithPoints:ccp(winSize.width/3, 0.0) :ccp(winSize.width/3, winSize.height)] autorelease];
        [self addChild:path1 z:0];
        [paths addObject:path1];

然后在 ccTouchesBegan 中,我将触摸位置发送到 touchWithinBounds。但是,当触摸矩形时,它不会报告 true,而是总是报告 false:

UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view ]];
    for (Path *path in paths) {
        CCLOG(@"%d", [path touchWithinBounds:location]);

各个部分之间是否缺少某种连接,或者我是否误解了交互的整体工作原理?

谢谢

I'm creating a CGRect within a class (Path). I've verified that the the class is creating the rectangle. Now, I've built a method into the class that should, in theory, return true when asked if a touch location is within the rectangle. Problem is, it always returns false. I've verified that the method is getting the touch, as needed. Relevant code below:

Path.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Path : CCSprite {
    CGPoint startP;
    CGPoint endP;
    CGRect pathRect;
    CGPoint touchP;
}

-(id)initWithPoints:(CGPoint)sP :(CGPoint)eP;
-(bool)touchWithinBounds:(CGPoint)touch;

@property (nonatomic, assign) CGPoint startP;
@property (nonatomic, assign) CGPoint endP;
@property (nonatomic, assign) CGRect pathRect;
@end

Path.mm

#import "Path.h"

@implementation Path

@synthesize startP;
@synthesize endP;
@synthesize pathRect;

-(id) initWithPoints:(CGPoint)sP :(CGPoint)eP {
    if ((self = [super init])) {
        startP = sP;
        endP = eP;
        pathRect = CGRectMake(startP.x-2, endP.y, 5, 480);
    }
    return self;
}

-(bool)touchWithinBounds:(CGPoint)touch {
    touchP = touch;
    if(CGRectContainsPoint([self pathRect], touch)) {
        return true;
    } else {
        return false;
    }
}

-(void) draw {
    glColor4f(1.0f,1.0f,1.0f,1.0f);
    glLineWidth(5.0f);
    ccDrawLine(startP, endP);
}

-(void) dealloc {
    [super dealloc];
}

@end

In my main game scene, I initialize a path and add it to a mutable array:

paths = [[NSMutableArray alloc] init];
        Path *path1 = [[[Path alloc] initWithPoints:ccp(winSize.width/3, 0.0) :ccp(winSize.width/3, winSize.height)] autorelease];
        [self addChild:path1 z:0];
        [paths addObject:path1];

And then in ccTouchesBegan, I send the touch location to touchWithinBounds. But instead of reporting true when the rectangle is touched, it always reports false:

UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view ]];
    for (Path *path in paths) {
        CCLOG(@"%d", [path touchWithinBounds:location]);

Is there some kind of connection I'm missing between the pieces, or am I misunderstanding how the interaction works all-together?

Thanks

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

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

发布评论

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

评论(1

っ〆星空下的拥抱 2024-12-06 11:21:43

想通了。我有一些观点是我错误地创建了我的矩形。

Figured it out. I had the points I was originating my rectangle at wrong.

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