-1
我目前正在使用Swift製作一個應用程序,遵循教程。當我在XCode中運行代碼時,出現標題中指出的錯誤。這是我的代碼:二元運算符' - ='不能應用於類型'CGFloat'和'Int'的operends
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()
}
}
我知道這可能是一個非常愚蠢的問題,但我很難過。我已經搜遍了,我發現了一個類似的問題,但解決方案沒有奏效。
這固定它!我想這個教程是過時的。我完全複製了他所做的一切,但那不是我必須改變的第一件事。 – Dingo
我其實還有一個問題,如果你不介意我的問題。我不想感到麻煩! – Dingo
如果相關,請在此處詢問。否則,請提出一個新問題。 –