Swift遊戲實戰-跑酷熊貓 12 與平台的碰撞

來源:互聯網
上載者:User

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

 

這節主要實現熊貓和平台的碰撞,實現熊貓在平台上奔跑

 

要點

對平台進行物理屬性設定

//設定物理體以及中心點self.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.width, self.height), center: CGPointMake(self.width/2, 0))//設定碰撞標示符self.physicsBody.categoryBitMask = BitMaskType.platform//不受碰撞影響self.physicsBody.dynamic = false//不允許角度變化self.physicsBody.allowsRotation = false//摩擦力self.physicsBody.restitution = 0

改造熊貓類

//用或“|”分割需要進行檢測的標示符self.physicsBody.contactTestBitMask = BitMaskType.scene | BitMaskType.platform//用physicsBody.collisionBitMask來設定產生碰撞效果的物體self.physicsBody.collisionBitMask = BitMaskType.platform

平台類的代碼

import SpriteKit class Platform:SKNode{    var width = 0.0    var height = 10.0    func onCreate(arrSprite:[SKSpriteNode]){        for platform in arrSprite{            platform.position.x=self.width            self.addChild(platform)            self.width += platform.size.width        }               self.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.width, self.height), center: CGPointMake(self.width/2, 0))        self.physicsBody.categoryBitMask = BitMaskType.platform        self.physicsBody.dynamic = false        self.physicsBody.allowsRotation = false        //摩擦力        self.physicsBody.restitution = 0    }   }

熊貓類的代碼

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()        status = .jump        self.runAction(SKAction.animateWithTextures(jumpFrames, timePerFrame: 0.05))    }       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 歡迎進入物理世界
相關文章

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.