15.Cocos跑酷游戏——02主角(与游戏界面交互)

文章目录


1.List工具篇
2.工具篇 Dictionary
3.工具篇 读取Json文件保存数据
4.资源管理ResourceManager
5.界面层级管理 LayerManager
6.界面管理 UIManager
7.事件监听篇 EventBus
8.枚举篇 枚举管理
9.游戏总管 Mir
10游戏入口 Main
11.声音管理器
12首页界面
13.游戏界面
14.01 背景
15.02主角(与游戏界面交互)
16.03添加怪物来袭
17.04添加障碍物
18.05 添加障碍物排列
19.06添加奖励物品
20.07奖励物质排列数据
21.从零开始-Cocos跑酷游戏——游戏结束界面
22.最后的补充

游戏最好的方式是可以通过配置文件去增加需求或者更改需求

主角因为涉及到需要变身切换并且 一个主角的基础属性需要有一个明确的值

  1. 先解析配置文件,将配置文件中的数据转为游戏中可以使用的数据
  2. 游戏运行时,将保存的配置文件数据保存到PlayerData中
  3. 载入主角时从,PlayerData中获取数据

配置文件

[
  {
    "ID": 4001,
    "name": "piqiu2",
    "Speed": 12,
    "jump": 20,
    "path": "ball/piqiu2",
    "maxYSpeed":50,
    "gravity":50
  },
  {
    "ID": 4002,
    "name": "piqiu2",
    "Speed": 8,
    "jump": 12,
    "path": "ball/piqiu2",
    "maxYSpeed":50,
    "gravity":50
  },
  {
    "ID": 4003,
    "name": "gangqiu",
    "Speed": 6,
    "jump": 10,
    "path": "ball/gangqiu",
    "maxYSpeed":50,
    "gravity":50
  }
]

这个工具篇中解析配置文件的通用模板
解析Json文件 设置到PlayersCfgVo 保存在容器中

文件名 PlayersCfgData.js

var PlayersCfgData=
{
    dataList:null,
    filePath:null,
    init:function()
    {
        this.dataList=new window.dictionary();
        this.filePath=window.Constant.RootPath.CONFIG_ROOT_PATH+"players";
        window.cc.loader.loadRes(this.filePath,(function(err,array){

            if(err)
            {
                console.log("错误信息:"+err);
                return;
            }
            var arrs=array.json;
            var PlayerData=window.data.PlayerData;
            for(var i=0;i<arrs.length;i++)
            {
                var mData=arrs[i];
                var cfgDataVo=new window.PlayersCfgVo();
                cfgDataVo.SetValue(mData);
                this.dataList.add(cfgDataVo.id,cfgDataVo);

                // var dataVo=new window.PlayerVoData();
                // dataVo.SetValue(mData)
                // PlayerData.AddPlayerData(dataVo.id,dataVo);
            }

        }).bind(this));
    },
    GetPlayersCfgData:function()
    {
        return this.dataList;
    },
}
module.exports=PlayersCfgData;

PlayersCfgVo 配置文件数据
文件名 PlayersCfgVo.js

function PlayersCfgVo()
{
    this.id;
    this.name;
    this.speed;
    this.jump;
    this.path;
    this.maxYSpeed;
    this.gravity;
}
PlayersCfgVo.prototype.SetValue=function(mData)
{
    this.id=mData.ID;
    this.name=mData.name;
    this.speed=mData.Speed;
    this.jump=mData.jump;
    this.path=mData.path;
    this.maxYSpeed=mData.maxYSpeed;
    this.gravity=mData.gravity;
}
module.exports=PlayersCfgVo;

游戏中运行的数据

文件名 PlayerData.js

var PlayerData=
{
    playerData:null,

    init:function()
    {
        this.dataList=new window.dictionary();
    },
    AddPlayerData:function(key,value)
    {
        this.dataList.add(key,value);
        console.log(key);
        console.log(value);
    },
    GetPlayerData:function(key)
    {
        var dataDic=window.cfg.PlayersCfgData.GetPlayersCfgData();
        this.playerData=dataDic.get(key); 
        return this.playerData;
    },
}
module.exports=PlayerData;

主角的逻辑处理

文件名 PlayerControll.js


