在数组中存储变量

Storing var in array

我是否必须将数组设置为特定类型以便它存储图像,或者是其他类型。图像存储在 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

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

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