Swift遊戲實戰-跑酷熊貓 13 熊貓打滾

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   檔案   

 

這節內容我們來實現熊貓打滾。思路是這樣的,當熊貓起跳時記錄他的Y座標,落到平台上的時候再記錄它的Y座標。兩個座標之間的差要是大於一定數值就判斷它從高處落下要進行打滾緩衝。至此跑酷熊貓已經像一個遊戲的樣子了

要點:

起跳y座標:

var jumpStart = 0.0

落地y座標:

var jumpEnd = 0.0

在didBeginContact方法中進行判斷

panda.jumpEnd = panda.position.yif panda.jumpEnd-panda.jumpStart <= -70 {        panda.roll()}

 

整體代碼:本節關鍵區段已加紅加粗

GameScene

import SpriteKitclass GameScene: SKScene,ProtocolMainScene ,SKPhysicsContactDelegate{        @lazy var panda = Panda()    @lazy var platformFactory = PlatformFactory()    @lazy var bg = BackGround()        var moveSpeed:CGFloat = 15    var lastDis = 0.0        func didBeginContact(contact: SKPhysicsContact!) {                if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == ( BitMaskType.platform | BitMaskType.panda ){            panda.run()                        panda.jumpEnd = panda.position.y            if panda.jumpEnd-panda.jumpStart <= -70 {                panda.roll()            }        }                if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == ( BitMaskType.scene | BitMaskType.panda ){            println("遊戲結束!")        }    }    func didEndContact(contact: SKPhysicsContact!){        panda.jumpStart = panda.position.y            }    override func didMoveToView(view: SKView) {        let skyColor = SKColor(red:113/255,green:197/255,blue:207/255,alpha:1)        self.backgroundColor = skyColor                //背景        self.addChild(bg)        bg.zPosition = 20                self.physicsWorld.contactDelegate = self        self.physicsWorld.gravity = CGVectorMake(0, -5)        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)        self.physicsBody.categoryBitMask = BitMaskType.scene        self.physicsBody.dynamic = false                panda.position = CGPointMake(200, 400)        panda.zPosition = 40        self.addChild(panda)                self.addChild(platformFactory)        platformFactory.sceneWidth = self.frame.size.width        platformFactory.delegate = self        platformFactory.zPosition = 30        platformFactory.createPlatform(3, x: 0, y: 200)    }        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {        panda.jump()    }       override func update(currentTime: CFTimeInterval) {        bg.move(moveSpeed/5)        lastDis -= moveSpeed        if lastDis <= 0 {            println("產生新的平台")            //platformFactory.createPlatform(1, x: 1500, y: 200)            platformFactory.createPlatformRandom()        }        platformFactory.move(moveSpeed)    }    func onGetData(dist:CGFloat){        self.lastDis = dist;    }}protocol ProtocolMainScene {    func onGetData(dist:CGFloat)}

Panda

import SpriteKitenum 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    //起跳 y座標    var jumpStart = 0.0    //落地 y座標    var jumpEnd = 0.0    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))            self.physicsBody.velocity = CGVectorMake(0, 450)            if status == Status.jump{                status = Status.jump2                self.jumpStart = self.position.y;            }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 與平台的碰撞13 二段跳的實現
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.