这是Stage类构造器。和Charactor差不多。主要是其方法:- Stage.prototype.run = function(){
-
- this.y += stageSpeed;
-
- if(this.y > LStage.height){
- this.mode = "die";
- }
- this.cheackHit();
- }
- Stage.prototype.cheackHit = function(){
- if(this.y > 170 && this.x > 132 - 33 && this.x < 166){
- this.mode = "die";
- point++;
- changeText();
- }else if(this.y > 170 && this.x > 293 - 33 && this.x < 330){
- this.mode = "die";
- point++;
- changeText();
- }else if(this.y > 178 && this.x > 475 - 33 && this.x < 508){
- this.mode = "die";
- point++;
- changeText();
- }
- }
其实很好理解,在run中,我们让礼物向下移5格,虽然只移5格,但是如果是在onframe中调用,它将不断下降。为了判断礼物是否已经送到家,我用加入cheackHit方法。我们可以用判断坐标的方法来实现。每碰到一次就更改分数,并将mode设置为die,然后在onframe中判断mode,如果mode是"die"就移除这个对象。实例化Stage类: - function addStage(){
- var stage = new Stage();
- if(oldMan.mode == "left"){
- stage.x = oldMan.x + 70;
- }else{
- stage.x = oldMan.x + 30;
- }
- stage.y = 30;
- stageLayer.addChild(stage);
- stageLayer.scaleX = 0.8;
- stageLayer.scaleY = 0.8;
- }
onframe完整代码: - function onframe(event){
- showTime = Math.floor(time/1000) + "s";
- if(canSnowing == true){
-
- addSnow();
- }
- if(backSound.playing == false){
-
- backSound.play();
- }
- if(showChara == true){
-
- oldMan.move();
-
- timeText.text = "Time:" + showTime;
- if(time>0){
- time -= 30000/(30000/50);
- }else{
- playerName = getName();
- gameOver();
- }
- }
- for(var key in stageLayer.childList){
-
- stageLayer.childList[key].run();
- if(stageLayer.childList[key].mode == "die"){
-
- stageLayer.removeChild(stageLayer.childList[key]);
- }
- }
- }
首先我们新加了一个遍历方法,遍历LSprite成员而获取每对象的状态,每遇见一个mode是die的就将它移除。接下来是加入分数以及时间的函数,没有任何逻辑。大家慢慢看就能看懂的。
- function addText(){
-
- pointText = new LTextField();
- pointText.size = 15;
- pointText.x = 10;
- pointText.y = 340;
- pointText.color = "white";
- pointText.text = "Point:" + point;
- pointText.font = "HG行書体";
- overLayer.addChild(pointText);
-
- timeText = new LTextField();
- timeText.size = 15;
- timeText.x = 10;
- timeText.y = LStage.height - 30;
- timeText.color = "white";
- timeText.text = "Time:" + showTime;
- timeText.font = "HG行書体";
- overLayer.addChild(timeText);
-
- var shadow = new LDropShadowFilter(0,45,"white",0);
- overLayer.filters = [shadow];
- }
- function changeText(){
- pointText.text = "Point:" + point;
- }
以下是游戏结束调用的函数,同样是很简单:- function gameOver(){
- backLayer.die();
-
- gameoverLayer.graphics.drawRect(2,"dimgray",[0,0,400,300],true,"lightgray");
- gameoverLayer.x = 100;
- gameoverLayer.y = 50;
- gameoverLayer.scaleX = 0.5,
- gameoverLayer.scaleY = 0.5,
- gameoverLayer.alpha = 0.5,
- gameoverLayer.rotate = 50;
- var shadow = new LDropShadowFilter(5,45,"black",0);
- gameoverLayer.filters = [shadow];
-
- LTweenLite.to(gameoverLayer,1,{
- alpha:0.7,
- scaleX:1,
- scaleY:1,
- rotate:0,
- ease:Back.easeInOut,
- onComplete:resultFont
- });
- }
- function resultFont(){
- var resultArr = ["GAME OVER","Tap to Restart Game","分数:"+point,"评价:"+playerName];
- for(var i=0;i<resultArr.length;i++){
-
- resultText = new LTextField();
- resultText.weight = "bold";
- resultText.text = resultArr[i];
-
- if(i==0){
- resultText.size = 30;
- resultText.color = "white";
- resultText.font = "HG行書体";
- resultText.x = 70;
- resultText.y = 20;
- }else if(i==1){
- resultText.size = 15;
- resultText.color = "white";
- resultText.font = "HG行書体";
- resultText.x = 105;
- resultText.y = 60;
- }else{
- resultText.size = 20;
- resultText.color = "white";
- resultText.font = "HG行書体";
- resultText.x = 35;
- resultText.y = 100 + (i-1)*32;
- }
- gameoverLayer.addChild(resultText);
- }
-
- backLayer.addEventListener(LMouseEvent.MOUSE_DOWN,function(){
-
- point = 0;
- time = 1000*30;
- showChara = false;
-
- backLayer.removeAllChild();
- removeChild(backLayer);
-
- gameInit();
- startGame()
- });
- }
重开游戏的函数:- function startGame(){
-
- logoLayer.die();
- logoLayer.removeAllChild();
- canSnowing = false;
-
- var backBitmapdata = new LBitmapData(imglist["background"],0,0,480,360);
- var backBitmap = new LBitmap(backBitmapdata);
- backBitmap.scaleX = 1.4;
- backBitmap.scaleY = 1.4;
- sceneLayer.addChild(backBitmap);
-
- var houseBitmapdata = new LBitmapData(imglist["house"],0,0,480,228);
- var houseBitmap = new LBitmap(houseBitmapdata);
- houseBitmap.scaleX = 1.4;
- houseBitmap.y = 200;
- sceneLayer.addChild(houseBitmap);
-
- addChara();
-
- addText();
- }
好了,运行一下代码: 
哈哈~~,还不错吧。
3,源代码下载本次开发就到这里,想了解详细代码的朋友可以看看。 下载地址:http://files.cnblogs.com/yorhom/Christmas.rar
谢谢大家阅读本文。支持就是最大的鼓励。
---------------------------------------------------------------- 转自Yorhom's Game Box http://blog.csdn.net/yorhomwang |