2015-06-26 23 views
-2

我正在製作一款SpriteKit遊戲,我無法讓角色動作與Super Mario類似。如何使用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.gravity = CGVectorMake(0.0, -5.0) 
    self.physicsWorld.contactDelegate = self 
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 

    //background 
    var background = SKSpriteNode(imageNamed: "background") 
    background.size.height = self.frame.size.height 
    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) 
    self.addChild(background) 


    //character 
    character.position = CGPointMake(self.frame.size.width * 0.6, self.frame.size.height * 0.6) 
    character.setScale(0.015) 
    character.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(character.size.width/2)) 
    character.physicsBody?.dynamic = true 
    self.addChild(character) 


    //platform 1 
    var platformTexture = SKTexture(imageNamed: "platform") 
    var platform = SKSpriteNode(texture: platformTexture) 
    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) 
    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) 
    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) 
    self.addChild(platform3) 


} 

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    var charPos = character.position 
    /* Called when a touch begins */ 
    for touch: AnyObject in touches { 
    let location = touch.locationInNode(self) 
    //Hold finger at upper area to move character constantly to the right. 
    if location == charPos{ 
     character.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 20)) 
    } 
    else { 

    } 
    if location.y > 400{ 
     //moving allowed, force is applied in update method. read the docs about applyImpulse and applyForce methods and the differences between those two. 
     move = true 

    }else{ 

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

} 

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

} 
} 



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

回答

0

馬里奧用一個非常簡單的遊戲人物動作系統 - 你將有一個完全成熟的物理系統模擬像你SpriteKit得到的問題。

馬里奧是一個基於瓦片的遊戲,我想他有一個X,Y positionxVelocityyVelocity。計算每個runloop的速度並更新他的位置。這就是爲什麼你可以在空中改變方向(你的控制器改變了他的速度)。

你會用一個物理引擎來反映這一點,你絕對不想通過沖動來控制角色:隨着時間的推移他們會解決(當他們放慢速度),而你無法控制它。

0
// :Ask Question Disable continuous movement and one movement per keyboard tapped or continuous movement when keyboard is held 
// Swift-Mario Game: I need help to disable continuous movement, and I want one movement per keyboard tapped and continuous movement when keyboard is Held. 


func run(direction: String) { 

    if direction == "right" { 

     let rightAnimation = SKAction.animateWithTextures([rightTexture1, rightTexture2, rightTexture3, rightTexture4], timePerFrame: 0.08) 
     let rightAnimationContinuous = SKAction.repeatActionForever(rightAnimation) 

     let moveRight = SKAction.moveByX(10, y: 0, duration: 0.1) 
     let moveRightForEver = SKAction.repeatActionForever(moveRight) 

     hero.runAction(SKAction.group([rightAnimationContinuous, moveRightForEver]), withKey: "runningRight") 

    } else if direction == "left" { 

     let leftAnimation = SKAction.animateWithTextures([leftTexture1, leftTexture2, leftTexture3, leftTexture4], timePerFrame: 0.08) 
     let leftAnimationContinuous = SKAction.repeatActionForever(leftAnimation) 

     let moveLeft = SKAction.moveByX(-10, y: 0, duration: 0.1) 
     let moveLeftForEver = SKAction.repeatActionForever(moveLeft) 

     hero.runAction(SKAction.group([leftAnimationContinuous, moveLeftForEver]), withKey: "runningLeft") 
    } 

    self.runAction(SKAction.repeatActionForever(runningsound), withKey: "runsound") 
} 

func stop(direction: String) { 

    if direction == "right" { 

     hero.removeAllActions() 
     hero.texture = rightTextureStill 

    } else if direction == "left" { 

     hero.removeAllActions() 
     hero.texture = leftTextureStill 
    } 

    self.removeActionForKey("runsound") 
相關問題