【发布时间】:2015-07-19 15:16:04
【问题描述】:
我有一个 spriteNode,它有一个黑色圆圈的默认纹理,我把它放在屏幕的中心。我还有一个包含 4 个纹理的数组。我想要做的是当我点击屏幕时,中心的黑色圆圈随机从数组中选择一个 SKTexture 并更改为设置的纹理。我一直在思考 didBeginTouches 中的代码行,但我一直坚持如何真正执行这个想法。谢谢你的帮助。 :)
var array = [SKTexture(imageNamed: "GreenBall"), SKTexture(imageNamed: "RedBall"), SKTexture(imageNamed: "YellowBall"), SKTexture(imageNamed: "BlueBall")]
override func didMoveToView(view: SKView) {
var choiceBallImg = SKTexture(imageNamed: "BlackBall")
choiceBall = SKSpriteNode(texture: choiceBallImg)
choiceBall.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
self.addChild(choiceBall)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
choiceBall.texture = SKTexture(imageNamed: arc4random(array))
//error: Cannot assign a value of type 'SKTexture!' to a value of type 'SKTexture?'
}
【问题讨论】:
标签: arrays xcode swift sprite-kit arc4random