ios - 无法移动 Sprite

标签 ios iphone swift sprite-kit game-physics

即使在 GameScene 中的 Sprite 上设置类后,我也无法移动该 Sprite。下面是我的 GameplayScene 类的代码。我没有包含我的 Player 类,因为代码非常基本。 (只是一个 movePlayer bool 函数,带有一个基于 x 位置移动的 if 语句)

import SpriteKit

class GameplayScene: SKScene {


var player: Player? // created new varible player for the scene, inherited the player class

var canMove = false // created new varible for letting player move; false by default

var moveLeft = false // created new varible to move player left; false by default

var center: CGFloat? // created new varible to set center of screen


override func didMoveToView(view: SKView) {

    center = (self.scene?.size.width)! / (self.scene?.size.height)! //calculates center of screen

    player.self?.childNodeWithName("Player") as? Player! // adds node "Player" within Player class(Player!)

}

override func update(currentTime: NSTimeInterval) {

    managePlayer() //constantly checks if player can/is moving

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {

        let location = touch.locationInNode(self) // gets location inside scene

        if location.x > center {  // if statement to check if user to taping screen on the right

            moveLeft = false //moves player right

        } else {

            moveLeft = true

        }
    }

    canMove = true 

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    canMove = false //says if player is not touching screen, the player will not move
}

func managePlayer() {

    if canMove {

        player?.movePlayer(moveLeft) //takes the Player varible(which has the class), then grabs the move player func, then checks argument moveLeft
    }

}

}

最佳答案

添加player = self.childNode(withName: "Player") 作为?玩家在 GameplayScene.swift 中执行 didMove

override func didMove(to view: SKView) {
        print("didmove")

        center = (self.scene?.size.width)! / (self.scene?.size.height)!
        player = self.childNode(withName: "Player") as? Player
    }

关于ios - 无法移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928440/

相关文章:

swift - 核心数据 : Can I perform a method on a FetchRequest results as it searches?

ios - ios-输入时替换/删除UITextView中的字符[•]

ios - 将数据设置为类变量(来自 Google Places API)

iphone - SQLite 语法(按引起错误的顺序排列)

javascript - 在 Parse Cloud Code 中将数据附加到数组

ios - 可点击的 UILabel

iphone - UIWebView canGoBack 在使用 POST 请求时突然停止工作

ios - 倒计时期间 iOS UILabel 中的小数点对齐?

ios - iOS 中的 Google 自动完成 API 集成

ios - MonoTouch:iOS 4.3.1 iPad中无法识别的选择器