设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

html5游戏开发-零基础开发《圣诞老人送礼物》小游戏

2013-6-3 15:00| 发布者: 红黑魂| 查看: 4324| 评论: 0|来自: CSDN博客

摘要: 原文:PHP Best Practices-A short, practical guide for common and confusing PHP tasks译者:youngsterxyf最后修订日期维护者本文档最后审阅于2013年3月8日。最后修改于2013年5月8日。由我,Alex Cabal,维护该文 ...
这是Stage类构造器。和Charactor差不多。主要是其方法:

[javascript] view plaincopy
  1. Stage.prototype.run = function(){  
  2.     //让礼物不断下降  
  3.     this.y += stageSpeed;  
  4.     //判断是否到达边缘  
  5.     if(this.y > LStage.height){  
  6.         this.mode = "die";  
  7.     }  
  8.     this.cheackHit();  
  9. }  
  10. Stage.prototype.cheackHit = function(){  
  11.     if(this.y > 170 && this.x > 132 - 33 && this.x < 166){  
  12.         this.mode = "die";  
  13.         point++;  
  14.         changeText();  
  15.     }else if(this.y > 170 && this.x > 293 - 33 && this.x < 330){  
  16.         this.mode = "die";  
  17.         point++;  
  18.         changeText();  
  19.     }else if(this.y > 178 && this.x > 475 - 33 && this.x < 508){  
  20.         this.mode = "die";  
  21.         point++;  
  22.         changeText();  
  23.     }  
  24. }  
其实很好理解,在run中,我们让礼物向下移5格,虽然只移5格,但是如果是在onframe中调用,它将不断下降。为了判断礼物是否已经送到家,我用加入cheackHit方法。我们可以用判断坐标的方法来实现。每碰到一次就更改分数,并将mode设置为die,然后在onframe中判断mode,如果mode是"die"就移除这个对象。

实例化Stage类:

[javascript] view plaincopy
  1. function addStage(){  
  2.     var stage = new Stage();  
  3.     if(oldMan.mode == "left"){  
  4.         stage.x = oldMan.x + 70;  
  5.     }else{  
  6.         stage.x = oldMan.x + 30;  
  7.     }  
  8.     stage.y = 30;  
  9.     stageLayer.addChild(stage);  
  10.     stageLayer.scaleX = 0.8;  
  11.     stageLayer.scaleY = 0.8;  
  12. }  

onframe完整代码:

[javascript] view plaincopy
  1. function onframe(event){  
  2.     showTime = Math.floor(time/1000) + "s";  
  3.     if(canSnowing == true){  
  4.         //加入雪花  
  5.         addSnow();  
  6.     }  
  7.     if(backSound.playing == false){  
  8.         //播放背景音乐  
  9.         backSound.play();  
  10.     }  
  11.     if(showChara == true){  
  12.         //使人物动起来  
  13.         oldMan.move();  
  14.         //改变时间显示  
  15.         timeText.text = "Time:" + showTime;  
  16.         if(time>0){  
  17.             time -= 30000/(30000/50);  
  18.         }else{  
  19.             playerName = getName();  
  20.             gameOver();  
  21.         }  
  22.     }  
  23.     for(var key in stageLayer.childList){  
  24.         //使用Stage中run函数,让障碍物动起来  
  25.         stageLayer.childList[key].run();  
  26.         if(stageLayer.childList[key].mode == "die"){   
  27.             //移除该成员  
  28.             stageLayer.removeChild(stageLayer.childList[key]);  
  29.         }  
  30.     }  
  31. }  
首先我们新加了一个遍历方法,遍历LSprite成员而获取每对象的状态,每遇见一个mode是die的就将它移除。

接下来是加入分数以及时间的函数,没有任何逻辑。大家慢慢看就能看懂的。微笑

[javascript] view plaincopy
  1. function addText(){  
  2.     //加入分数文字  
  3.     pointText = new LTextField();   
  4.     pointText.size = 15;  
  5.     pointText.x = 10;  
  6.     pointText.y = 340;  
  7.     pointText.color = "white";  
  8.     pointText.text = "Point:" + point;  
  9.     pointText.font = "HG行書体";  
  10.     overLayer.addChild(pointText);  
  11.     //加入时间文字  
  12.     timeText = new LTextField();   
  13.     timeText.size = 15;  
  14.     timeText.x = 10;  
  15.     timeText.y = LStage.height - 30;  
  16.     timeText.color = "white";  
  17.     timeText.text = "Time:" + showTime;  
  18.     timeText.font = "HG行書体";  
  19.     overLayer.addChild(timeText);  
  20.     //加入滤镜  
  21.     var shadow = new LDropShadowFilter(0,45,"white",0);  
  22.     overLayer.filters = [shadow];  
  23. }  
  24. function changeText(){  
  25.     pointText.text = "Point:" + point;  
  26. }  
