使用lufylegend编写手游-jungle man

40 篇文章 0 订阅
20 篇文章 0 订阅

前言

这个别说我山寨–我确实是山寨,不过只是看看html5能够写到什么程度而已。这个游戏现在还是一个草稿版。。虽然已经有1300了—假如我转做写手绝对可以赚钱,一个小故事也要几十万字的那种。现在只是做了刚体,跳跃,人物动画,碰撞检测等等,当然,开始菜单,正式界面,gameover界面都是有的。。。这些图片资源都是来自jungle man—网上搜的。

运行截图

这里写图片描述
这里写图片描述
这里写图片描述

代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>

  <meta name="apple-mobile-web-app-capable" content="yes"/>
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta content="telephone=no,email=no" name="format-detection">
  <meta name="full-screen" content="true"/>
  <%--竖屏--%>
  <%--<meta name="screen-orientation" content="portrait"/>--%>
  <%--横屏--%>
  <meta name="screen-orientation" content="landscape"/>

  <meta name="x5-fullscreen" content="true"/>
  <meta name="360-fullscreen" content="true"/>
  <script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="../js/jquery-migrate-1.2.1.min.js"></script>
  <script src="../js/box2dWeb/Box2dWeb-2.1.a.3.min.js"></script>
  <script src="../js/lufylegend/lufylegend-1.10.0.min.js"></script>


  <style>
    body{
      padding: 0;
      margin: 0;
    }
  </style>

</head>
<body>

<div id="game" style=""></div>

<script type="text/javascript">

  var GameSettings={
    limitTime:25 //游戏设定,这是当前游戏的限制时间,以秒为单位。
    ,rows:10

    ,opening_animation_time:1 //默认的开场动画的时间,秒为单位,默认1s。
    ,cup_open_or_close_time:0.2 //默认的打开一个杯子的动画的演示时间,秒为单位,默认1s。
    ,cup_stay_time_after_open:0.5 //默认打开一个杯子以后停留时间,该时间是让猜测者可以看清楚硬币是不是在这个杯子。
    ,max_life:3 //最多限制有几条命,当然,-1 表示无限制。
    ,cup_amount:3 //有多少个杯子,请勿放太多杯子,否则会让玩家变傻的。

    ,groundSpeed:10 //每次地板移动距离---
    ,birdSpeed:5 //每次小鸟移动距离,请注意,小鸟速度必然比地板的慢。。

  };


  var GameOptions={
    width:630
    ,height:360
    ,actorWidth:57*2*0.8
    ,actorHeight:61*2*0.8
  };
  var GameData={
    score:0
    ,beginTime:0
    ,stage:1 //当前关卡。

    ,endTime:0
    ,currentRowIndex:0 //游戏当前的行数是。。。以0开始。
    ,userWantsStart:false
    ,playing:false
    ,busy:false //当前是否忙
    ,showAnimation:false //是否正在执行动画。
    ,timeOver:false //是否游戏结束
    ,usedLife:0//已经用了多少条命了。
    ,gameOver:false//是否游戏结束
    ,allowOpenCup:false//是否允许打开杯子。
  };
  var innerTools={
    timeFormat:function(seconds){
      var _seconds=parseInt(seconds);
      var _s=_seconds%60;
      var _m=parseInt((_seconds-_s)/60)%60;
      var _str="";
      if(_m<=9){
        _str="0"+""+_m;
      }
      else{
        _str=""+_m;
      }
      if(_s<=9){
        _str=_str+":0"+_s;
      }
      else{
        _str=_str+":"+_s;
      }
      return _str;
    }
    //--设定刚体的位置。。。请留意box2d刚体位置的规则。
    ,setRigidBodyPosition:function(rigidBodyObj,vec_x,vec_y){
      var A=rigidBodyObj;
      A.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(vec_x,vec_y));
      A.box2dBody.SetAwake(true);
    }
    //--根据delta变量来移动相关的刚体变量。--最后一个变量表明当前的rigidbody是单独一个还是hash表数组什么的,有两个值,"single"和"hashmap"
    ,moveRigidBody:function(rigidbody,delta_vec_x,delta_vec_y,listOrSingle){
      function _move(obj,_x,_y){
        var A=obj;
        A.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(A.box2dBody.GetPosition().x+_x,A.box2dBody.GetPosition().y+_y));
        A.box2dBody.SetAwake(true);
      }
      if(listOrSingle==undefined||listOrSingle=="single"){
        _move(rigidbody,delta_vec_x,delta_vec_y);
      }
      else{
        for(var key in rigidbody){
          var _obj=rigidbody[key];
          _move(_obj,delta_vec_x,delta_vec_y);
        }
      }
    }
  };


