Swift游戏实战-跑酷熊猫 14 熊猫打滚

 

这节内容我们来实现熊猫打滚。思路是这样的,当熊猫起跳时记录他的Y坐标,落到平台上的时候再记录它的Y坐标。两个坐标之间的差要是大于一定数值就判断它从高处落下要进行打滚缓冲。至此跑酷熊猫已经像一个游戏的样子了

要点:

起跳y坐标:

var jumpStart = 0.0

落地y坐标:

var jumpEnd = 0.0

在didBeginContact方法中进行判断

panda.jumpEnd = panda.position.y

if panda.jumpEnd-panda.jumpStart <= -70 {

        panda.roll()

}

 

整体代码:本节关键部分已加红加粗

GameScene

import SpriteKit



class 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 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

    //起跳 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 二段跳的实现

你可能感兴趣的:(swift)