gpt4 book ai didi

xcode - 如何停止对 SKSpriteNode 施加力?

转载 作者:行者123 更新时间:2023-11-30 13:34:42 25 4
gpt4 key购买 nike

我有几个彼此相邻排列的小桨,尺寸约为 50x10,不断地从左到右、从右到左移动,它们也会时不时地随机停止移动并变得静止。有一个球朝他们落下,当击中桨时,如果桨没有移动并且静止,球会从桨上弹开并直接向上飞去。然而,如果 Racket 正在移动并且球在此期间击中其中一个 Racket ,则球不会击中 Racket 并向右返回,而是击中 Racket 并开始向右或向左移动(取决于 Racket 的方向)移动)同时返回。我试图让球立即返回,即使它在移动时撞到 Racket (几乎是一个永远在 Racket 上弹跳的球)。

我尝试了几种不同的方法来解决这个问题,但我认为我错过了一些重要的事情,使其表现得像这样。这是代码:

class GameScene: SKScene, SKPhysicsContactDelegate {
let greenPaddle = SKSpriteNode(imageNamed: "Green")
var ball = SKSpriteNode()

let ballCategory: UInt32 = 1 << 0
let greenPaddleCategory: UInt32 = 1 << 1

override func didMoveToView(view: SKView) {

self.physicsWorld.contactDelegate = self

greenPaddle.position = CGPoint(x: 0, y: frame.size.height / -6.5)
greenPaddle.physicsBody = SKPhysicsBody(rectangleOfSize: greenPaddle.size)
greenPaddle.physicsBody!.affectedByGravity = false
greenPaddle.physicsBody!.dynamic = false

greenPaddle.physicsBody!.categoryBitMask = greenPaddleCategory
greenPaddle.physicsBody!.contactTestBitMask = ballCategory


ball = SKSpriteNode(imageNamed: "Red")
ball.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2)
ball.physicsBody!.friction = 0
ball.physicsBody!.restitution = 1
ball.physicsBody!.linearDamping = 0
ball.physicsBody!.allowsRotation = false

ball.physicsBody!.categoryBitMask = ballCategory
ball.physicsBody!.contactTestBitMask = greenPaddleCategory

addChild(circle)
}

func didBeginContact(contact: SKPhysicsContact) {

let bodyA = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyA : contact.bodyB
let bodyB = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? contact.bodyB : contact.bodyA

if (bodyA.categoryBitMask) == ballCategory && (bodyB.categoryBitMask) == greenPaddleCategory {

greenPaddle.physicsBody!.friction = 0
greenPaddle.physicsBody!.restitution = 0
}

}

最佳答案

我不太明白这个问题;但是,您忘记在 didmovetoview 中设置物理世界。

 self.physicsWorld.contactDelegate = self

如果问题是联系人不起作用,这会起作用:)

关于xcode - 如何停止对 SKSpriteNode 施加力?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36213596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com