</script>
<script>
  //--这是需要加载的各种资源。
  var _resources=[];
  //--加载完成后得到的数据。,
  var resourceList={};
  function _addResource(name,path){
    _resources.push({
      name:name
      ,path:path
    });
  }

  var LayerLoading={};

  //--初始化其他设置,设置屏幕等等。
  function resetSystem(){
    //--刚体系统设置
    //LStage.setDebug(true);
    LGlobal.box2d = new LBox2d();
    //--屏幕设置
    //LGlobal.align = LStageAlign.BOTTOM_MIDDLE;
    LGlobal.stageScale = LStageScaleMode.SHOW_ALL;
    //LGlobal.stageScale = LStageScaleMode.EXACT_FIT;

    LSystem.screen(LStage.FULL_SCREEN);
//    LGlobal.stageScale = LStageScaleMode.SHOW_ALL;
//    LSystem.screen(LStage.FULL_SCREEN);
  }
  //--加载资源。
  function loadResources(callback){
//    _addResource("coin","images/coin.png");
//    _addResource("cup_shadow","images/cup-shadow.png");
//    _addResource("cup","images/cup.png");
//    _addResource("game_title","images/game-title.png");
//    _addResource("game_title2","images/game-title2.png");
//    _addResource("game_whole_cup","images/game-whole-cup.png");
//    _addResource("heart","images/heart.png");
//    _addResource("start_words","images/start_words.png");
    _addResource("tiledbackground","images/tiledbackground.png");
    _addResource("tiledbackground2","images/tiledbackground2.png");
    _addResource("title-sheet0","images/title-sheet0.png");
    _addResource("lets_run_button-sheet0","images/lets_run_button-sheet0.png");
    _addResource("lets_run_button_pressed-sheet0","images/lets_run_button_pressed-sheet0.png");
    _addResource("highscore_button-sheet0","images/highscore_button-sheet0.png");
    _addResource("highscore_button_pressed-sheet0","images/highscore_button_pressed-sheet0.png");
    _addResource("music_button_unmute-sheet0","images/music_button_unmute-sheet0.png");
    _addResource("music_button_mute-sheet0","images/music_button_mute-sheet0.png");
    _addResource("cloud_1-sheet0","images/cloud_1-sheet0.png");
    _addResource("cloud_2-sheet0","images/cloud_2-sheet0.png");
    _addResource("cloud_3-sheet0","images/cloud_3-sheet0.png");
    _addResource("collect_live-sheet0","images/collect_live-sheet0.png");
    _addResource("runnerlives-sheet0","images/runnerlives-sheet0.png");
    _addResource("particlesrunnerlives","images/particlesrunnerlives.png");
    _addResource("play-sheet0","images/play-sheet0.png");
    _addResource("pause-sheet0","images/pause-sheet0.png");
    _addResource("block","images/block.png");
    _addResource("player-sheet0","images/player-sheet0.png");
    _addResource("gameover_panel-sheet0","images/gameover_panel-sheet0.png");











    LayerLoading = new LoadingSample3();
    addChild(LayerLoading);
    //--这里开始要加载资源了。
    LLoadManage.load(
            _resources,
            function(progress){LayerLoading.setProgress(progress);}
            ,
            function(_resource_loaded_object){
              resourceList=_resource_loaded_object;
              removeChild(LayerLoading);
              if(callback){
                callback();
              }
            }
    );
  }

  init(25,"game",GameOptions.width,GameOptions.height,function(){
    resetSystem();
    loadResources(function(){
      console.log("初始化完成。");
      gameInit();
    });
  });
</script>

<script>
  //--游戏的初始化。
  function gameInit(){
    ObjMenu=initMenuLayer({
      onClick:function(){
        //--点击开始游戏了。
        console.log("快开始游戏啊。");
        ObjMain=initMainLayer({
          onGameOver:function(){
            GameOverObj=initGameOverLayer({

            });
            LayerGameOVer=GameOverObj.getRoot();
            LGlobal.stage.removeChild(LayerMain);
            LGlobal.stage.addChild(LayerGameOVer);
            GameOverObj.bgRun();

          }
        });
        LayerMain=ObjMain.getRoot();
        LayerMenu.visible=false;
        ObjMenu.bgStop();
        ObjMain.startGame();
        LGlobal.stage.addChild(LayerMain);



      }
    });
    LayerMenu=ObjMenu.getRoot();
    LGlobal.stage.addChild(LayerMenu);
    ObjMenu.bgRun();
  }
</script>

