嘗試使用Sprite Kit製作我的第一個Swift應用程序。 應用程序必須創建最多5個必須隨機移動屏幕的對象。速度增加,尺寸每5秒減少一次。 場景設置正確,對象出現並移動,但一段時間後CPU使用率變爲100%,FPS降至0,應用程序掛起。有時它會繼續工作幾秒鐘,有時它不會。 是什麼原因?Sprite Kit應用程序掛起
我只是在Xcode noob - 在模擬過程中是否有可能找到哪些代碼塊正在工作?
以下是更新功能。我沒有包括didMoveToView
和touchesBegan
部分原因他們似乎沒有影響到這個問題。
override func update(currentTime: CFTimeInterval) {
if (currentTime-oldTime)>1 {
if arrayofAims.isEmpty==false {
for currentAim in arrayofAims {
repeat {xvelocity=(Int(arc4random())%3 - 1) * aimSpeedX} while xvelocity == 0
repeat {yvelocity=(Int(arc4random())%3 - 1) * aimSpeedX} while yvelocity == 0
currentAim.physicsBody?.applyForce(CGVectorMake (CGFloat(xvelocity), CGFloat(yvelocity)))
}
}
oldTime=currentTime
timecount+=1
}
if timecount>5 {
if aimSpeedX < 5000 {
aimSpeedX+=200
}
if aimSizeRatioX > 0.3 {
aimSizeRatioX -= 0.05
aimSizeRatioY -= 0.05
}
timecount=0
if arrayofAims.count<6 { arrayofAims.append(createAnAim())
currentAim=arrayofAims[arrayofAims.count-1]
addChild(currentAim)
}
}
}
歡迎互動,(因此)!請閱讀[問]部分。順便說一句,還沒有人回答,所以你感謝誰? –
作爲最原始的調試方式,嘗試刪除部分代碼以隔離問題。因此,首先刪除'addChild(currentAim)'行並等待問題是否仍然存在。你完全確定更新方法是負責任的嗎?啓用調試標籤以顯示draws count('skView.showsDrawsCount')和節點數('skView.showsNodesCount')。當應用程序卡住時,您會看到哪些數字? – Whirlwind
或者用最小的例子更新你的問題,它可以重現你遇到的問題(甚至上傳一個github回購)。 – Whirlwind