我正在尝试在我的 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
}
Run Code Online (Sandbox Code Playgroud)
这与 watchOS 无关,而与 Apple Watch 的小屏幕尺寸有关。尝试在 iOS 上使用宽度 150 和高度 150 的框架修饰符运行代码,您就会明白我的意思;气泡可能会粘在一边。
你的气泡看起来像是粘在边缘上,因为它们随着时间的推移而减慢速度(由于恢复值为 0.5 而不是 1),而且从统计角度来看,气泡的最终移动接近屏幕边缘的可能性更大(因为它们最终会移动到边缘,从边缘反弹,从而减慢速度并最终停止)。
为此,您可以采取以下 3 项措施:
update(_:)| 归档时间: |
|
| 查看次数: |
130 次 |
| 最近记录: |