<script>

  var LayerMenu=new LSprite();
  var ObjMenu={};
  function initMenuLayer( __opts){

    var settings={
      onClick:function(){

      }
    };
    $.extend(settings,__opts);
    var menuLayer = new LSprite();

    //--背景。。。
    var _bg_sprite=new LSprite();
    var _bmp_bg1=new LBitmap(new LBitmapData(resourceList["tiledbackground2"]));
    var _bmp_bg_cpy=new LBitmap(new LBitmapData(resourceList["tiledbackground2"]));
    window.bg_sprite=_bg_sprite;
    _bg_sprite.addChild(_bmp_bg1);
    _bmp_bg_cpy.x=_bmp_bg1.width;
    _bg_sprite.addChild(_bmp_bg_cpy);

    //--云彩。
    var bmp_cloud1=new LBitmap(new LBitmapData(resourceList["cloud_1-sheet0"]));
    var bmp_cloud2=new LBitmap(new LBitmapData(resourceList["cloud_2-sheet0"]));
    var bmp_cloud3=new LBitmap(new LBitmapData(resourceList["cloud_3-sheet0"]));

    var sprite_clouds=new LSprite();


    bmp_cloud1.x=0;
    bmp_cloud1.y=60;

    bmp_cloud2.x=parseInt(GameOptions.width/3)+bmp_cloud2.width;
    bmp_cloud2.y=50;

    bmp_cloud3.x=parseInt(GameOptions.width/3)*2+bmp_cloud3.width;
    bmp_cloud3.y=40;


    sprite_clouds.addChild(bmp_cloud1);
    sprite_clouds.addChild(bmp_cloud2);
    sprite_clouds.addChild(bmp_cloud3);


    //--好了,标题文字及动画。

    var _screen_middle_x=parseInt(GameOptions.width/2);
    var _screen_middle_y=parseInt(GameOptions.height/2);

    var sprite_title=new LSprite();
    var bmp_title=new LBitmap(new LBitmapData(resourceList["title-sheet0"]));
    window.bmp_title=bmp_title;
    bmp_title.scaleX=0.24;
    bmp_title.scaleY=0.24;
    var _title_width=bmp_title.width*0.24;



    var _title_x=parseInt(_screen_middle_x-_title_width/2);
    sprite_title.x=_title_x;
    sprite_title.y=50;
    sprite_title.addChild(bmp_title);

    //--不停左右缓动。。
    sprite_title.x=_title_x-10;
    function _title_animate(){
      LTweenLite.to(sprite_title,1,{x:_title_x+10}).
              to(sprite_title,1,{x:_title_x-10,onComplete:function(){
                _title_animate();
              }});
    }
    _title_animate();


    //--开始按钮。

    var bmp_start_btn_normal=new LBitmap(new LBitmapData(resourceList["lets_run_button-sheet0"]));
    var bmp_start_btn_pressed=new LBitmap(new LBitmapData(resourceList["lets_run_button_pressed-sheet0"]));

    bmp_start_btn_normal.scaleX=0.6;
    bmp_start_btn_normal.scaleY=0.6;
    bmp_start_btn_pressed.scaleX=0.6;
    bmp_start_btn_pressed.scaleY=0.6;

    var _start_width=bmp_start_btn_normal.width*0.6;
    var _start_height=bmp_start_btn_normal.height*0.6;

    var btn_start=new LButton(bmp_start_btn_normal,bmp_start_btn_pressed);

    btn_start.x=parseInt(_screen_middle_x-(_start_width)/2);
    btn_start.y=parseInt(_screen_middle_y-(_start_height)/2);

    //--好了,底部的相关工具栏目

    var sprite_bottom=new LSprite();

    //--

    var _tmp_bmp_mute=new LBitmap(new LBitmapData(resourceList["music_button_mute-sheet0"]));
    var _tmp_bmp_unmute=new LBitmap(new LBitmapData(resourceList["music_button_unmute-sheet0"]));
    var _bottom_btn_width=_tmp_bmp_mute.width*0.5;
    var _bottom_btn_height=_tmp_bmp_mute.height*0.5;
    _tmp_bmp_mute.scaleX=0.5;
    _tmp_bmp_mute.scaleY=0.5;
    _tmp_bmp_unmute.scaleX=0.5;
    _tmp_bmp_unmute.scaleY=0.5;

    var _tmp_bmp_high_score_normal=new LBitmap(new LBitmapData(resourceList["highscore_button-sheet0"]));
    var _tmp_bmp_high_score_pressed=new LBitmap(new LBitmapData(resourceList["highscore_button_pressed-sheet0"]));


    _tmp_bmp_high_score_normal.scaleX=0.5;
    _tmp_bmp_high_score_normal.scaleY=0.5;
    _tmp_bmp_high_score_pressed.scaleX=0.5;
    _tmp_bmp_high_score_pressed.scaleY=0.5;








    var button_music=new LButton(_tmp_bmp_unmute,_tmp_bmp_mute,_tmp_bmp_unmute);
    var button_high_score=new LButton(_tmp_bmp_high_score_normal,_tmp_bmp_high_score_pressed,_tmp_bmp_high_score_normal);

    window.button_music=button_music;


    sprite_bottom.addChild(button_music);
    sprite_bottom.addChild(button_high_score);

    button_high_score.x=_bottom_btn_width+20;
    sprite_bottom.y=GameOptions.height-_bottom_btn_height-20;
    sprite_bottom.x=GameOptions.width-(_bottom_btn_width+20)*2;





    function bg_run(){

      //console.log(new Date());

     // return;
      if(_bg_sprite.x<(GameOptions.width-_bmp_bg1.width)){
        _bg_sprite.x=0;
        //_bg_sprite.x=_bg_sprite.x-1;
      }
      else{
        _bg_sprite.x=_bg_sprite.x-2;
      }

      //--云彩移动相关。
      if(bmp_cloud1.x>GameOptions.width+bmp_cloud1.width){
        bmp_cloud1.x=0-bmp_cloud1.width;
      }
      else{
        bmp_cloud1.x=bmp_cloud1.x+1;
      }

      if(bmp_cloud2.x>GameOptions.width+bmp_cloud2.width){
        bmp_cloud2.x=0-bmp_cloud2.width;
      }
      else{
        bmp_cloud2.x=bmp_cloud2.x+2;
      }

      if(bmp_cloud3.x>GameOptions.width+bmp_cloud3.width){
        bmp_cloud3.x=0-bmp_cloud3.width;
      }
      else{
        bmp_cloud3.x=bmp_cloud3.x+2;
      }

    }

    //--初始化相关点击事件。
    function _attach_events(){
      btn_start.addEventListener(LMouseEvent.MOUSE_UP,function(event){
        if(settings.onClick){
          settings.onClick();
        }
      });
    }
    _attach_events();



    menuLayer.addChild(_bg_sprite);
    menuLayer.addChild(sprite_clouds);
    menuLayer.addChild(sprite_title);
    menuLayer.addChild(btn_start);
    menuLayer.addChild(sprite_bottom);

    //--分别有标题和开始按钮。
//
//    var bitmap_title=new LBitmap(new LBitmapData(resourceList["game_title2"]));
//    var sp_title=bitmap_title;
//    var bitmap_start=new LBitmap(new LBitmapData(resourceList["start_words"]));
//    var button_start=new LButton(bitmap_start);
//    sp_title.alpha=0;
//    button_start.alpha=0;
//    sp_title.x=parseInt(GameOptions.width/2-bitmap_title.width/2);
//    sp_title.y=parseInt(GameOptions.height/2-bitmap_title.height/2);
//
//    button_start.x=parseInt(GameOptions.width/2-bitmap_start.width/2);
//    button_start.y=parseInt(GameOptions.height/2-bitmap_start.height/2);
//
//
//    menuLayer.addChild(sp_title);
//    menuLayer.addChild(button_start);
//    function _click_handler(event){
//      if(settings.onClick){
//        settings.onClick();
//      }
//    }
//    //--开幕动画。
//
//    var tween = LTweenLite.to(sp_title,GameSettings.opening_animation_time,{alpha:1,onComplete:function(){
//      LTweenLite.to(sp_title,GameSettings.opening_animation_time,{alpha:0,onComplete:function(){
//        LTweenLite.to(button_start,GameSettings.opening_animation_time,{alpha:1,onComplete:function(){
//          menuLayer.addEventListener(LMouseEvent.MOUSE_UP,_click_handler);
//        }});
//      }});
//    }});


    var robj={
      getRoot:function(){
        return menuLayer;
      }
      ,bgRun:function(){
        menuLayer.addEventListener(LEvent.ENTER_FRAME,bg_run);
      }
      ,bgStop:function(){
        menuLayer.removeEventListener(LEvent.ENTER_FRAME,bg_run);
      }
    };

    return robj;
  }


