2016-11-23 52 views
0

我正在嘗試使用傾斜屏幕來移動播放器的選項。所以我遵循了Ray Wenderlich和他的太空侵略者教程的指示。我似乎無法弄清楚爲什麼我的播放器在傾斜屏幕時沒有從左向右移動。爲什麼我的播放器在傾斜時無法在SpriteKit中移動?

請看看它來幫助我。

import SpriteKit 
import CoreMotion 

enum BodyType: UInt32 { 
case player = 2 
case enemy = 3 
} 
let motionManager = CMMotionManager() 

class GameScene: SKScene, SKPhysicsContactDelegate { 

let points = SKLabelNode(text: "0") 

let gamePlayerSize = CGSize(width: 30, height: 16) 
let gamePlayerName = "gameplayer" 


override func didMove(to view: SKView) { 


//SET UP FRAME 
physicsBody = SKPhysicsBody(edgeLoopFrom: frame) 

//SETTING UP Player 
func setupPlayer() { 
    // 1 
    let player = makePlayer() 

    // 2 
    player.position = CGPoint(x: 240, y: 28) 
    addChild(player) 
} 

func makePlayer() -> SKNode { 
    let player = SKSpriteNode(imageNamed: "block") 
    player.name = gamePlayerName 

    // 1 
    player.physicsBody = SKPhysicsBody(rectangleOf: player.frame.size) 

    // 2 
    player.physicsBody!.isDynamic = true 

    // 3 
    player.physicsBody!.affectedByGravity = false 

    // 4 
    player.physicsBody!.mass = 0.02 


    return player 
} 

setupPlayer() 

motionManager.startAccelerometerUpdates() 

//score label 
let points = SKLabelNode(text: "0") 
points.position = CGPoint(x: 280, y: 510) 
points.fontColor = UIColor.black 
points.fontSize = 50 
addChild(points) 

// setting border around game 
let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame) 
borderBody.friction = 0 
self.physicsBody = borderBody 

//ENEMY SETTINGS START 

//repeat enemy spawning 
run(SKAction.repeatForever(
    SKAction.sequence([ 
     SKAction.run(spawnEnemy), 
     SKAction.wait(forDuration: 1.0)]))) 

} 
//Enemy settings 
func random() -> CGFloat { 
    return CGFloat(Float(arc4random())/0xFFFFFFFF) 
} 

func random(min: CGFloat, max: CGFloat) -> CGFloat { 
    return random() * (max - min) + min 
} 

//spawn enemies 
func spawnEnemy() { 
    // 2 
    let enemy = SKSpriteNode(imageNamed: "ball") 
    // 3 
    enemy.name = "enemy" 
    // 4 
    enemy.position = CGPoint(x: frame.size.width * random(min: 0, max: 1), y: frame.size.height + enemy.size.height/2) 
    // 5 
    addChild(enemy) 

    enemy.run(
     SKAction.moveBy(x: 0.0 , y: -size.height - enemy.size.height, 
         duration: TimeInterval(random(min: 1, max: 2)))) 

//ENEMY SETTINGS END 

    func processUserMotion(forUpdate currentTime: CFTimeInterval) { 
     // 1 
     if let player = childNode(withName: gamePlayerName) as? SKSpriteNode { 
      // 2 
      if let data = motionManager.accelerometerData { 
       // 3 
       if fabs(data.acceleration.x) > 0.2 { 
        // 4 How do you move the ship? 
        player.physicsBody!.applyForce(CGVector(dx: 40 * CGFloat(data.acceleration.x), dy: 0)) 
       } 
      } 
     } 
    } 


func update(_ currentTime: TimeInterval) { 
    processUserMotion(forUpdate: currentTime) 
} 
    } } 

回答

0

您是否意外嵌套了您的方法?看你的代碼似乎是如此。

您是否檢查運動代碼是否實際調用?

像這樣的東西應該用你的物理身體,而不是力展開他們在工作時

class GameScene: SKScene { 

    let motionManager = CMMotionManager() 

    override func didMove(to view: SKView) { 
     // setup player etc 
     motionManager.startAccelerometerUpdates() 
    } 

    func update(_ currentTime: TimeInterval) { 
      processUserMotion(forUpdate: currentTime) 
    } 
} 

而且作爲小費,我會用自選(?)(!)。如果它在一點 變爲零,則在強制解包時會崩潰。

e.g

player.physicsBody?.affectedByGravity = false 

希望這有助於

相關問題