Swift-SpriteKit浮动气泡粘在角落

我正在尝试在我的 Watch 应用上创建一个浮动气泡视图。气泡可以相互碰撞和弹开,也可以从屏幕的两侧弹开。但出于某种原因,气泡出现在视野范围之外并卡在框架的两侧而不是弹开。此代码在我的 iOS 应用程序上按预期工作,但在我的 Watch 应用程序中使用相同的代码时,它不会。

这个确切的代码在我的 iOS 应用程序上完美运行对我来说没有多大意义,但在 Watch 应用程序上却没有。

我将以下代码传递到SpriteView我的 SwiftUI 视图中

let ballCategory: UInt32 = 0xb0001
let edgeCategory: UInt32 = 0xb0010
var nodeCount = 0

override func sceneDidLoad() {
    //set physicsWorld properties
    physicsWorld.contactDelegate = self
    physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)

    //set edges as PhysicsBody
    let edge = SKPhysicsBody(edgeLoopFrom: self.frame)
    edge.friction = 0
    edge.categoryBitMask = edgeCategory
    self.physicsBody = edge

    makebubble()
    makebubble()
    makebubble()
    makebubble()
}

func makebubble() {

    let bubbleTexture = SKTexture(imageNamed: "bubble")
    let bubble = SKSpriteNode(texture: bubbleTexture)
    let bphysicsBody = SKPhysicsBody(circleOfRadius: bubbleTexture.size().height/2)

    bphysicsBody.isDynamic = true
    bphysicsBody.usesPreciseCollisionDetection = true
    bphysicsBody.restitution = 0.5
    bphysicsBody.friction = 0
    bphysicsBody.angularDamping = 0
    bphysicsBody.linearDamping = 0
    bphysicsBody.categoryBitMask = ballCategory
    bphysicsBody.collisionBitMask = ballCategory | edgeCategory
    bphysicsBody.contactTestBitMask = ballCategory | edgeCategory
    bubble.physicsBody = bphysicsBody
    bubble.name = "bubble"

    // Get a random possition within the width of the scene
    let x = CGFloat(randomize(number: Int(size.width - 40)))
    let y = CGFloat(randomize(number: Int(size.height - 40)))

    // position the bubble
    bubble.position.x = x
    bubble.position.y = y

    // Add the bubble
    addMyChild(node: bubble)
}

func addMyChild(node:SKSpriteNode){
    self.addChild(node)
    node.physicsBody!.applyImpulse(CGVector(dx: 10.0, dy: -2.0))
    nodeCount += 1
}

// function that returns a random int from 0 to n-1
func randomize(number: Int) -> Int{
    return Int(arc4random()) % number
}

以上是Swift-SpriteKit浮动气泡粘在角落的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>