</script>
<script type="text/javascript">
  var LayerMain={};
  var ObjMain={};
  var MainData={
    distance:0
    ,score:0
    ,isPlaying:true
    ,isPause:false
    ,isGameOver:false
    ,isTimeOver:false
  };

  ///--这是生成地板,小鸟,奖励时候的相关参数数据。
  var GenerateOptions={
    ground_min_sep:40 //地板的最小间隔
    ,ground_max_sep:180//地板的最大间隔
    ,ground_min_width:200 //地板最小宽度
    ,ground_max_width:400 //地板最大宽度。
    ,ground_min_height:20 //地板最小高度。
    ,ground_max_height:110 //地板最大高度。
  };
  //--舞台上面地板的所有刚体的hashmap
  var GroundBlockMap={};
  var BirdBlockMap={};//小鸟刚体map
  var PrizeBlockMap={};//奖励的刚体的map
  //--接下来这个方法是换算lufylegend舞台坐标和box2dbody物理世界坐标的。输入的是当前物体在舞台的像素绝对定位,输出的是物理世界的坐标。
  function GetBox2dPosition(Stage_x_with_px,Stage_y_with_px){
//----********请注意,在刚体的世界,定位的点是刚体的中心点,!!!!!且30像素为一个物理世界坐标单位。********---//
    var stage_width=630;
    var stage_height=360;
    var vec_x=0;
    var vec_y=0;
    vec_x=Stage_x_with_px/(630/21)+10.5;
    vec_y=Stage_y_with_px/(360/12)+6;
    console.log(vec_x,vec_y);
    return {
      vec_x:vec_x
      ,vec_y:vec_y
    };
    //return (new LGlobal.box2d.b2Vec2(vec_x,vec_y));
    //A.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(A.box2dBody.GetPosition().x+myself_num,A.box2dBody.GetPosition().y));
  }
  function initMainLayer(__opts){

    //----********请注意,在刚体的世界,譬如现在这种情况,对于y轴而言,360像素的高度,6为最顶端,18为最低端,对于x轴而言,譬如现在630这种情况,10.5为x的开端,31.5为x的终端。********---//

    var BlockIndex=0;//当前已经制作的block的序号。
    var PhysicStageX=0;//当前舞台的x轴位置。
    var PhysicStageWidth=0;//当前舞台的宽度。

    var settings={
      onGameOver:function(){

      }
    };
    $.extend(settings,__opts);
    MainData={
      distance:0
      ,score:0
      ,isPlaying:true
      ,isPause:false
      ,isGameOver:false
      ,isTimeOver:false
    };
    GroundBlockMap={};
    BirdBlockMap={};//小鸟刚体map
    PrizeBlockMap={};//奖励的刚体的map

    var _root_=new LSprite();

    //--初始化--背景。
    var sprite_bg=new LSprite();
    function _init_bg(){
      var bmp_bg=new LBitmap(new LBitmapData(resourceList["tiledbackground"]));
      sprite_bg.addChild(bmp_bg);
      //--背景需要循环显示。
      _root_.addEventListener(LEvent.ENTER_FRAME,function(event){
        if(MainData.isPlaying==false){
          return;
        }
        if(sprite_bg.x<(GameOptions.width-bmp_bg.width)){
          sprite_bg.x=0;
          //_bg_sprite.x=_bg_sprite.x-1;
        }
        else{
          sprite_bg.x=sprite_bg.x-2;
        }

      });

    }
    _init_bg();
    //--初始化固定的 顶部状态显示。

    var _top_settings={
      onPlayClick:function(){
        console.log("play click");
      }
      ,onPauseClick:function(){
        console.log("pause click");
      }
      ,maxLives:5 //--最多多少条生命。
    };
    var top_bar_container=new LSprite();
    var top_btn_pause=new LSprite();
    var bmp_play_btn=new LBitmap(new LBitmapData(resourceList["play-sheet0"]));
    var bmp_pause_btn=new LBitmap(new LBitmapData(resourceList["pause-sheet0"]));
    var _play_state="playing";
    var tips_distance=new LTextField();
    var tips_score=new LTextField();
    var top_lives_container=new LSprite();

    var game_top_bar={
      init:function(___opts){
        var child=this;
        $.extend(_top_settings,___opts);
        child.initUI();
        child.setPause();
      }
      ,setPlay:function(){
        _play_state="playing";
        bmp_play_btn.alpha=1;
        bmp_pause_btn.alpha=0;
      }
      ,setPause:function(){
        _play_state="pause";
        bmp_play_btn.alpha=0;
        bmp_pause_btn.alpha=1;
      }
      ,setScore:function(score_num){
        tips_score.text="分数:"+score_num;
      }
      ,setDistance:function(dist_num){
        tips_distance.text="距离:"+dist_num;
      }
      ,setCurrentLives:function(lives_num){
        if(_top_settings.maxLives<=0){
          return;
        }
        for(var i=0;i< _top_settings.maxLives;i++){
          var _now_bitmap=top_lives_container.getChildByName('life-icon-'+i);
          if(_now_bitmap==null){
            continue;
          }
          if(i<lives_num){
            _now_bitmap.visible=true;
          }
          else{
            _now_bitmap.visible=false;
          }
        }
      }
      ,initUI:function(){
        bmp_play_btn.alpha=1;
        bmp_pause_btn.alpha=0;
        bmp_pause_btn.scaleX=0.5;
        bmp_pause_btn.scaleY=0.5;
        bmp_play_btn.scaleX=0.5;
        bmp_play_btn.scaleY=0.5;
        top_btn_pause.addChild(bmp_play_btn);
        top_btn_pause.addChild(bmp_pause_btn);
        top_btn_pause.addEventListener(LMouseEvent.MOUSE_UP,function(event){
          if(_play_state=="playing"){
            _top_settings.onPlayClick();
          }
          else if(_play_state=="pause"){
            _top_settings.onPauseClick();
          }
        });

        tips_distance.color="#000000";
        tips_distance.size=18;
        tips_distance.text="距离:0";

        tips_score.color="#000000";
        tips_score.size=18;
        tips_score.text="分数:0";

        tips_distance.x=60;
        tips_score.x=60;
        tips_distance.y=10;
        tips_score.y=30;

        //--生命插件。
        //--life_width=32 life_height=27;
        var lifeW=32;
        var lifeH=27;
        var _total_container_w=0;
        var _life_sep=5;//生命图标间隔。

        for(var i=0;i<_top_settings.maxLives;i++){
          if(i!=0){
            _total_container_w+=_life_sep;

          }
          _total_container_w+=lifeW;
          var tmpSprite=new LSprite();
          tmpSprite.addChild(new LBitmap(new LBitmapData(resourceList["runnerlives-sheet0"])));
          tmpSprite.name="life-icon-"+i;
          tmpSprite.x=i*lifeW;
          if(i!=0){
            tmpSprite.x+=i*_life_sep;
          }
          top_lives_container.addChild(tmpSprite);
        }

        top_lives_container.width=_total_container_w;
        top_lives_container.x=GameOptions.width-_total_container_w-_life_sep;




        top_bar_container.addChild(top_lives_container);
        top_bar_container.addChild(tips_distance);
        top_bar_container.addChild(tips_score);

        top_bar_container.addChild(top_btn_pause);

      }

    };


    //--playing_handler

    var mainGameContainer=new LSprite();

    var gamectr_settings={};
    var PlayerActor={};//主角。
    var SpriteActor=new LSprite();
    var ActorData={
      running:true
      ,jumping:false
    };
    var GameActor={
      init:function(){

        var list = LGlobal.divideCoordinate(1024,1024, 5,5);
        var data = new LBitmapData(resourceList["player-sheet0"]);
        //mainGameContainer.addChild(new LBitmap(data));

        //return;

        list=[];
        //--设置相关动画数组。
        for(var i=0;i< 5;i++){
          var tmpImageInfo={x : 54, y : 58, width : 114, height : 122, sx : 0, sy : 0, dataIndex : 0};
          tmpImageInfo.dataIndex=0;
          var delta_width=194;
          var delta_x=54;
          var delta_height=194;
          var delta_y=58;
          //delta_height=200;
          tmpImageInfo.y=delta_y+i*delta_height;

//          if(i==1){
//            tmpImageInfo.y=248;
//          }
          for(var j=0;j<5;j++){
            if(i<4){
              tmpImageInfo.x=delta_x+j*delta_width;
              list.push(tmpImageInfo);
            }
            else if(i==4&&j==0){
              tmpImageInfo.x=delta_x+j*delta_width;
              list.push(tmpImageInfo);
              continue;
            }

          }
//          list.push(tmpImageInfo);




          //tmpImageInfo.x=delta_x+(i%5)*delta_width;
//          if(i==1){
//            tmpImageInfo.x=i*delt_width;
//            tmpImageInfo.y=58;
//          }
//          if(i==2){
//            tmpImageInfo.x=442;
//            tmpImageInfo.y=58;
//          }
//          tmpImageInfo.x=1*delt_width;
//          tmpImageInfo.y=58;
          //tmpImageInfo.x=i*delt_width;
          //tmpImageInfo.y=0;



        }

        var datas=[];
        datas.push(data);
        PlayerActor = new LAnimationTimeline(datas, [list]);
        PlayerActor.speed=1;//设定运动的速度。
        SpriteActor.addChild(PlayerActor);
        SpriteActor.scaleX=0.8;
        SpriteActor.scaleY=0.8;
        SpriteActor.name="actor";

        //--设定sprite的初始化位置。
//        SpriteActor.y=GameOptions.height-50-GameOptions.actorHeight;
//        SpriteActor.x=GameOptions.actorWidth*3;

        //--刚体。
        window.SpriteActor=SpriteActor;
        SpriteActor.addBodyPolygon(GameOptions.actorWidth,GameOptions.actorHeight,1,0, 0, 0);
        mainGameContainer.addChild(SpriteActor);

        innerTools.setRigidBodyPosition(SpriteActor,(GameOptions.actorWidth*3)/30,(GameOptions.height-50-GameOptions.actorHeight)/30+(GameOptions.actorWidth/2)/30);

        window.PlayerActor=PlayerActor;

        return;
        addChild(playerLeft);

        var playerImage=new LBitmap(new LBitmapData(resourceList["player-sheet0"],0,0,180,180));



        SpriteActor.addChild(playerImage);
        SpriteActor.x=0;

        mainGameContainer.addChild(SpriteActor);

        //--好了,这里要设定重力,。。让人物重一点,别轻飘飘吓人。
        var massdata= new LGlobal.box2d.b2MassData();
        massdata.mass=2.5;
        massdata.I=10;
        SpriteActor.box2dBody.SetMassData(massdata);


      }
      //--移动多少个身位
      ,move:function(myself_num){
        var A=SpriteActor;
        A.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(A.box2dBody.GetPosition().x+myself_num,A.box2dBody.GetPosition().y));
        A.box2dBody.SetAwake(true);
      }

      //--跑动。
      ,run:function(){
        //SpriteActor.box2dBody.ApplyImpulse(new LGlobal.box2d.b2Vec2(1,1),SpriteActor.box2dBody.GetWorldCenter())
        var tmpVec = new LStage.box2d.b2Vec2();
        tmpVec.x=5;
        SpriteActor.box2dBody.ApplyImpulse(tmpVec,SpriteActor.box2dBody.GetWorldCenter())

      }
      //--跳跃。
      ,jump:function(){
//        var tmpVec = new LStage.box2d.b2Vec2();
//        tmpVec.y=-6.5;
//        SpriteActor.box2dBody.ApplyImpulse(tmpVec,SpriteActor.box2dBody.GetWorldCenter());

        var tmpVec = new LStage.box2d.b2Vec2();
        tmpVec.y=-220;
        SpriteActor.box2dBody.ApplyForce(tmpVec,SpriteActor.box2dBody.GetWorldCenter());
      }

    };

    var GameController={
      init:function(___opts){
        $.extend(gamectr_settings,___opts);
        var child=this;
        child.initMap();
        GameActor.init();
        setTimeout(function(){
          child.initSpriteEvent();
          child.initCollisionEvent();
        },1000);
      }

      //--初始化操作事件。
      ,initSpriteEvent:function(){
        addEventListener(LMouseEvent.MOUSE_UP,function(){
          if(MainData.isGameOver==true||MainData.isPause==true||MainData.isTimeOver){
            return;
          }
          if(ActorData.jumping){
            return;
          }
          console.log("角色要开始跳了。");
          ActorData.jumping=true;
          GameActor.jump();
        });
        addEventListener(LMouseEvent.TOUCH_START,function(){
          if(MainData.isGameOver==true||MainData.isPause==true||MainData.isTimeOver){
            return;
          }
          if(ActorData.jumping){
            return;
          }
          console.log("角色要开始跳了。");
          ActorData.jumping=true;
          GameActor.jump();
        });
      }

      //--初始化底图。
      ,initMap:function(){
        var child=this;


        var _begin_block=child.createBlock(0,GameOptions.width+300,50);

        // innerTools.setRigidBodyPosition(_begin_block,((GameOptions+300))/60,(GameOptions.height)/30-50/60);
        //innerTools.setRigidBodyPosition(blockSprite,(GameOptions.width+300)/60,GameOptions.height/30-50/60);
        window.blockSprite=_begin_block;
        mainGameContainer.addChild(_begin_block);
      }

      //--这是创建某个符合要求的地面地板。只需要告诉在某个x,宽度及离地面高度(高度)即可。
      ,createBlock:function(loc_x,loc_width,loc_height,callback){

        var blockSprite=new LSprite();
        blockSprite.x=loc_x;
        blockSprite.y=GameOptions.height-loc_height;
        blockSprite.width=loc_width;
        blockSprite.height=loc_height;
        var _theBlockIndex=0;
        var _theBlockLocX=0;

        blockSprite.name="ground_block_"+BlockIndex;
        //--刚体。

        blockSprite.addChild(new LBitmap(new LBitmapData(resourceList["block"],0,0,loc_width,loc_height)));
        blockSprite.addBodyPolygon(loc_width,loc_height,0,0,0,0);
        //blockSprite.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2(20,20));
        //blockSprite.box2dBody.SetAwake(true);
        //--设定刚体当前的位置。
        var vec_x=loc_x/30+loc_width/60;
        var vec_y=GameOptions.height/30-loc_height/60;
        innerTools.setRigidBodyPosition(blockSprite,vec_x,vec_y);
        //--加到刚体的map;里面。
        GroundBlockMap["ground_block_"+BlockIndex]=blockSprite;
        //--好了,计算舞台的宽度了。。。
        var preStageWidth=PhysicStageWidth;
        if(PhysicStageWidth<loc_x+loc_width){
          PhysicStageWidth=loc_x+loc_width;
        }
//        console.log("输入参数为:x "+loc_x+" width "+loc_width+" height "+loc_height);
      //  console.log("新建立的地板的宽度和高度是:"+loc_width+"x"+loc_height+"上一个舞台宽度是:"+preStageWidth+",当前物理舞台的宽度:"+PhysicStageWidth+" 建立的向量为:"+vec_x+" 那么当前的中心点坐标为: "+(vec_x*30)+" 那么左边边距的坐标为:"+(vec_x*30-loc_width/2));
//


        if(callback){
          callback();
        }
        BlockIndex++;

        return blockSprite;
      }

      //-- 好了,这个是移动当前世界的所有地板刚体
      ,moveGround:function(delta_vec_x){
        PhysicStageX=PhysicStageX+Math.abs(delta_vec_x*30);
        innerTools.moveRigidBody(GroundBlockMap,delta_vec_x,0,"hashmap");
      }

      //--移动所有小鸟刚体。
      ,moveBirds:function(delta_vec_x){

        innerTools.moveRigidBody(BirdBlockMap,delta_vec_x,0,"hashmap");
      }

      //--好了,检查当前物理舞台的位置和舞台宽度,判断是不是要制作新的地板,新的小鸟和新的道具。

      ,stageGenerate:function(__options){
        var innerSettings={
          afterCreateGround:function(){
            console.log("现在执行afterCreateGround");
          }
        };

        $.extend(innerSettings,__options);

        var child=this;
        if(PhysicStageX+300+GameOptions.width<PhysicStageWidth){
          return;
        }
//        console.log("当前的stagex和 width:",PhysicStageX,PhysicStageWidth,"已经在危险范围以内,需要新创建地板,小鸟和奖励奖品等等。");
        //--地板的创建,地板有最小间隔,最大间隔和最小长度,最大长度,还有最小高度和最大高度,都是取随机。

        function getSeed(){
          var r=Math.random();
          r=parseInt(r*100)/100;

          return r;
        }

        var _ground_sep=getSeed()*(GenerateOptions.ground_max_sep-GenerateOptions.ground_min_sep)+GenerateOptions.ground_min_sep;
        var _ground_width=getSeed()*(GenerateOptions.ground_max_width-GenerateOptions.ground_min_width)+GenerateOptions.ground_min_width;
        var _ground_height=getSeed()*(GenerateOptions.ground_max_height-GenerateOptions.ground_min_height)+GenerateOptions.ground_min_height;
        var ground_x=PhysicStageWidth+_ground_sep;
        var _currentBlockIndex=BlockIndex;

        var _ground_sprite=child.createBlock(ground_x,_ground_width,_ground_height,function(){
          var _preBlockIndex=_currentBlockIndex-1;
          console.log("已经执行完create block 操作,当前时间:"+new Date().getTime(),"目前的block 序号:"+_currentBlockIndex,"间隔是:"+_ground_sep);
          //--好了,因为刚体是异步产生的。在不停运动的情况下,位置会出现偏差--怎么偏差法?自己试试就知道了。
          //现在来尝试修正偏差的位置。
          var _preBlock= GroundBlockMap["ground_block_"+_preBlockIndex];
          var _currentBlock=GroundBlockMap["ground_block_"+_currentBlockIndex];
          innerTools.setRigidBodyPosition(_currentBlock,_preBlock.box2dBody.GetPosition().x+_ground_sep/30+_preBlock.width/60+_currentBlock.width/60,_currentBlock.box2dBody.GetPosition().y);
          console.log("尝试修复异步后的地板位置偏移");
          innerSettings.afterCreateGround();
        });
        mainGameContainer.addChild(_ground_sprite);
//        console.log("当前的新地板参数:",ground_x,_ground_width,_ground_height);
//        console.log("间隔是:::",_ground_sep);


      }

      //--这种小游戏是不能无限新建对象的。。于是就有了对多余对象的清除问题了。。
      ,clearGroundRubbish:function(){
        //--根据目前的stage x 位置,来判断一下哪一些刚体是已经消失了的,对于消失了的刚体就要清除释放内存了。


        //--对地板刚体的处理。
        var _ground_keys=[];
        for(var key in GroundBlockMap){
          _ground_keys.push(key);
        }
        for(var i=0;i<_ground_keys.length;i++){
          var now_key=_ground_keys[i];
          var now_block=GroundBlockMap[now_key];
          var _block_vec_x=now_block.box2dBody.GetPosition().x+now_block.width/60;
          console.log(""+now_key+" 而 block模块的终点:"+_block_vec_x);
          if(_block_vec_x<0){
            console.log("清除该地板");
            delete GroundBlockMap[now_key];
            now_block.clearBody();
            now_block.clearShape();
            now_block.die();
            now_block.remove();

          }
        }
      }
      //--初始化碰撞检测相关事件。
      ,initCollisionEvent:function(){
        function beginContact(contact){
          var _sprite_a_name=contact.GetFixtureA().GetBody().GetUserData().name ;
          var _sprite_b_name=contact.GetFixtureB().GetBody().GetUserData().name;
          var str="碰撞双方:"+_sprite_a_name+" "+_sprite_b_name;
          function _check_id(str_name_a,str_name_b){
            if(_sprite_a_name==str_name_a&&_sprite_b_name==str_name_b){
              return true;
            }
            else if(_sprite_a_name==str_name_b&&_sprite_b_name==str_name_a){
              return true;
            }
            return false;
          }

          if(_sprite_a_name=="actor"){
            if(_sprite_b_name.indexOf("ground_block_")!=-1){
              if(ActorData.jumping){
                ActorData.jumping=false;
              }
            }
          }
          console.log(str);
        }

        LGlobal.box2d.setEvent(LEvent.BEGIN_CONTACT,beginContact);
      }
    };

    var __loopIndex=0;

    function _game_playing_handler(){

      //--检查一下是不是已经gameover了。
      //情况1、主角已经出了边缘区域。

      if((SpriteActor.box2dBody.GetPosition().x+GameOptions.actorWidth/60)<0){
        //--出了左边边缘了,game over。
        MainData.isGameOver=true;
        settings.onGameOver();
        return;
      }
      //--假如主角已经跌下去了,那么就认为game over。
      if(SpriteActor.box2dBody.GetPosition().y>GameOptions.height/30){
        MainData.isGameOver=true;
        settings.onGameOver();
        return;
      }

      //--设定距离显示。
      //MainData.distance+=GameSettings.speed;
      game_top_bar.setDistance(parseInt(PhysicStageX/30));
      GameController.moveGround(0-GameSettings.groundSpeed/30);
      GameController.moveBirds(0-GameSettings.birdSpeed/30);
      //--好了,检查一下是不是需要新建相关的游戏物体。
      GameController.stageGenerate({
        afterCreateGround:function(){
          //--好了,清理垃圾。
          GameController.clearGroundRubbish();
        }
      });


    }

    //---好了,设定核心部分的那个frame-事件。

    function _game_core_handler(){

      if(MainData.isPause){
        return;
      }
      if(MainData.isGameOver){

        return;
      }
      if(MainData.isTimeOver){
        return;
      }
      if(MainData.isPlaying){
        _game_playing_handler();
      }
    }



    function initGame(){
      game_top_bar.init();
      _root_.addChild(sprite_bg);
      _root_.addChild(top_bar_container);

      GameController.init();
      _root_.addChild(mainGameContainer);
      mainGameContainer.addEventListener(LEvent.ENTER_FRAME,_game_core_handler);
      window.mainGameContainer=mainGameContainer;
    }

    initGame();

    var robj={
      getRoot:function(){

        return _root_;
      }
      ,startGame:function(){

      }

    };

    return robj;
  }
