gpt4 book ai didi

ios - 我的节点没有发生冲突

转载 作者:行者123 更新时间:2023-11-30 12:11:27 24 4
gpt4 key购买 nike

class GameScene: SKScene, SKPhysicsContactDelegate {

let balls = [
SKSpriteNode(imageNamed: "blueball.png"),
SKSpriteNode(imageNamed: "greenball.png"),
SKSpriteNode(imageNamed: "realredball.png")
]


let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png")
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png")
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png")

override func didMove(to view: SKView) {
spawnBalls()
rectangles()
physicsWorld.contactDelegate = self

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for ball in balls{
ball.physicsBody = SKPhysicsBody()
ball.physicsBody?.affectedByGravity = true
}
}

func spawnBalls() {

let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.physicsBody = SKPhysicsBody()
ball.physicsBody?.affectedByGravity = false
ball.position = CGPoint(x: 0, y: 250)
ball.size = CGSize(width: 70, height: 70)
ball.physicsBody?.categoryBitMask = 0
ball.physicsBody?.collisionBitMask = 1
self.addChild(ball)
}

func rectangles() {
redRectangle.position = CGPoint(x: -316.5, y: -657)
redRectangle.size = CGSize(width: 400, height: 20)
redRectangle.physicsBody = SKPhysicsBody()
redRectangle.physicsBody?.categoryBitMask = 1
redRectangle.physicsBody?.collisionBitMask = 0
blueRectangle.size = CGSize(width: 400, height: 20)
blueRectangle.position = CGPoint(x: -100, y: -657)
blueRectangle.physicsBody = SKPhysicsBody()
greenRectangle.position = CGPoint(x: 0, y: -657)
greenRectangle.size = CGSize(width: 400, height: 20)
greenRectangle.physicsBody = SKPhysicsBody()
self.addChild(redRectangle)
self.addChild(blueRectangle)
self.addChild(greenRectangle)
}

}

我希望球落下并与矩形节点接触,尽管看起来球只是穿过它们。我使用了碰撞和类别位掩码。我想知道是否有人可以帮助解决这个问题,谢谢。

最佳答案

您正确初始化了球,但通过在 touchesBegan(_:) 中创建一个新的球体来删除它们的物理体。只需从 touchesBegan(_:) 函数中删除这一行:

 ball.physicsBody = SKPhysicsBody()

关于ios - 我的节点没有发生冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003229/

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