2015-12-23 80 views
0

我想使用Swift在Xcode中製作一個簡單的平臺遊戲,這樣當我按住屏幕的左側時,它會不斷向左移動,直到放開,反之亦然。我已經添加了跳轉功能,但如果您有任何其他建議,我願意接受建議。如何使用觸控快速製作角色控制器?

這裏是我的GameController.swift代碼: 進口SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate { 
let character = SKSpriteNode(imageNamed:"square.png") 
let block = SKSpriteNode(imageNamed: "square.png") 
    override func didMoveToView(view: SKView) { 
    /* Setup your scene here */ 
    //Physics 
    self.physicsWorld.gravity = CGVectorMake(0.0, -5.0) 
    self.physicsWorld.contactDelegate = self 
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 
    //Character 
    character.position = CGPoint(x:CGRectGetMidX(self.frame), 
y:CGRectGetMidY(self.frame)) 
    character.setScale(0.25) 
    character.physicsBody = SKPhysicsBody(rectangleOfSize: character.size) 
     self.addChild(character) 

     //block 

     block.position = CGPoint(x: CGRectGetMidX(self.frame), y: 
     CGRectGetMidY(self.frame)*0.3) 
     block.setScale(0.2) 
     block.physicsBody = SKPhysicsBody(rectangleOfSize: block.size) 
     block.physicsBody?.dynamic = false 
     self.addChild(block) 


} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    /* Called when a touch begins */ 
    for touch:AnyObject in touches { 
     let location = touch.locationInNode(self) 

     if location.x < CGRectGetMidX(self.frame) && location.y <  
    CGRectGetMidY(self.frame)*0.7{ 
      character.physicsBody?.applyImpulse(CGVector(dx: -50, dy: 0)) 
     } else if location.x > CGRectGetMidX(self.frame) && location.y <   
    CGRectGetMidY(self.frame)*0.7{ 
      character.physicsBody?.applyImpulse(CGVector(dx: 50, dy: 0)) 
     } else if location.y > character.position.y + 15 { 
      character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 50)) 
     } 





     func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 

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

只要有它,當觸摸位置在touchesBegan位於屏幕的左側時(例如),開始一個永遠運行的SKAction,將字符向左移動,直到觸摸被釋放 - 在' touchesEnded',移除SKAction。這同樣適用於屏幕的右半邊,等等。有意義還是你希望我發佈一個完整的答案? - 這就是你問的,對吧? – Dopapp

+0

是否有任何特定的原因,你在'physicsBody'上使用'.applyImpulse'而不是添加'SKAction'? – Dopapp

+0

我不知道還有什麼是我真正的唯一原因... –

回答

0
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    character.removeActionForKey("moveAction") 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch:AnyObject in touches { 
     let location = touch.locationInNode(self) 

     if location.x < CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{ 
      moveAction = SKAction.repeatActionForever(SKAction.moveByX(-20, y: 0, duration: 0.1)) 
      let character.runAction(moveAction, withKey: "moveAction") 
     } else if location.x > CGRectGetMidX(self.frame) && location.y < CGRectGetMidY(self.frame)*0.7{ 
      let moveAction = SKAction.repeatActionForever(SKAction.moveByX(20, y: 0, duration: 0.1)) 
      character.runAction(moveAction, withKey: "moveAction") 
     } else if location.y > character.position.y + 15 { 
      let moveAction = SKAction.repeatActionForever(SKAction.moveByX(0, y: 20, duration: 0.1)) 
      character.runAction(moveAction, withKey: "moveAction") 
     } 
    } 
} 

我編輯了touchesBegan方法和我添加了一個touchesEnded方法。你似乎試圖嵌入touchesEnded(和update)到touchesBegan,你絕對不能這樣做。

好了,所以,代碼是不言自明的,但無論如何:

而不是你有什麼用載體,我補充說,一直運行的SKAction。改變持續時間將改變對象移動的速度。持續時間越短,對象速度越快,反之亦然(因爲持續時間越短意味着獲得某處的時間越少,因此速度越快)。當調用touchesEnded時,將從character中刪除鍵爲"moveAction"的操作,從而停止它。

就是這樣。如果您有任何問題,請告訴我!順便說一句,我不確定你是否打算這樣做,但是當你運行物體向上移動的動作時,character最終會因重力而上下跳動。