使用sprikit构建关卡1 4-13 人物通过轻扫可以实现左右移动上跳下蹲(复杂的有待构建)

//
//  WLHuman.swift
//  iobb
//
//  Created by lotawei on 16/4/7.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SpriteKit
enum  ActionState:Int {
    case stand=1,left,right,jump;
}

@objc  protocol  WlHumanprotocol
{
    
    


    func   humanwilljump()->CGPoint
    func   humanwillleft()->CGPoint
    func   humanwillright()->CGPoint
}



class WLHuman: SKSpriteNode {
    
    static let   jumpvalue:CGFloat=40
    static  let  contactmask=00000001
    
    
    
    var   actionstate:ActionState=ActionState.stand
    var    standatlas=SKTextureAtlas(named: "stand")
    var    turnrightatlas=SKTextureAtlas(named: "turnright")
    var    turnleftatlas=SKTextureAtlas(named: "turnleft")
     var   jumpatlas=SKTextureAtlas(named: "jump")
    
    var   standframe=[SKTexture]()
    var   turnrightframe=[SKTexture]()
    var   turnleftframe=[SKTexture]()
    var  jumpframe=[SKTexture]()
    var    delegate:WlHumanprotocol?
    
    init()
    {
        let    texture=standatlas.textureNamed("stand_01.png")
        
        let  size=CGSizeMake(texture.size().width/4.0, texture.size().height/4.0)
        print("original"+"\(size)")
        super.init(texture: texture, color:SKColor.redColor(), size: size)
        settextureframe("stand_", atlas: standatlas)
        settextureframe("left_", atlas: turnleftatlas)
        settextureframe("right_", atlas: turnrightatlas)
          settextureframe("jump_", atlas: jumpatlas)
        
    }
    func   settextureframe(name:String,atlas:SKTextureAtlas)
    {
        let   cout  = atlas.textureNames.count
        if   name=="stand_"
        {
            
            for   i  in  1...cout
            {
                let   aname=String(format: "stand_%.2d", i)
                let   atexture=standatlas.textureNamed(aname)
                if   atexture.isKindOfClass(SKTexture)
                {
                    standframe.append(atexture)
                }
                
                
            }
            
            
        }
        else if  name=="left_"
        {
            for  i in 1...cout
            {
                
                let   aname=String(format: "left_%.2d", i)
                let   atexture=turnleftatlas.textureNamed(aname)
                if   atexture.isKindOfClass(SKTexture)
                {
                    turnleftframe.append(atexture)
                }
            }
            
            
        }
        else  if  name=="right_"
        {
            for  i in 1...cout
            {
                
                let   aname=String(format: "right_%.2d", i)
                let   atexture=turnrightatlas.textureNamed(aname)
                if   atexture.isKindOfClass(SKTexture)
                {
                    turnrightframe.append(atexture)
                }
            }
            
        }
        else  if  name=="jump_"
        {
            for  i in 1...cout
            {
                
                let   aname=String(format: "jump_%.2d", i)
                let   atexture=jumpatlas.textureNamed(aname)
                if   atexture.isKindOfClass(SKTexture)
                {
                    jumpframe.append(atexture)
                }
            }
        }
        
        
    }
    
    
    
    
    func   standaction()
    {
        self.removeAllActions()
        self.actionstate=ActionState.stand
        self.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(standframe, timePerFrame: 0.3)))
        
    }
        //向右行走的动画
        func   turnrightaction()
        {
    
            self.removeAllActions()
            self.actionstate=ActionState.right
            self.delegate?.humanwillright()
            self.runAction(SKAction.sequence([SKAction.group([SKAction.animateWithTextures(self.turnrightframe , timePerFrame:2/30)
                
                
                ]),SKAction.repeatActionForever(SKAction.animateWithTextures(standframe, timePerFrame: 0.3))]))
            
    
    
        }
        //zuo行走的动画
        func   turnleftaction()
        {
    
            self.removeAllActions()
            self.actionstate=ActionState.left
           self.delegate!.humanwillleft()
            self.runAction(SKAction.sequence([SKAction.group([SKAction.animateWithTextures(self.turnleftframe , timePerFrame:2/30)
                
                
                ]),SKAction.repeatActionForever(SKAction.animateWithTextures(standframe, timePerFrame: 0.3))]))
            
            
    
        }
    //  跳跃的动画
    func   jumpaction()
    {
    
    self.removeAllActions()
    self.actionstate=ActionState.jump
   
     let   newhigh = self.delegate!.humanwilljump()
      
  
        self.runAction(SKAction.sequence([SKAction.group([                        SKAction.moveToY(newhigh.y, duration:2/10),SKAction.animateWithTextures(self.jumpframe , timePerFrame:2/30)
        
        
                                                   ]),SKAction.repeatActionForever(SKAction.animateWithTextures(standframe, timePerFrame: 0.3))]))
     

    
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}





