这节主要实现熊猫和平台的碰撞,实现熊猫在平台上奔跑
要点
对平台进行物理属性设置
//设置物理体以及中心点
self.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.width, self.height), center: CGPointMake(self.width/, ))
//设置碰撞标示符
self.physicsBody.categoryBitMask = BitMaskType.platform
//不受碰撞影响
self.physicsBody.dynamic = false
//不允许角度变化
self.physicsBody.allowsRotation = false
//摩擦力
self.physicsBody.restitution =
改造熊猫类
//用或“|”分割需要进行检测的标示符
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/, ))
self.physicsBody.categoryBitMask = BitMaskType.platform
self.physicsBody.dynamic = false
self.physicsBody.allowsRotation = false
//摩擦力
self.physicsBody.restitution =
} }
熊猫类的代码
import SpriteKit enum Status:Int{
case run=,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= ; 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= ; 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= ; 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 =
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