2015-07-03 15 views
0

我在一個精靈套件中製作遊戲,爲了移動我的角色,我正在應用一個衝動,但這是一個問題,因爲這會讓他每移動50個像素左右。我的問題是,有人可以告訴我如何製作一個簡單的角色控制器,這樣我的角色就​​可以持續移動,直到用戶放開屏幕才能離開。如何在SpriteKit中製作一個簡單的字符控制器?

我GameScene.swift文件:完成此

import SpriteKit 


class GameScene: SKScene, SKPhysicsContactDelegate { 

let character = SKSpriteNode(texture: SKTexture(imageNamed: "character")) 
var move = false 

override func didMoveToView(view: SKView) { 
    /* Setup your scene here */ 
    //world 
    self.physicsWorld.contactDelegate = self 
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 


    //character 
    character.position = CGPointMake(self.frame.size.width * 0.6, self.frame.size.height * 0.6) 
    character.setScale(0.2) 
    character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size) 
    character.physicsBody?.dynamic = true 
    character.physicsBody?.allowsRotation = false 
    self.addChild(character) 
    character.physicsBody?.affectedByGravity = true 




    //platform 1 
    var platform = SKSpriteNode(texture: SKTexture(imageNamed: "platform")) 
    platform.position = CGPointMake(self.frame.size.width * 0.6, CGRectGetMidY(self.frame)) 
    platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size) 
    platform.physicsBody?.dynamic = false 
    platform.setScale(0.25) 
    platform.physicsBody?.friction = 1 
    platform.physicsBody?.restitution = 0 
    platform.physicsBody?.linearDamping = 0 
    self.addChild(platform) 

    //platform 2 
    var platformTexture2 = SKTexture(imageNamed: "platform") 
    var platform2 = SKSpriteNode(texture: platformTexture2) 
    platform2.position = CGPointMake(self.frame.size.width * 0.4, self.frame.size.height * 0.3) 
    platform2.physicsBody = SKPhysicsBody(rectangleOfSize: platform2.size) 
    platform2.physicsBody?.dynamic = false 
    platform2.setScale(0.25) 
    platform2.physicsBody?.friction = 1 
    platform2.physicsBody?.restitution = 0 
    platform2.physicsBody?.linearDamping = 0 
    self.addChild(platform2) 


    //platform main 
    var platformTexture3 = SKTexture(imageNamed: "platform") 
    var platform3 = SKSpriteNode(texture: platformTexture2) 
    platform3.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + platform3.size.height/3) 
    platform3.physicsBody = SKPhysicsBody(rectangleOfSize: platform3.size) 
    platform3.physicsBody?.dynamic = false 
    platform3.setScale(1) 
    platform3.size.width = platform3.size.width * CGFloat(2.0) 
    platform3.physicsBody?.friction = 1 
    platform3.physicsBody?.restitution = 0 
    platform3.physicsBody?.linearDamping = 0 
    self.addChild(platform3) 

    } 

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    /* Called when a touch begins */ 


    for touch: AnyObject in touches { 
    let location = touch.locationInNode(self) 

     if location.x < CGRectGetMidX(self.frame){ 
      character.physicsBody?.applyImpulse(CGVector(dx: -50, dy: 0)) 
     } else if location.x > CGRectGetMidX(self.frame){ 
      character.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 0)) 
         } 
    } 



    func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
     character.physicsBody?.velocity = CGVector(dx: 0, dy: 0) 
} 




func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 


    } 
    }; 
} 

回答

0

一種方式是維持一個變量來跟蹤觸摸事件(左,右,或無)的狀態。以下是基本步驟:

  1. touchesBegan,設置觸摸狀態變量向左或向右
  2. update,不斷應用的衝動,同時用戶在觸摸屏幕
  3. touchesEnded,復位觸摸狀態變量

下面是如何實現這樣的一個例子...

以上GameScene類的定義,添加以下

enum TouchState { 
    case Left 
    case Right 
    case None 
} 

GameScene,添加以下變量

var touchLocation:TouchState = .None 

GameScene,添加或更換以下方法

