2014-09-30 3 views
0

는 현재, 나는 상자 (이 벽의 경계 안타 때에 튀는 공을 가지고있는 아이폰의 크기가 호출되는 움직이는 물체를 재배치SKSpriteKit는 -. 예상대로 다시 반사, didBeginContact이

.

이제 공을 왼쪽으로 치고 왼쪽 벽에 닿았을 때 오른쪽에 나타나고 왼쪽으로 계속 가고 싶습니다. 또한 공이 오른쪽으로 움직여 벽에 부딪쳤을 때 오른쪽에 여전히 오른쪽에 표시되어야합니다 (예전의 소행성 게임처럼)

나는 didBeginContact를 사용할 수 있고 단순히 위치를 변경 할 수 있다고 생각했습니다. 문제는 setPosition을 사용하여 위치를 변경하는 것입니다. 실제로 그것을 바꾼다. . 작동하는 moveTOx :를 사용하면 오른쪽의 didBeginContact에 대한 호출이 호출되고 왼쪽으로 다시 이동하므로 볼을 오른쪽 가장자리로 이동할 수 없다는 점이 문제입니다.

공은 한 모서리에서 다음 모서리로 부드럽게 움직여야합니다. 아마도 didBeginContact가 올바른 작업을 수행 할 수 없습니다.

제안 사항? 내가 솔루션 (https://github.com/macshome/RockBuster/tree/master)을 발견

답변

0

...이 독특한 문제가 상상할 수 없으며 여기있다 :

// Create a method to be called from the update: 
-(void)update:(NSTimeInterval)currentTime { 
    /* Called before each frame is rendered */ 
    [self updateSpritePositions]; 
} 

// In your method, iterate through your objects and set the position: 
- (void)updateSpritePositions { 
    [self enumerateChildNodesWithName:@"ball" usingBlock:^(SKNode *node, BOOL *stop) { 

     // Get the current position 
     CGPoint nodePosition = CGPointMake(node.position.x, node.position.y); 

     // If we've gone beyond the edge warp to the other side. 
     if (nodePosition.x > (CGRectGetMaxX(self.frame) + 20)) { 
      node.position = CGPointMake((CGRectGetMinX(self.frame) - 10), nodePosition.y); 
     } 

     if (nodePosition.x < (CGRectGetMinX(self.frame) - 20)) { 
      node.position = CGPointMake((CGRectGetMaxX(self.frame) + 10), nodePosition.y); 
     } 

     if (nodePosition.y > (CGRectGetMaxY(self.frame) + 20)) { 
      node.position = CGPointMake(nodePosition.x, (CGRectGetMinY(self.frame) - 10)); 
     } 

     if (nodePosition.y < (CGRectGetMinY(self.frame) - 20)) { 
      node.position = CGPointMake(nodePosition.x, (CGRectGetMaxY(self.frame) + 10)); 
     } 

    }]; 

} 
+0

당신은 노드의 크기 속성을 사용하는 대신 하드 코딩하여 코드를 일반화 할 수 값. – 0x141E

+0

확실히, 제안에 감사드립니다. 이 작업을 완벽하게 수행하면 바로 그 작업을 수행 할 것입니다. – fawsha1