目前,我有一个盒子(iPhone的大小有一个球在其中弹跳。当它碰到墙壁的边界时,它会按预期反弹。
现在我要做的是当球向左移动并击中左墙时,我希望它出现在右侧并继续向左移动。此外,如果球向右移动并击中右侧的墙壁,那么它应该出现在左侧仍然向右。 (就像旧的小行星游戏一样)
我认为我可以使用didBeginContact并简单地改变位置。问题是使用setPosition改变位置:实际上并没有改变它。如果我使用moveTOx:,这确实有效,那么问题是球不能被移动到右边缘,因为调用右边的didBeginContact会被调用并且它会被移回左侧
球应该从一个边缘平滑移动到下一个边缘。也许didBeginContact不是正确的地方。
有什么建议吗?我无法想象这是一个独特的问题......
答案 0 :(得分:0)
我找到了解决方案(https://github.com/macshome/RockBuster/tree/master),现在是:
// 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));
}
}];
}