zoukankan      html  css  js  c++  java
  • Swift游戏实战-跑酷熊猫 13 二段跳的实现

    这节内容我们来实现熊猫的二段跳。

    要点:

    二段跳的逻辑:

    逻辑一,第一次点击屏幕,status就会变成jump。

    逻辑二,第二次点击屏幕,status就会变成jump2。

    逻辑三,当status变成jump2的时候不响应屏幕点击

    逻辑四,当熊猫落地时将status变为run。

    所以只要当status的不等于jump2的时候,点击屏幕都会给熊猫施加一个向上的速度。这时候判断。如果是一段跳就将status设为jump。如果是二段跳就将status设为jump2。落地改变状态之前已经写好了。

    二段跳的实现:

           

     func jump (){
            self.removeAllActions()
            if status != Status.jump2 {
                self.runAction(SKAction.animateWithTextures(jumpFrames, timePerFrame: 0.05),withKey:"jump")
                self.physicsBody.velocity = CGVectorMake(0, 450)
                if status == Status.jump {
                    status = Status.jump2
                }else{
                    status = Status.jump
                }
            }
        }

     

    整体代码:

    import SpriteKit
     
    enum Status:Int{
        case run=1,jump,jump2,roll;
    }
     
    class Panda : SKSpriteNode {
        let runAtlas = SKTextureAtlas(named: "run.atlas")
        let runFrames = [SKTexture]()
       
        let jumpAtlas = SKTextureAtlas(named: "jump.atlas")
        let jumpFrames = [SKTexture]();
       
        let rollAtlas = SKTextureAtlas(named: "roll.atlas")
        let rollFrames = [SKTexture]();
       
        var status = Status.run
     
        init(){
            let texture = runAtlas.textureNamed("panda_run_01")
            let size = texture.size()
            super.init(texture:texture,color:SKColor.whiteColor(),size:size)
           
            var i:Int
            for i=1 ; i<=runAtlas.textureNames.count ; i++ {
                let tempName = String(format: "panda_run_%.2d", i)
                let runTexture = runAtlas.textureNamed(tempName)
                if runTexture {
                    runFrames.append(runTexture)
                }
            }
            for i=1 ; i<=jumpAtlas.textureNames.count ; i++ {
                let tempName = String(format: "panda_jump_%.2d", i)
                let jumpTexture = jumpAtlas.textureNamed(tempName)
                if jumpTexture {
                    jumpFrames.append(jumpTexture)
                }
            }
            for i=1 ; i<=rollAtlas.textureNames.count ; i++ {
                let tempName = String(format: "panda_roll_%.2d", i)
                let rollTexture = rollAtlas.textureNamed(tempName)
                if rollTexture {
                    rollFrames.append(rollTexture)
                }
            }
           
            self.physicsBody = SKPhysicsBody(rectangleOfSize: texture.size())
            self.physicsBody.dynamic = true
            self.physicsBody.allowsRotation = false
            //摩擦力
            self.physicsBody.restitution = 0
            self.physicsBody.categoryBitMask = BitMaskType.panda
            self.physicsBody.contactTestBitMask = BitMaskType.scene | BitMaskType.platform
            self.physicsBody.collisionBitMask = BitMaskType.platform
            run()
        }
        func run(){
            self.removeAllActions()
            self.status = .run
            self.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(runFrames, timePerFrame: 0.05)))
        }
       
        func jump (){
            self.removeAllActions()
            if status != Status.jump2 {
                self.runAction(SKAction.animateWithTextures(jumpFrames, timePerFrame: 0.05),withKey:"jump")
                self.physicsBody.velocity = CGVectorMake(0, 450)
                if status == Status.jump {
                    status = Status.jump2
                }else{
                    status = Status.jump
                }
            }
        }
       
        func roll(){
            self.removeAllActions()
            status = .roll
            self.runAction(SKAction.animateWithTextures(rollFrames, timePerFrame: 0.05),completion:{() in self.run()})
        }
      
    }

    项目文件地址

    http://yun.baidu.com/share/link?shareid=3824235955&uk=541995622

    Swift游戏实战-跑酷熊猫系列

    00 游戏预览

    01 创建工程导入素材

    02 创建熊猫类

    03 熊猫跑动动画

    04 熊猫的跳和滚的动作

    05 踩踏平台是怎么炼成的

    06 创建平台类以及平台工厂类

    07 平台的移动

    08 产生源源不断的移动平台

    09 移除场景之外的平台

    10 视差滚动背景

    11 欢迎进入物理世界

    12 与平台的碰撞

  • 相关阅读:
    影响CSS的margin合并的几个属性
    Mouse w/o Borders实现两台主机共用一套键鼠方法及问题处理
    隐藏"Input"标签默认样式
    如何快速开发网站?
    如何让Web.xml变得简洁?
    关于中文处理方面的研究
    Hello,World 百态
    UI开发的终极解决方案
    构建网络爬虫?so easy
    MDA数据校验规则定义
  • 原文地址:https://www.cnblogs.com/sandal1980/p/3896269.html
Copyright © 2011-2022 走看看