我正在尝试编写一个滚动模具的例程。在登陆最终号码之前,我希望脸部多次改变。下面的代码不显示模具更换面。我添加了睡眠声明,希望它能给它时间更新,但它只是保留在初始面部直到结束,然后显示最后的面孔。更改纹理后是否有要添加的语句以强制视图更新?
func rollDice() {
var x: Int
for var i = 0; i < 50; i++
{
x = GKRandomSource.sharedRandom().nextIntWithUpperBound(6)
let texture = dice[0].faces[x]
let action:SKAction = SKAction.setTexture(texture)
pieces[3].runAction(action)
pieces[3].texture = dice[0].faces[x]
NSThread.sleepForTimeInterval(0.1)
print(x)
}
}
答案 0 :(得分:0)
正如评论中指出的那样,屏幕只在主线程上重绘。因此,您可以让骰子卷在后台线程上进行,并在主线程上重绘屏幕:
def jsonXForm(value: String) = (__ \ "customerId").json.update(
(__ \ "customerId").json.put(JsString(value))
)
json.transform(jsonXForm(yourNewValue)) match {...}`
答案 1 :(得分:0)
感谢您的帮助。在阅读了一些关于计时器之后,我想出了以下代码:
func rollDice() {
rollCount = 0
timer = NSTimer.scheduledTimerWithTimeInterval(0.05, target:self, selector: "changeDie", userInfo: nil, repeats: true)
}
func changeDie () {
x = GKRandomSource.sharedRandom().nextIntWithUpperBound(6)
print(x)
let texture = dice[0].faces[x]
let action:SKAction = SKAction.setTexture(texture)
pieces[3].runAction(action)
++rollCount
if rollCount == 20 {
timer.invalidate()
}
}