</script>

<script>
  var LayerGameOVer={};
  var GameOverObj={};
  function initGameOverLayer( __opts){

    var settings={
      onRestartClick:function(){}

    };
    $.extend(settings,__opts);

    var _root_=new LSprite();



    //--背景。。。
    var _bg_sprite=new LSprite();
    var _bmp_bg1=new LBitmap(new LBitmapData(resourceList["tiledbackground2"]));
    var _bmp_bg_cpy=new LBitmap(new LBitmapData(resourceList["tiledbackground2"]));
    window.bg_sprite=_bg_sprite;
    _bg_sprite.addChild(_bmp_bg1);
    _bmp_bg_cpy.x=_bmp_bg1.width;
    _bg_sprite.addChild(_bmp_bg_cpy);

    //--云彩。
    var bmp_cloud1=new LBitmap(new LBitmapData(resourceList["cloud_1-sheet0"]));
    var bmp_cloud2=new LBitmap(new LBitmapData(resourceList["cloud_2-sheet0"]));
    var bmp_cloud3=new LBitmap(new LBitmapData(resourceList["cloud_3-sheet0"]));

    var sprite_clouds=new LSprite();


    bmp_cloud1.x=0;
    bmp_cloud1.y=60;

    bmp_cloud2.x=parseInt(GameOptions.width/3)+bmp_cloud2.width;
    bmp_cloud2.y=50;

    bmp_cloud3.x=parseInt(GameOptions.width/3)*2+bmp_cloud3.width;
    bmp_cloud3.y=40;


    sprite_clouds.addChild(bmp_cloud1);
    sprite_clouds.addChild(bmp_cloud2);
    sprite_clouds.addChild(bmp_cloud3);









    function bg_run(){

      //console.log(new Date());

      // return;
      if(_bg_sprite.x<(GameOptions.width-_bmp_bg1.width)){
        _bg_sprite.x=0;
        //_bg_sprite.x=_bg_sprite.x-1;
      }
      else{
        _bg_sprite.x=_bg_sprite.x-2;
      }

      //--云彩移动相关。
      if(bmp_cloud1.x>GameOptions.width+bmp_cloud1.width){
        bmp_cloud1.x=0-bmp_cloud1.width;
      }
      else{
        bmp_cloud1.x=bmp_cloud1.x+1;
      }

      if(bmp_cloud2.x>GameOptions.width+bmp_cloud2.width){
        bmp_cloud2.x=0-bmp_cloud2.width;
      }
      else{
        bmp_cloud2.x=bmp_cloud2.x+2;
      }

      if(bmp_cloud3.x>GameOptions.width+bmp_cloud3.width){
        bmp_cloud3.x=0-bmp_cloud3.width;
      }
      else{
        bmp_cloud3.x=bmp_cloud3.x+2;
      }

    }





    _root_.addChild(_bg_sprite);
    _root_.addChild(sprite_clouds);

    //--下面是积分板。
    var sprite_score_board=new LSprite();
    function _init_score_board(){
      var bmp_board=new LBitmap(new LBitmapData(resourceList["gameover_panel-sheet0"]));

      var _scale_=0.5;

      sprite_score_board.x=GameOptions.width/2-bmp_board.width/2*_scale_;
      sprite_score_board.y=60;

      var sprite_board_tmp=new LSprite();
      sprite_board_tmp.addChild(bmp_board);
      sprite_board_tmp.scaleX=_scale_;
      sprite_board_tmp.scaleY=_scale_;
      sprite_score_board.addChild(sprite_board_tmp);


      _root_.addChild(sprite_score_board);
    }

    _init_score_board();

    //--初始化相关点击事件。
    function _attach_events(){

    }
    _attach_events();

    var robj={
      getRoot:function(){
        return _root_;
      }
      ,bgRun:function(){
        _root_.addEventListener(LEvent.ENTER_FRAME,bg_run);
      }
      ,bgStop:function(){
        _root_.removeEventListener(LEvent.ENTER_FRAME,bg_run);
      }
    };

    return robj;
  }

</script>

</body>
</html>

资源下载地址

下载资源

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值