我目前正在通过遵循教程使用Swift制作应用程序。当我在XCode中运行代码时,我得到标题中声明的错误。这是我的代码:
import SpriteKit
class GameScene: SKScene {
var currentLevel = 0
var currentLevelData:NSDictionary
var levels:Levels
var bg:SKSpriteNode
var man:SKSpriteNode
var levelType:SKSpriteNode
var manSpeed = 10
var runningManTextures = [SKTexture]()
var stageMaxLeft:CGFloat = 0
var stageMaxRight:CGFloat = 0
var x: CGFloat = 0
var manLeft = false
var manRight = false
override func didMoveToView(view: SKView) {
stageMaxRight = self.size.width / 2
stageMaxLeft = -stageMaxRight
loadLevel()
}
func loadLevel() {
currentLevelData = levels.data[currentLevel]
loadBackground()
loadLevelType()
loadMan()
}
func loadBackground() {
var bgNum = currentLevelData["bg"] as! String
bg = SKSpriteNode(imageNamed: "bg_\(bgNum)")
bg.name = "bg"
bg.zPosition = 1.0
scene!.addChild(bg)
}
func loadLevelType() {
var levelTypeInfo = currentLevelData["type"] as! String
levelType = SKSpriteNode(imageNamed: levelTypeInfo)
levelType.name = "level_type"
levelType.position.y = -140
levelType.zPosition = 2.0
scene!.addChild(levelType)
}
func loadMan() {
loadManTextures()
man.position.y -= man.size.height/2
man.position.x = -(scene!.size.width/2) + man.size.width * 2
man.zPosition = 3.0
addChild(man)
}
func loadManTextures() {
var runningAtlas = SKTextureAtlas(named: "run")
for i in 1...3 {
var textureName = "run_\(i)"
var temp = runningAtlas.textureNamed(textureName)
runningManTextures.append(temp)
}
}
func moveMan(direction:String) {
if direction == "left" {
manLeft = true
man.xScale = -1
manRight = false
runMan()
} else {
manRight = true
man.xScale = 1
manLeft = false
runMan()
}
}
func runMan() {
man.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(runningManTextures, timePerFrame: 0.2, resize:false, restore:true)))
}
override func touchesBegan(touches:, Set<NSObject>, withEvent event: UIEvent) {
if event.allTouches()!.count == 1 {
let location = touches.first().locationInNode(self) //Error(2) reads: Cannot invoke 'locationInNode' with an argument list of type '(GameScene)'.
if location.x > 0 {
moveMan("right")
} else {
moveMan("left")
}
} else {
println("jump")
}
}
func cancelMoves() {
manLeft = false
manRight = false
man.removeAllActions()
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
cancelMoves()
}
override init(size: CGSize) {
self.currentLevel = 0
self.currentLevelData = [:]
self.levels = Levels()
self.bg = SKSpriteNode()
self.man = SKSpriteNode(texture: SKTexture(imageNamed: "run_0"))
self.man.name = "man"
self.levelType = SKSpriteNode()
super.init(size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func cleanScreen() {
bg.removeFromParent()
levelType.removeFromParent()
}
func nextScreen(level:Int) -> Bool {
if level >= 0 && level < levels.data.count {
currentLevel = level
currentLevelData = levels.data[currentLevel]
cleanScreen()
loadBackground()
loadLevelType()
return true
}
return false
}
func updateManPosition() {
if man.position.x < stageMaxLeft {
if nextScreen(currentLevel - 1) {
man.position.x = stageMaxRight
}
if manLeft {
return
}
}
if man.position.x > stageMaxRight {
if nextScreen(currentLevel + 1) {
man.position.x = stageMaxLeft
}
if manRight {
return
}
}
if manLeft {
man.position.x -= manSpeed //error here
} else if manRight {
man.position.x += manSpeed //error here also
}
}
override func update(currentTime: CFTimeInterval) {
updateManPosition()
}
}
我知道这可能是一个非常愚蠢的问题,但我很难过。我已经四处搜索了,我发现了一个类似的问题,但解决方案没有用。
答案 0 :(得分:2)
幸运的是,编译器正在告诉你这里的问题究竟是什么,编译器不允许你混合类型。问题出在这里:
var manSpeed = 10
这隐含地将manSpeed创建为Int。所以当你尝试使用CGFloat(它就是man.position.x)时,它会 - 抱怨:
if manLeft {
man.position.x -= manSpeed //error here
} else if manRight {
man.position.x += manSpeed //error here also
}
解决此问题的最佳方法是显式输入manSpeed
var manSpeed: CGFloat = 10.0
这将确保类型匹配