2011-10-16 3 views
0

* 작업 코드는 이제 *터치 위치 CCLayer

OK, 나는이 작업을 쉽게 얻을 수있을 것이라고 생각하지만 내가 예상처럼 작동하지에 밝혀졌습니다.

화면의 위치가 아니라 이동하거나 확대 할 수있는 CCLayer에서 Touch 위치를 얻으려고합니까? 여기가 어떻게 작동 할 것이라고 생각했지만 충돌이 일어 났습니까?

인터페이스 # import를 "cocos2d.h"

@interface TestTouch : CCLayer { 
    CCLayerColor *layer; 
} 

+(CCScene *) scene; 


@end 

구현

#import "TestTouch.h" 

@implementation TestTouch 

+(CCScene *) scene 
{ 
// 'scene' is an autorelease object. 
CCScene *scene = [CCScene node]; 

// 'layer' is an autorelease object. 
TestTouch *layer = [TestTouch node]; 

// add layer as a child to scene 
[scene addChild: layer]; 

// return the scene 
return scene; 
} 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
     CGSize winsize = [[CCDirector sharedDirector]winSize]; 
     CCLayerColor *layer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; 
//  layer.scale = 0.7f; 
     layer.contentSize = CGSizeMake(640, 960); 
     layer.position = CGPointMake(winsize.width/2, winsize.height/2); 
     layer.isRelativeAnchorPoint = YES; 
     [self addChild:layer z:0]; 
     [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:layer priority:0 swallowsTouches:YES]; 
    } 

    return self; 
} 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; 
    touchStart = [layer convertToNodeSpace:touchStart]; 
    NSLog(@"Touch:%f,%f",touchStart.x, touchStart.y); 

    return YES; 
} 

@end 

나는 "자기"를 포함하는이 줄을 변경할 경우 :

[[CCTouchDispatcher sharedDispatcher을] addTargetedDelegate : 자기 우선 순위 : 0 swallowsTouches : YES];

분명히 작동하지만 화면과 관련이있는 위치를 얻을 수 있습니다. 이는 필요한 부분입니다.

답변

1

당신은 레이어의 "노드 공간"화면의 위치를 ​​변환해야합니다

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; 

    // convert touch location to layer space 
    touchStart = [self convertToNodeSpace:touchStart]; 

    NSLog(@"Touch:%f,%f",touchStart.x, touchStart.y); 

    return YES; 
} 
+0

불행하게도 그 차이가 없습니다 만 답변을 주셔서 대단히 감사합니다. touchDispacher를 레이어와 셀프로 설정하려고했지만 여전히 레이어에서 충돌합니다. 이 문제에 대한 테스트 프로젝트를 설정하고 원래의 질문을 정확한 실행 코드로 수정했습니다. – EcksMedia

+0

당신은 전설입니다. 나는 몇 분 동안 놀고 보았고 당신이 실제로 답을 찾았지만 Var로 레이어를 설정하고 [self convertToNodeSpace : touchStart]를 변경해야한다는 것을 알았습니다. ~ [layer convertToNodeSpace : touchStart]; – EcksMedia