以下是游戏结束调用的函数,同样是很简单:

[javascript] view plaincopy
  1. function gameOver(){  
  2.     backLayer.die();  
  3.     //绘制成绩板  
  4.     gameoverLayer.graphics.drawRect(2,"dimgray",[0,0,400,300],true,"lightgray");  
  5.     gameoverLayer.x = 100;  
  6.     gameoverLayer.y = 50;  
  7.     gameoverLayer.scaleX = 0.5,  
  8.     gameoverLayer.scaleY = 0.5,  
  9.     gameoverLayer.alpha = 0.5,  
  10.     gameoverLayer.rotate = 50;  
  11.     var shadow = new LDropShadowFilter(5,45,"black",0);  
  12.     gameoverLayer.filters = [shadow];  
  13.     //通过缓动显示成绩板  
  14.     LTweenLite.to(gameoverLayer,1,{  
  15.         alpha:0.7,  
  16.         scaleX:1,  
  17.         scaleY:1,  
  18.         rotate:0,  
  19.         ease:Back.easeInOut,  
  20.         onComplete:resultFont  
  21.     });  
  22. }  
  23. function resultFont(){  
  24.     var resultArr = ["GAME OVER","Tap to Restart Game","分数:"+point,"评价:"+playerName];  
  25.     for(var i=0;i<resultArr.length;i++){  
  26.         //公有属性  
  27.         resultText = new LTextField();  
  28.         resultText.weight = "bold";  
  29.         resultText.text = resultArr[i];  
  30.         //私有有属性  
  31.         if(i==0){  
  32.             resultText.size = 30;  
  33.             resultText.color = "white";  
  34.             resultText.font = "HG行書体";  
  35.             resultText.x = 70;  
  36.             resultText.y = 20;  
  37.         }else if(i==1){  
  38.             resultText.size = 15;  
  39.             resultText.color = "white";  
  40.             resultText.font = "HG行書体";  
  41.             resultText.x = 105;  
  42.             resultText.y = 60;  
  43.         }else{  
  44.             resultText.size = 20;  
  45.             resultText.color = "white";  
  46.             resultText.font = "HG行書体";  
  47.             resultText.x = 35;  
  48.             resultText.y = 100 + (i-1)*32;    
  49.         }  
  50.         gameoverLayer.addChild(resultText);  
  51.     }  
  52.     //加入鼠标事件  
  53.     backLayer.addEventListener(LMouseEvent.MOUSE_DOWN,function(){  
  54.         //变量清空  
  55.         point = 0;  
  56.         time = 1000*30;  
  57.         showChara = false;  
  58.         //清空全局  
  59.         backLayer.removeAllChild();  
  60.         removeChild(backLayer);  
  61.         //游戏重开  
  62.         gameInit();  
  63.         startGame()  
  64.     });  
  65. }  
重开游戏的函数:

[javascript] view plaincopy
  1. function startGame(){  
  2.     //清空画布  
  3.     logoLayer.die();  
  4.     logoLayer.removeAllChild();  
  5.     canSnowing = false;  
  6.     //加入背景  
  7.     var backBitmapdata = new LBitmapData(imglist["background"],0,0,480,360);  
  8.     var backBitmap = new LBitmap(backBitmapdata);  
  9.     backBitmap.scaleX = 1.4;  
  10.     backBitmap.scaleY = 1.4;  
  11.     sceneLayer.addChild(backBitmap);  
  12.     //加入房屋  
  13.     var houseBitmapdata = new LBitmapData(imglist["house"],0,0,480,228);  
  14.     var houseBitmap = new LBitmap(houseBitmapdata);  
  15.     houseBitmap.scaleX = 1.4;  
  16.     houseBitmap.y = 200;  
  17.     sceneLayer.addChild(houseBitmap);  
  18.     //加入人物  
  19.     addChara();  
  20.     //加入文字  
  21.     addText();  
  22. }  

好了,运行一下代码:

哈哈~~,还不错吧。


3,源代码下载

本次开发就到这里,想了解详细代码的朋友可以看看。

下载地址:http://files.cnblogs.com/yorhom/Christmas.rar


谢谢大家阅读本文。支持就是最大的鼓励。

----------------------------------------------------------------

转自Yorhom's Game Box

http://blog.csdn.net/yorhomwang


酷毙
2

雷人
1

鲜花

鸡蛋

漂亮

刚表态过的朋友 (3 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部