2014-02-20 3 views
1

그래서 SKShapeNode 생성하고 해당 노드를 클릭하면 알 필요가 있습니다. 나는 전화해서 이렇게한다 :SKShapeNode 클릭했을 때 확인

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint positionInScene = [touch locationInNode:self]; 
    SKNode *node = [self nodeAtPoint:positionInScene]; 
    if ([node.name isEqualToString:TARGET_NAME]) { 
     // do whatever 
    } 
    } 
} 

그래서 내가 얻는 결과는 꽤 별났다. 도트 자체를 클릭하면 실제로 작동합니다. 그러나 SKShapeNode 위치의 남서쪽에있는 화면의 아무 곳이나 누르면 위 코드가 true로 렌더링됩니다. 빨간 점으로 표시되는 SKShapeNode와

enter image description here

는 음영 지역에있는 UITouch는 사실보다 내 코드를 렌더링 것입니다.

다음은 SKShapeNode를 구축하는 방법입니다. 내 응용 프로그램이 가로 모드로 실행된다는 점도 중요 할 수 있습니다.

#define RANDOM_NUMBER(min, max) (arc4random() % (max - min) + min) 


- (SKShapeNode *)makeNodeWithName:(NSString *)name color:(UIColor *)color 
{ 
    SKShapeNode *circle = [SKShapeNode new]; 

    int maxXCoord = self.frame.size.width; 
    int maxYCoord = self.frame.size.height; 
    CGFloat x = RANDOM_NUMBER((int)TARGET_RADIUS, (int)(maxXCoord - TARGET_RADIUS)); 
    CGFloat y = RANDOM_NUMBER((int)TARGET_RADIUS, (int)(maxYCoord - TARGET_RADIUS - 15)); 

    circle.fillColor = color; 
    circle.strokeColor = color; 
    circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, TARGET_RADIUS, TARGET_RADIUS)].CGPath; 

    circle.name = name; 
    return circle; 
} 

어떤 도움을 주셔서 감사합니다!

답변

4

이것은 원 노드의 위치가 원점이며 (x, y)에서 시작하는 rect에 경로를 그리기 때문에 발생합니다. 따라서 노드의 프레임은 (0,0)에서 (x + TARGET_RADIUS, y + TARGET_RADIUS) 사이의 모든 것을 포함하도록 확장됩니다. ,

Visualized clickable region

이 문제를 해결하려면 다음

SKSpriteNode *debugFrame = [SKSpriteNode spriteNodeWithColor:[NSColor yellowColor] size:circle.frame.size]; 
debugFrame.anchorPoint = CGPointMake(0, 0); 
debugFrame.position = circle.frame.origin; 
debugFrame.alpha = 0.5f; 
[self addChild:test]; 

이 (OSX에) 실제 클릭이 가능한 영역을 보여준다 :

당신은 원의 프레임을 시각화함으로써, 자신이을 확인할 수 있습니다 이것을 시도하십시오 :

circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-TARGET_RADIUS/2.0f, -TARGET_RADIUS/2.0f, TARGET_RADIUS, TARGET_RADIUS)].CGPath; 

및 추가

circle.position = CGPointMake(x, y); 
+0

내 친구가 지난 밤에 내게 이것을 실제로 지적했습니다. 나는 내 자신의 직책에 응답 할 것이지만, 내가 가질 수있는 것보다 더 나은 일을 해낸 것처럼 보입니다. 감사! – Dan

관련 문제