//

//  GameScene.swift
//  firstscene
//
//  Created by lw on 16/4/12.
//  Copyright (c) 2016年 lw. All rights reserved.
//

import SpriteKit

//
//  PlayScene.swift
//  iobb
//
//  Created by lotawei on 16/4/4.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import SpriteKit


class PlayScene: SKScene,UIGestureRecognizerDelegate,WlHumanprotocol {
    
    //跳跃手势
    var    jumpswip:UISwipeGestureRecognizer!
    //向左移动
       var    leftswip:UISwipeGestureRecognizer!
    //向右移动
       var    rightswip:UISwipeGestureRecognizer!
    //向下蹲
       var    downswip:UISwipeGestureRecognizer!
    var   iscreate=false
    var   lasttime:NSTimeInterval=0
    var   dt:NSTimeInterval=0
    var   backgroud:SKNode!
    var   padding:CGFloat=20
    //通过这个变量停止动画
    var    isstopanimation=true

 
    
    var    wlhuman:WLHuman!
    //地平线
    var    groundline:SKSpriteNode!
      var    endgroundline:SKSpriteNode!
    //第一个矮的方块
    
    var    barrier:SKSpriteNode!
    //第二个的方块
    
    var    midbarrier:SKSpriteNode!
    //弹簧
    var     spring:SKSpriteNode!
    //三个奖励星星的
    var     s1:SKSpriteNode!
    var     s2:SKSpriteNode!
     var     s3:SKSpriteNode!
    //传送门
    var     door:SKSpriteNode!
    override  func didMoveToView(view: SKView) {
        self.size=view.frame.size
        
        
        createcontent()
        
        
    }
    
    
    
    func   createcontent()
    {
        if  iscreate {
            
            return
        }
        
        //页面内容的设置
        backgroud=SKNode()
        
        backgroud.zPosition=0
        backgroud.name="backgroud"
        let   backsprite=SKSpriteNode(imageNamed: "backgroud")
        backsprite.name="backsprite"
        backsprite.anchorPoint=CGPointMake(0, 0)
        backsprite.zPosition=1
        backsprite.position=CGPointZero
        backgroud.addChild(backsprite)
        
        
        self.backgroundColor=UIColor.whiteColor()
        
        
        wlhuman=WLHuman()
        wlhuman.physicsBody=SKPhysicsBody(rectangleOfSize: wlhuman.size)
        wlhuman.physicsBody?.dynamic=true
        self.physicsWorld.speed=0.6
        
        wlhuman.name="wlhuman"
        wlhuman.zPosition=7
        print("sadasd"+"\(wlhuman.size)")
        wlhuman.anchorPoint=CGPointMake(0.5, 1)
        wlhuman.position=CGPointMake(CGRectGetMidX((view?.frame)!),CGRectGetMidY((view?.frame)!)+wlhuman.size.height+padding)
        print(wlhuman.size)
        wlhuman.standaction()
        backgroud.addChild(wlhuman)
        addChild(backgroud)
        groundline=SKSpriteNode(imageNamed: "groudline")
        groundline.anchorPoint=CGPointZero
        groundline.zPosition=2
        groundline.position=CGPointMake(0, CGRectGetMidY((view?.frame)!))
        backgroud.addChild(groundline)
        
        barrier=SKSpriteNode(imageNamed: "smallbarrier")
        barrier.anchorPoint=CGPointMake(0.5, 1)
        barrier.zPosition=2
        print(barrier.size)
        barrier.position=CGPointMake(CGRectGetMaxX(groundline.frame)-padding*6,groundline.position.y+barrier.size.height)
        backgroud.addChild(barrier)
        
        
        midbarrier=SKSpriteNode(imageNamed: "midbarrier")
        midbarrier.anchorPoint=CGPointMake(0.5, 1)
        midbarrier.zPosition=2
        print(midbarrier.size)
        midbarrier.position=CGPointMake(barrier.position.x+barrier.size.width+padding,groundline.position.y+midbarrier.size.height)
        backgroud.addChild(midbarrier)
        
        
        spring = SKSpriteNode(imageNamed: "spring")
        spring.anchorPoint=CGPointMake(0.5, 1)
        spring.zPosition=2
        print(spring.size)
        spring.position=CGPointMake(CGRectGetMaxX( groundline.frame)+spring.size.width/2.0,groundline.position.y)
        backgroud.addChild(spring)
        
        
        s1 = SKSpriteNode(imageNamed: "star")
        s1.anchorPoint=CGPointMake(0.5, 1)
        s1.zPosition=2
        print(s1.size)
        s1.position=CGPointMake(spring.position.x,spring.position.y+midbarrier.size.height+WLHuman.jumpvalue)
        backgroud.addChild(s1)
        s2 = SKSpriteNode(imageNamed: "star")
        s2.anchorPoint=CGPointMake(0.5, 1)
        s2.zPosition=2
        print(s1.size)
        s2.position=CGPointMake(CGRectGetMaxX(spring.frame)+WLHuman.jumpvalue*3,spring.position.y+s2.size.height)
        backgroud.addChild(s2)
        s3 = SKSpriteNode(imageNamed: "star")
        s3.anchorPoint=CGPointMake(0.5, 1)
        s3.zPosition=2
        
        s3.position=CGPointMake(s2.position.x+s2.size.width+padding,spring.position.y+s2.size.height)
        backgroud.addChild(s3)
        
        
        door  = SKSpriteNode(imageNamed: "door")
      
       door.anchorPoint=CGPointMake(0.5, 1)
        door.zPosition=5
        
        door.position=CGPointMake(CGRectGetMaxX(s3.frame)+padding*2,groundline.position.y+door.size.height)
        backgroud.addChild(door)
        
        
        
        endgroundline  = SKSpriteNode(imageNamed: "groudline")
        
        endgroundline.anchorPoint=CGPointMake(0,0)
        endgroundline.zPosition=2
        
        endgroundline.position=CGPointMake(CGRectGetMinX(s2.frame),groundline.position.y)
        backgroud.addChild(endgroundline)
        
        
        
        jumpswip=UISwipeGestureRecognizer(target: self, action: "guestureaction:")
        
        jumpswip.direction=UISwipeGestureRecognizerDirection.Up
        
        self.view!.addGestureRecognizer(jumpswip)
        downswip=UISwipeGestureRecognizer(target: self, action: "guestureaction:")
        
        downswip.direction=UISwipeGestureRecognizerDirection.Down
        
        self.view!.addGestureRecognizer(downswip)
        leftswip=UISwipeGestureRecognizer(target: self, action: "guestureaction:")
        
        leftswip.direction=UISwipeGestureRecognizerDirection.Left

        self.view!.addGestureRecognizer(leftswip)
        rightswip=UISwipeGestureRecognizer(target: self, action: "guestureaction:")
        
        rightswip.direction=UISwipeGestureRecognizerDirection.Right
        
        self.view!.addGestureRecognizer(rightswip)
        
        //设置物理边界
        self.backgroud.physicsBody=SKPhysicsBody(edgeFromPoint: groundline.position, toPoint: CGPointMake(groundline.position.x+groundline.size.width, groundline.position.y))
        
        
        
       
        iscreate=true
        
    }
    
