我无法为我的生活找到解决方案来解决这个问题.有没有人有任何想法?
Ball节点代码:
func createBallNode(ballcolor: String) -> SKSpriteNode { let ball = SKSpriteNode(imagenamed: ballcolor) ball.position = CGPoint(x: CGRectGetMIDX(frame),y: CGRectGetMIDY(frame)+30) ball.zposition = 1 ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.wIDth/2) ball.physicsBody?.affectedByGravity = true ball.physicsBody?.restitution = 1 ball.physicsBody?.lineardamPing = 0 ball.physicsBody?.friction = 0 ball.physicsBody?.categoryBitMask = CollIDerType.Ball.rawValue ball.physicsBody?.contactTestBitMask = CollIDerType.Rect.rawValue ball.physicsBody?.collisionBitMask = CollIDerType.Rect.rawValue let centerX = ball.position.x let range = SKRange(lowerlimit: centerX,upperlimit: centerX) let constraint = SKConstraint.positionX(range) ball.constraints = [constraint] return ball}解决方法 是的,问题可能是由于球没有完全“对齐”时击中六边形.在这种情况下,球失去垂直速度而有利于水平轴.
既然你想要一个“离散逻辑”我相信这个场景你应该避免物理学(至少对于d跳部分).重复垂直移动球的SKAction会容易得多.
例
我准备了一个简单的例子
class GameScene: SKScene { overrIDe func dIDMove(to vIEw: SKVIEw) { super.dIDMove(to: vIEw) let ball = createBallNode() self.addChild(ball) } func createBallNode() -> SKSpriteNode { let ball = SKSpriteNode(imagenamed: "ball") ball.position = CGPoint(x: frame.mIDX,y: frame.minY + ball.frame.height / 2) let goUp = SKAction.move(by: CGVector(dx: 0,dy: 600),duration: 1) goUp.timingMode = .eaSEOut let godown = SKAction.move(by: CGVector(dx: 0,dy: -600),duration: 1) godown.timingMode = .easeIn let goUpAndDown = SKAction.sequence([goUp,godown]) let forever = SKAction.repeatForever(goUpAndDown) ball.run(forever) return ball }}
更新
如果您需要在每次球接触Hexagon底座时执行检查,您可以使用此代码
class GameScene: SKScene { overrIDe func dIDMove(to vIEw: SKVIEw) { super.dIDMove(to: vIEw) let ball = createBallNode() self.addChild(ball) } func createBallNode() -> SKSpriteNode { let ball = SKSpriteNode(imagenamed: "ball") ball.position = CGPoint(x: frame.mIDX,duration: 1) godown.timingMode = .easeIn let check = SKAction.customAction(withDuration: 0) { (node,elapsedtime) in self.balltouchesBase() } let goUpAndDown = SKAction.sequence([goUp,godown,check]) let forever = SKAction.repeatForever(goUpAndDown) ball.run(forever) return ball } private func balltouchesBase() { print("The ball touched the base") }}
正如您现在所看到的,每次球是一个较低的y坐标时,都会调用balltouchesBase方法.这是添加六边形颜色检查的正确位置.
总结以上是内存溢出为你收集整理的ios – SKSpriteNode在碰撞时失去速度全部内容,希望文章能够帮你解决ios – SKSpriteNode在碰撞时失去速度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)