cc.Class({
    extends: cc.Component,
    properties: {

        cameraSpeed:0,  
        m_nYSpeed:0,
        m_nYMaxSpeed:50,
        m_nGravity:50,
        m_nGround:-1,    //1在地面 -1不在地面
        m_nXSpeed:0,
        m_nJump:0,
        groundY:-600,
        isGameOver:false,
        GameView:null,
        prePlayerType:null, //场景中的角色类型
        curPlayerType:null,//按钮显示的当前玩家类型
        playerIndex:0,
        touchNumber:0,

        invincible:false,//是否无敌
        invincibleTime:2, //无敌时间
        tempInvincibleTime:0,// 临时时间

        absorb:true, //吸收
        absorbTime:3,//时间
        tempAbsorbTime:0,
        ballSprite:null,
    },

    onLoad:function() {

        //碰撞器必须使用的代码
        cc.director.getCollisionManager().enabled = true;
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);   
        //获得球的动画
        this.ballSprite=cc.find("ball",this.node);
        this.ballAni=this.ballSprite.getComponent(cc.Animation);
    },
    //初始化操作
    //保存球的种类
    //碰撞器的方向标识
    onEnable()
    {
        this.arry=new Array();
        this.arry.push(window.Constant.PlayerType.NormalBall);
        this.arry.push(window.Constant.PlayerType.SteelBall);
        this.curPlayerType=this.arry[this.playerIndex];
        this.collisionY=0;              //0 有重力 -1 无重力
        this.collisionX=0;              //1 被阻挡了 0无阻挡
        this.prePos=this.node.x;
        this.isGameOver=false;
    },
    ///由GameView调用控制  初始化角色数据默认值
    init(GameView)
    {
        this.GameView=GameView;   
            ///前一次的临界点    
        this.playerIndex=0;
        this.curPlayerType=window.Constant.PlayerType.NormalBall;      
        this.OnPlayerDataChange(this.curPlayerType);
        this.addListerner();
    },

    //改变球的形态
    GetRandomChangePlayerType()
    {     
       if(this.isGameOver)
          return; 
       this.OnPlayerDataChange(this.curPlayerType);
    },
    ///改变球的形态
    OnPlayerDataChange:function(playerType)
    {
        this.curPlayerType=playerType;
        var playerVoData=window.data.PlayerData.GetPlayerData(playerType);
        this.ChangePlayerData(playerVoData);
        this.ChangeShow(this.playerName);
        this.prePlayerType=this.curPlayerType;     
        //获取下一个玩家信息
        this.GetRandomPlayerData();
    }, 
    //键盘控制
    onKeyDown:function(event)
    {
       switch(event.keyCode)
       {      
            case cc.macro.KEY.j:
              this.jump();   
           break;
           case cc.macro.KEY.f:
             this.GetRandomChangePlayerType();              
           break;
           case cc.macro.KEY.r:
            this.Restart();
            break;
       }
    },

    //碰撞处理
    onCollisionEnter:function(other, self)
    {     
        
        //限制Y轴   tag=10 是地板的标识 
        if(other.tag==10)
        {     
            this.touchNumber++;
            this.collisionY = -1;
            this.m_nYSpeed=0;
            this.canJumpCount=0;
            var otherAabb = other.world.aabb;
            var selfAabb = self.world.aabb;
            var otherPreAabb = other.world.preAabb.clone();
            var selfPreAabb = self.world.preAabb.clone();
            //处理Y轴上的碰撞
            selfPreAabb.y = selfAabb.y;
            otherPreAabb.y = otherAabb.y;
            //判断是否有碰撞
            if(cc.Intersection.rectRect(selfPreAabb,otherPreAabb))
            {
                //是在地板上方还是下方
                if(selfPreAabb.y<otherPreAabb.y)
                {
                    this.collisionY=0;
                    this.canJumpCount=3; 
                }else
                {
                    this.m_nGround = 1;                   
                }
                
                ///碰撞产生形成的正方形的大小    
                var intersection=new cc.Rect();
                selfPreAabb.intersection(intersection, otherPreAabb);
                this.node.y+=intersection.height;   
                return;
            }
        }
        
        //限制X轴   与上方同理
        if(other.tag==15)
        {

            var otherAabb = other.world.aabb;
            var selfAabb = self.world.aabb;
            var otherPreAabb = other.world.preAabb.clone();
            var selfPreAabb = self.world.preAabb.clone();
                  //处理X轴上的碰撞
            selfPreAabb.x = selfAabb.x;
            otherPreAabb.x = otherAabb.x;
          //  this.collisionX=1;
            if(cc.Intersection.rectRect(selfPreAabb,otherPreAabb))
            {
              //  this.m_nGround = 1;             
                var intersection=new cc.Rect();
                this.collisionX=1;
                selfPreAabb.intersection(intersection, otherPreAabb);      
                this.node.x-=intersection.width;     
            }
        }
        if(other.tag==50)
        {
            this.PlayerDie("撞到边界线死亡");
        }
    },
    // 碰撞离开做的处理
    onCollisionExit: function (other) {

        if(other.tag==10)
        {
            this.touchNumber--;
            {
                if(this.touchNumber==0)
                {
                    this.collisionY = 0;     
                }
            }          
        } 
        if(other.tag==15)
        {
            this.collisionX=0;            
        }
      
    },
    
    update:function(dt) {

        if(this.GameView==null)
           return;
        this.dt=dt;
        this.gravityUp();
        this.refresh();
        if(this.invincible)
        {
           this.Invincible();
        }
        // if(this.absorb)
        // {
        //     this.Absorb();
        // }
    },
    lateUpdate()
    {
        if(this.GameView==null)
           return;
        if(this.cameraSpeed==0)
           return;
        if(this.isGameOver) 
          return;  
       this.GameView.PlayerCamera.x+=this.cameraSpeed;
       this.GetPlayerDistance();   
    },
    //更换玩家数据
    ChangePlayerData(mData)
    {
        //重玩的速度
      //  this.Speed=mData.speed;
        this.m_nXSpeed=mData.speed;
        this.m_nJump=mData.jump;
        this.m_nYMaxSpeed=mData.maxYSpeed;
        this.m_nGravity=mData.gravity;
        this.playerName=mData.path;       //图集路径
        this.cameraSpeed=mData.speed;
    },
    // 换图片
    ChangeShow:function(name)
    {
        var path=name;
        var spriteObj=this.ballSprite.getComponent(cc.Sprite);
        window.ResourceManager.LoadSpriteFrameByName(spriteObj,path);
        if(this.curPlayerType==window.Constant.PlayerType.NormalBall)
        {
            this.ballAni.play();
        }else
        {
            this.ballAni.stop();
        }
      
    },
    //玩家碰到障碍物
    PlayerDie:function(msg)
    {

        this.collisionY=-1;   
        this.m_nYSpeed=0;
        this.m_nXSpeed=0;  
        this.m_nGround=1;
        this.canJumpCount=0;
        console.log(msg);
        this.isGameOver=true;     
      //  this.showRestarBG();   //点击播放视频 调用
        //this.gameOver();       //震动效果完了执行
        this.onVibrateShort();
        this.onCameraXYShort();
    },
    showRestarBG()
    {
        this.GameView.RestarBG.active=true;
    },
    hideRestarBG()
    {
        this.GameView.RestarBG.active=false;
    },
    //重新开始游戏
    Restart()
    {  
        this.invincible=true;
        this.hideRestarBG();   
        this.collisionY=0;
        this.isGameOver=false;
        this.node.y=400;
        this.m_nXSpeed= this.Speed;   
        this.node.x=this.GameView.PlayerCamera.x;
       // this.node.getComponent(cc.Animation).play();
    },
    gameOver()
    {
      //  this.hideRestarBG();
        window.EventBus.pos(window.Constant.EventTypeID.OnGameOver); 
    },
    //重力效果
    gravityUp()
    {
        if(this.collisionY!=0)
         return;
        this.m_nYSpeed-=(this.dt*this.m_nGravity);
        this.m_nYSpeed=this.m_nYSpeed < -this.m_nYMaxSpeed?-this.m_nYMaxSpeed:this.m_nYSpeed;
    },
    //XY轴的更新
    refresh()
    {   

        if(this.isGameOver)
           return;     
        this.node.y += this.m_nYSpeed;
        this.checkAndMoveX();
        if(this.node.y<this.groundY)
        {
            this.collisionY=-1;   
            this.m_nYSpeed=0;
            this.m_nXSpeed=0;              
            this.PlayerDie("掉入深渊死亡发送");
                 
        }
    }, 
    //X 轴的更新判断
    checkAndMoveX()
    {
       
        if(this.collisionX==1)
          return; 

        if(this.node.x<this.GameView.PlayerCamera.x&&this.node.x!=this.GameView.PlayerCamera.x)
        {
             this.node.x+=this.m_nXSpeed+3;
        }else
        {    
            this.node.x += this.m_nXSpeed;
        }
    },
    checkAndMoveY()
    {

    },
    //起跳的逻辑 每碰触一次地板,可以跳跃三次 
    jump()
    {
        if(this.m_nGround==-1&&this.canJumpCount>2)
        {
            return;
        }
        this.jumpAction();
        window.data.YMData.GetItemFromPool(this.GameView.node,this.node.position);
        window.AudioController.playSXF(window.Constant.GameClip.jump);
        this.m_nYSpeed=this.m_nJump;
        this.m_nGround=-1;
        this.canJumpCount++;
    },
    ///每一千米 增加一次分数
    GetPlayerDistance()
    {
        if((this.node.x-this.prePos)/100>10)
        {
            this.prePos=this.node.x;
            window.data.GameRunData.SetSorce(1);
        }
    },
    //按顺序来切换球
    GetRandomPlayerData()
    {         
        this.playerIndex++;
        this.playerIndex%=this.arry.length; 
        this.curPlayerType=this.arry[this.playerIndex];
    },


    addListerner()
    {
        this.JumpEvent= this.GameView.jumpBtn.on(cc.Node.EventType.TOUCH_END,this.jump.bind(this));
        this.ChangeBtnEvent=this.GameView.ChangeBtn.on(cc.Node.EventType.TOUCH_END,this.GetRandomChangePlayerType.bind(this));
        this.reStarEvent= this.GameView.restarBtn.on(cc.Node.EventType.TOUCH_END,this.Restart.bind(this));
        this.gameOverEvent= this.GameView.gameOverBtn.on(cc.Node.EventType.TOUCH_END,this.gameOver.bind(this));
    },
    removeListener()
    {
        this.GameView.jumpBtn.off(cc.Node.EventType.TOUCH_END,this.JumpEvent);
        this.GameView.ChangeBtn.off(cc.Node.EventType.TOUCH_END,this.ChangeBtnEvent);
        this.GameView.restarBtn.off(cc.Node.EventType.TOUCH_END,this.reStarEvent);
        this.GameView.gameOverBtn.off(cc.Node.EventType.TOUCH_END,this.gameOverEvent);
    },
    onDisable()
    {
       this.removeListener();
    },
    //是否无敌判断
    Invincible()
    {
        this.tempInvincibleTime+=this.dt;
        if(this.tempInvincibleTime>this.invincibleTime)
        {
            this.tempInvincibleTime=0;
            this.invincible=false;
           // this.node.getComponent(cc.Animation).stop();
            this.node.color=cc.Color.WHITE;
        }    
    },
    
    Absorb()
    {
        // this.tempAbsorbTime+=this.dt;
        // if(this.tempAbsorbTime>this.tempAbsorbTime)
        // {
        //     this.tempAbsorbTime=0;
        //     this.absorb=false;
        // }

    },
    //球的变形
    jumpAction()
    {
        
        if(this.prePlayerType==window.Constant.PlayerType.SteelBall) //钢球不做处理
           return; 
        this.squashAction = cc.scaleTo(0.2, 1, 0.6);
        this.stretchAction = cc.scaleTo(0.2, 1, 1.2);
        this.scaleBackAction = cc.scaleTo(0.1, 1, 1);
        var seq = cc.sequence(this.squashAction, this.stretchAction,
            this.scaleBackAction, this.squashAction, this.scaleBackAction);     
        this.node.runAction(seq);
    },
    //震动效果
    onVibrateShort() {
        
        //区分设备
       if(!cc.sys.isMobile)
       {                  
            return;
       }
        wx.vibrateLong(
          {
              //震动效果执行完毕 执行
            complete()
            {
                this.gameOver();
            }
          })
    },
    // 死亡时的摄像机晃动效果
    onCameraXYShort()
    {
        var x= this.GameView.PlayerCamera.x;
        var y= this.GameView.PlayerCamera.y;
        this.squashAction = cc.moveTo(0.1, 50+x, y);
        this.stretchAction = cc.moveTo(0.1, -50+x, y);

        this.scaleBackAction = cc.moveTo(0.1, x, y);
        var seq = cc.sequence(this.squashAction, this.stretchAction,this.squashAction2,this.scaleBackAction, this.squashAction, 
            this.scaleBackAction,cc.callFunc(function()
            {
                this.gameOver();                
            }.bind(this)));     
        this.GameView.PlayerCamera.runAction(seq);
    }
});

在这里插入图片描述

4399平台

4399游戏链接:http://www.4399.com/flash/203652.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值