如何使用SpriteKit进行类似马里奥的角色运动?

时间:2015-06-26 20:36:44

标签: ios swift cocoa-touch sprite-kit

我正在使用SpriteKit进行游戏,但是我的角色运动与超级马里奥相似。

我想让他侧身移动取决于触摸的方向,当用户放手时我希望他停下来。

是否有人可以帮助我编写必要的代码?

这是我的GameScene.swift文件:

gluPerspective

2 个答案:

答案 0 :(得分:1)

马里奥使用了一个相当简单的角色移动系统 - 你会遇到像使用SpriteKit这样完全成熟的物理系统来模仿它的问题。

马里奥是一款以游戏为基础的游戏,我想他有XY positionxVelocityyVelocity。每个runloop计算他的速度并更新他的位置。这就是为什么你可以改变空中方向(你的控制器改变他的速度)。

你将与一个物理引擎战斗以反映这一点,你绝对不希望通过施加冲动来控制一个角色:它们随着时间的推移而解决(因为它们减速)并且你无法控制它。 / p>

答案 1 :(得分:1)

// :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")
相关问题