2011-08-12 8 views
0

クラス内でCGRectを作成しています(パス)。クラスが長方形を作成していることを確認しました。さて、私はクラス内に、タッチ位置が四角形内にあるかどうかを尋ねられたときに、理論的にtrueを返すメソッドを構築しました。問題は、常にfalseを返すことです。私は、メソッドが必要に応じて触れていることを確認しました。下記の関連コード:Cocos2d:CGRect、UITouch - 境界内の位置を検出しないオブジェクト/クラス

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にタッチ位置を送信します。しかし、その代わりに四角形をタッチすると、真の報告のために、それは常に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]); 

は私が片の間に欠けている、または私は相互作用はすべて一緒にどのように機能するかを誤解しています接続のいくつかの種類がありますか?

ありがとうございました

答えて

0

それがわかりました。私は、私が間違って私の四角形を生成していた点を持っていた。

関連する問題