SKShapeNode触摸

时间:2017-06-12 19:46:32

标签: objective-c sprite-kit physics skphysicsbody

我目前正在使用SpriteKit开发IOS游戏。

我的背景是SKShapeNode。基本上这个形状的路径是带有一些曲线的bezierPath。播放器可以使用touchBegan或touchMove触发器更新此路径。

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event{
     CGPoint touchPoint = [[touches anyObject] locationInView:self.view];

     // Create path
     UIBezierPath* newPath = ...

     // Update path
     [self.backgroundShape setPath:newPath.CGPath];
     [self.backgroundShape setPhysicsBody:[SKPhysicsBody bodyWithEdgeLoopFromPath:newPath.CGPath]];
} 

在更新背景路径时,其他一些SKNode可以进入我的背景。

让我们说我们在y0(原始垂直位置)的桌子上有一些桌子(SKNodes)。将表格的物理更新为另一个垂直位置(例如y0 + deltaY)时,框(受重力影响)会在屏幕底部下降。

我该怎样防止这种情况?我想更新表的物理,但我希望盒子能够保留在它上面。

Short video of the current issue

谢谢,

1 个答案:

答案 0 :(得分:0)

在你的游戏中:

func keepBallOnTable() {
  if ball.frame.minY < table.frame.maxY {
    ball.position.y = table.frame.maxY + (ball.size.height / 2) + 1
  }
}

override func didFinishUpdate() {
  keepBallOnTable()
}

你必须翻译成objC:P

Video results of implementation

gist of the whole gamescene

物理主体和.moveTo.position之类的东西并不是很好。每当你试图混合它们时,你将不得不与物理系统作斗争。因此,我在上面创建的函数:)

相关问题