这节内容我们来实现熊猫打滚。思路是这样的,当熊猫起跳时记录他的Y坐标,落到平台上的时候再记录它的Y坐标。两个坐标之间的差要是大于一定数值就判断它从高处落下要进行打滚缓冲。至此跑酷熊猫已经像一个游戏的样子了
要点:
起跳y坐标:
var jumpStart = 0.0
落地y坐标:
var jumpEnd = 0.0
在didBeginContact方法中进行判断
panda.jumpEnd = panda.position.y
if panda.jumpEnd-panda.jumpStart <= - {
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 =
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:/,green:/,blue:/,alpha:)
self.backgroundColor = skyColor //背景
self.addChild(bg)
bg.zPosition = self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(, -)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody.categoryBitMask = BitMaskType.scene
self.physicsBody.dynamic = false panda.position = CGPointMake(, )
panda.zPosition =
self.addChild(panda) self.addChild(platformFactory)
platformFactory.sceneWidth = self.frame.size.width
platformFactory.delegate = self
platformFactory.zPosition =
platformFactory.createPlatform(, x: , y: ) } override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
panda.jump()
} override func update(currentTime: CFTimeInterval) {
bg.move(moveSpeed/)
lastDis -= moveSpeed
if lastDis <= {
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=,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= ; 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()
if status != Status.jump2{
self.runAction(SKAction.animateWithTextures(jumpFrames, timePerFrame: 0.05))
self.physicsBody.velocity = CGVectorMake(, )
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