将var存储在数组中

时间:2015-07-24 13:22:11

标签: arrays swift textures

我是否必须将数组设置为特定类型,以便它将存储图像或是其他内容。图像存储在Images.xcassets

class GameScene: SKScene {

   var blueTexture = SKTexture(imageNamed: "BlueBall.png")
   var greenTexture = SKTexture(imageNamed: "GreenBall.png")
   var redTexture = SKTexture(imageNamed: "redTexture.png")

   var array = [blueTexture, greenTexture, redTexture]
   //error: GameScene does not have a member called blueTexture
   //this goes for the other textures as well

1 个答案:

答案 0 :(得分:1)

这是因为您在初始化期间无法访问其他属性,因此您可以用以下代码替换当前代码:

var blueTexture = SKTexture(imageNamed: "BlueBall.png")
var greenTexture = SKTexture(imageNamed: "GreenBall.png")
var blueTexture = SKTexture(imageNamed: "BlueBall.png")

var array:[SKTexture]!
override func didMoveToView(view: SKView) 
{
    array = [blueTexture,greenTexture,blueTexture]      
}

另外,你做了两个blueTextures