    //实现即将跳跃的代理因为这里不像左右移动 这里我们的任务他的位置实际是要发生变化的
    func  humanwilljump() -> CGPoint {
        
         return  CGPointMake(wlhuman.position.x,  wlhuman.position.y+WLHuman.jumpvalue)
    }
//    func  humanwilldown(huaman: WLHuman) {
//        wlhuman.delegate=self
//        
//        print("去实现下蹲的动画")
//    }
    func   humanwillleft()->CGPoint {
      
        return   wlhuman.position
        
    }
    func   humanwillright()->CGPoint {
          return   wlhuman.position
    }
  override  func  update(currentTime: NSTimeInterval) {
          print(wlhuman.position.y)
    }
    
    func  guestureaction(gesture:UISwipeGestureRecognizer)
    {
    
        let    atype=gesture.direction
        
        if   atype==UISwipeGestureRecognizerDirection.Up
        {
        
            wlhuman.delegate=self
            wlhuman.jumpaction()
          
            
        }
        else   if atype==UISwipeGestureRecognizerDirection.Down

        {
            print("move down")
           
        }
        else   if atype==UISwipeGestureRecognizerDirection.Right
            
        {
            
                 wlhuman.delegate=self
            
                wlhuman.turnrightaction()
                allcontentmoveleft()
            
        }
        else   if atype==UISwipeGestureRecognizerDirection.Left
            
        {
            wlhuman.delegate=self
            wlhuman.turnleftaction()
            allcontentmoveright()
        }
        
        
    }
    func    allcontentmoveleft()
    {
     
      let    nodes=backgroud.children
      let    backsp=backgroud.childNodeWithName("backsprite") as! SKSpriteNode
    if   backsp.position.x < -1000
        {
            return
        }

        for  anode in nodes
        
        {
            if  !anode.isKindOfClass(WLHuman)
            {
                let    contentnode=anode  as! SKSpriteNode
                contentnode.position.x-=10
            }
         
        }
     
    }
    func    allcontentmoveright()
    {
        
       let   nodes=backgroud.children
       let    backsp=backgroud.childNodeWithName("backsprite") as! SKSpriteNode
        if   backsp.position.x>=0
        {
            return
        }
        
        for  anode in nodes
        {
            if  !anode.isKindOfClass(WLHuman)
            {
                
                let    contentnode=anode  as! SKSpriteNode
                contentnode.position.x+=10
            }
        }
    }
    
    

}


逻辑结构简单:但动画还是扫尾流畅点了




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值