override func update(currentTime: CFTimeInterval) { 
    // Apply impulse to physics body while users is touching the screen 
    switch (touchState) { 
    case .Left: 
     character.physicsBody?.applyImpulse(CGVector(dx: -1, dy: 0)) 
    case .Right: 
     character.physicsBody?.applyImpulse(CGVector(dx: 1, dy: 0)) 
    case .None: 
     break 
    } 
} 

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    for touch:AnyObject in touches { 
     let location = touch.locationInNode(self) 
     if location.x < CGRectGetMidX(self.frame) { 
      touchState = .Left 
     } else if location.x > CGRectGetMidX(self.frame) { 
      touchState = .Right 
     } 
    } 
} 

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    // Update touch status 
    touchState = .None 
} 

這裏的GameScene.swift .. 。

import SpriteKit 

enum TouchLocation { 
    case Left 
    case Right 
    case None 
} 

class GameScene: SKScene, SKPhysicsContactDelegate { 
    var character = SKSpriteNode(imageNamed: "character") 

    var touchLocation:TouchLocation = .None 

    override func didMoveToView(view: SKView) { 
     /* Setup your scene here */ 
     scaleMode = .ResizeFill 

     //character 
     character.position = CGPointMake(view.frame.size.width * 0.5, view.frame.size.height * 0.5) 
     character.setScale(0.1) 
     character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size) 
     character.physicsBody?.dynamic = true 
     character.physicsBody?.allowsRotation = false 
     self.addChild(character) 
     character.physicsBody?.affectedByGravity = true 


     //platform 1 
     var platform = SKSpriteNode(texture: SKTexture(imageNamed: "platform")) 
     platform.position = CGPointMake(view.frame.size.width * 0.6, CGRectGetMidY(view.frame)) 
     platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size) 
     platform.physicsBody?.dynamic = false 
     platform.setScale(0.25) 
     platform.physicsBody?.friction = 1 
     platform.physicsBody?.restitution = 0 
     platform.physicsBody?.linearDamping = 0 
     self.addChild(platform) 

     //platform 2 
     var platformTexture2 = SKTexture(imageNamed: "platform") 
     var platform2 = SKSpriteNode(texture: platformTexture2) 
     platform2.position = CGPointMake(view.frame.size.width * 0.4, view.frame.size.height * 0.3) 
     platform2.physicsBody = SKPhysicsBody(rectangleOfSize: platform2.size) 
     platform2.physicsBody?.dynamic = false 
     platform2.setScale(0.25) 
     platform2.physicsBody?.friction = 1 
     platform2.physicsBody?.restitution = 0 
     platform2.physicsBody?.linearDamping = 0 
     self.addChild(platform2) 


     //platform main 
     var platformTexture3 = SKTexture(imageNamed: "platform") 
     var platform3 = SKSpriteNode(texture: platformTexture2) 
     platform3.position = CGPointMake(CGRectGetMidX(view.frame), CGRectGetMinY(view.frame) + platform3.size.height/3) 
     platform3.physicsBody = SKPhysicsBody(rectangleOfSize: platform3.size) 
     platform3.physicsBody?.dynamic = false 
     platform3.setScale(1) 
     platform3.size.width = platform3.size.width * CGFloat(2.0) 
     platform3.physicsBody?.friction = 1 
     platform3.physicsBody?.restitution = 0 
     platform3.physicsBody?.linearDamping = 0 
     self.addChild(platform3) 
    } 

    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 
     switch (touchLocation) { 
     case .Left: 
      character.physicsBody?.applyImpulse(CGVector(dx: -1, dy: 0)) 
     case .Right: 
      character.physicsBody?.applyImpulse(CGVector(dx: 1, dy: 0)) 
     case .None: 
      break 
     } 
    } 

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
     for touch:AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.x < CGRectGetMidX(self.frame) { 
       touchLocation = .Left 
      } else if location.x > CGRectGetMidX(self.frame) { 
       touchLocation = .Right 
      } 
     } 
    } 

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
     touchLocation = .None 
    } 
} 
+0

當我寫在更新功能switch語句中我得到一個錯誤的說法,「枚舉Case模式無法比擬的非枚舉類型‘<>’的值」 –

+0

你確定你複製/粘貼確切的代碼到你的GameScene? – 0x141E

+0

是的,你認爲你可以粘貼整個文件,如果你有它,所以我不會搞砸了嗎? –

相關問題