在编写SKAction.animateWithTextures(...)时,我遇到了这个错误('字符串'不能转换为' DictionaryIndex')当我使用我的&的字符串键时#39;状态'检索SKTexture相关。有人可以解释可能导致错误的原因吗?
import SpriteKit
class Player {
let states: Dictionary<String, SKTexture> = [
"standing": SKTexture(imageNamed: "standing"),
"right1": SKTexture(imageNamed: "right1"),
"right2": SKTexture(imageNamed: "right2"),
"right3": SKTexture(imageNamed: "right3"),
"left1": SKTexture(imageNamed: "left1"),
"left2": SKTexture(imageNamed: "left2"),
"left3": SKTexture(imageNamed: "left3")
]
let LEFT: UInt32 = 1 << 0
let FRONT: UInt32 = 1 << 1
let RIGHT: UInt32 = 1 << 2
var node: SKSpriteNode!
var status: UInt32!
let delay = SKAction.waitForDuration(NSTimeInterval(0.1))
var runLeft: SKAction!
var runRight: SKAction!
init(){
for state in states {
state.1.filteringMode = .Nearest
}
node = SKSpriteNode(texture: states["standing"])
node.physicsBody = SKPhysicsBody(rectangleOfSize: node.size)
node.physicsBody.dynamic = false
status = FRONT
runLeft = SKAction.animateWithTextures([states["left1"], states["left2"], states["left1"], states["left3"]]!, timePerFrame: 0.1)
runRight = SKAction.animateWithTextures([states["right1"], states["right2"], states["right1"], states["right3"]]!, timePerFrame: 0.1)
}
}
答案 0 :(得分:0)
字典查找返回选项,因为密钥可能不存在。您需要打开每个查找,因为它们是类型SKTexture!
:
runLeft = SKAction.animateWithTextures([states["left1"]!, states["left2"]!, states["left1"]!, states["left3"]!], timePerFrame: 0.1)
runRight = SKAction.animateWithTextures([states["right1"]!, states["right2"]!, states["right1"]!, states["right3"]!], timePerFrame: 0.1)