作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款游戏,你必须控制一艘船,并且你必须成为最后一艘活着的船。看,我希望游戏运行的方式是,当你的船的前部撞击另一艘船的侧面或背面时,它会“杀死”另一艘船,反之亦然。到目前为止,我还没有找到基于节点不同侧面来做到这一点的方法。我尝试通过在船的侧面和前面添加小的、不可见的节点来实现这一点,然而,这导致了很多故障,船在与随机物体碰撞时会瞬间离开碰撞箱。到目前为止,这是我的代码:
func addApple(_ position: CGPoint) {
guard let appleImage = UIImage(named: "apple") else {
return
}
let texture = SKTexture(image: appleImage)
let apple = SKSpriteNode(texture: texture)
apple.position = position
apple.setScale(0.1)
addChild(apple)
appleNode = apple
}
addApple(CGPoint(x: -100, y: -100))
然后我用这个来添加CPU船
locationx2 = (appleNodeCpu?.position.x)!
locationy2 = (appleNodeCpu?.position.y)!
let dx2 = locationx2 - (appleNodeCpu2?.position.x)!
let dy2 = locationy2 - (appleNodeCpu2?.position.y)!
let angle2 = atan2(dy2, dx2)
let vx2 = cos(angle2) * appleNodeCpu2Speed
let vy2 = sin(angle2) * appleNodeCpu2Speed
然后移动船:
moveAnalogStick.beginHandler = { [unowned self] in
guard self.appleNode != nil else {
return
}
}
moveAnalogStick.trackingHandler = { [unowned self] jData in
let rotateToAngle = SKAction.rotate(toAngle: jData.angular, duration:0.25, shortestUnitArc:true)
self.appleNode?.run(rotateToAngle)
guard let aN = self.appleNode else {
return
}
let when = DispatchTime.now() + 0.25 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
aN.position = CGPoint(x: aN.position.x + (jData.velocity.x * 0.12), y: aN.position.y + (jData.velocity.y * 0.12))
}
}
moveAnalogStick.stopHandler = { [unowned self] in
guard self.appleNode != nil else {
return
}
}
这只是我将船只添加到场景中的代码,它的移动效果很好,但是,我不知道如何创建这些不同的联系人。抱歉:(
感谢您的帮助!!!!
最佳答案
您可以向每艘船添加 SKPhysicsBody,然后使用提供的内置接触和碰撞检测机制,请参阅 Working With Collisions and Contacts .
在更简单的层面上,您可以使用 SKNode.intersects 来测试两个节点何时相交。方法。
关于ios - 节点的不同侧有不同的触点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47585237/
我是一名优秀的程序员,十分优秀!