标签:
增加猜拳次数及猜拳按钮
//初始化引擎组件 init(50, "div_caiquan", 800, 400, main); //定义游戏层,加载进度层、游戏背景层,结果显示层,出拳操作层 var loadingLayer, backLayer, resultLayer, clickLayer; //定义游戏图片 //定义图片资源加载完成后返回的图片对象 var imglist = {}; //定义图片资源数组 var imgData = new Array( { name: "title", path: "../images/title.png" }, { name: "shitou", path: "../images/shitou.png" }, { name: "jiandao", path: "../images/jiandao.png" }, { name: "bu", path: "../images/bu.png" } ); //定义自己出拳图片,电脑出拳图片 var selfBitMap, enemyBitMap; //定义出拳图片数组 var showList = new Array(); //定义结果显示文字信息 //猜拳次数,胜利次数,失败次数,平局次数文本框 var selfTextAll, selfTextWin, selfTextLoss, selfTextDraw; //胜利次数,失败次数,平局次数初始值 var win = 0, loss = 0, draw = 0; //回调函数 function main() { //创建背景层 backLayer = new LSprite(); //将背景层添加到画布 addChild(backLayer); //创建加载进度层 loadingLayer = new LoadingSample3(); //将加载进度层添加到背景层 backLayer.addChild(loadingLayer); //loadingLayer.setProgress(35); //加载图片资源 LLoadManage.load( //图片数据数组 imgData, function (progress) { //显示加载进度 loadingLayer.setProgress(progress); }, function (result) { //加载完成 //result可以认为是一个map,资源列表中的资源可以按键获取 imglist = result; //移除加载进度层 backLayer.removeChild(loadingLayer); loadingLayer = null; //游戏初始化 gameInit(); } ); }; //游戏初始化 function gameInit() { //game begin //构造出拳图片数组 showList.push(new LBitmapData(imglist["shitou"])); showList.push(new LBitmapData(imglist["jiandao"])); showList.push(new LBitmapData(imglist["bu"])); //绘制游戏界面背景 backLayer.graphics.drawRect(10, "#008800", [0, 0, LGlobal.width, LGlobal.height], true, "#000000"); //显示游戏标题 var titleBitmap = new LBitmap(new LBitmapData(imglist["title"])); titleBitmap.x = (LGlobal.width - titleBitmap.width) / 2; titleBitmap.y = 10; backLayer.addChild(titleBitmap); //显示玩家出拳图片 selfBitMap = new LBitmap(showList[0]); selfBitMap.x = 400 - selfBitMap.width - 50; selfBitMap.y = 130; backLayer.addChild(selfBitMap); //显示电脑出拳图片 enemyBitMap = new LBitmap(showList[0]); enemyBitMap.x = 400 + 50; enemyBitMap.y = 130; backLayer.addChild(enemyBitMap); //玩家电脑名称设定 var nameText; nameText = new LTextField(); nameText.text = "玩家"; nameText.weight = "bolder"; nameText.color = "#ffffff"; nameText.size = 24; nameText.x = selfBitMap.x + (selfBitMap.width - nameText.getWidth()) / 2; nameText.y = 95; backLayer.addChild(nameText); nameText = new LTextField(); nameText.text = "电脑"; nameText.weight = "bolder"; nameText.color = "#ffffff"; nameText.size = 24; nameText.x = enemyBitMap.x + (enemyBitMap.width - nameText.getWidth()) / 2; nameText.y = 95; backLayer.addChild(nameText); //结果显示层初始化 initResultLayer(); //操作层初始化 initClickLayer(); }; function initResultLayer() { //创建结果显示层对象 resultLayer = new LSprite(); //绘制结果显示层,使用Sprite的graphic绘图 resultLayer.graphics.drawRect(4, ‘#ff8800‘, [0, 0, 150, 110], true, ‘#ffffff‘); //设置结果显示层位置 resultLayer.x = 10; resultLayer.y = 100; //添加到游戏背景层 backLayer.addChild(resultLayer); //显示猜拳次数 selfTextAll = new LTextField(); selfTextAll.text = "猜拳次数 : 0"; selfTextAll.weight = "bolder"; selfTextAll.x = 10; selfTextAll.y = 20; resultLayer.addChild(selfTextAll); //显示胜利次数 selfTextWin = new LTextField(); selfTextWin.text = "胜利次数 : 0"; selfTextWin.weight = "bolder"; selfTextWin.x = 10; selfTextWin.y = 40; resultLayer.addChild(selfTextWin); //显示失败次数 selfTextLoss = new LTextField(); selfTextLoss.text = "失败次数 : 0"; selfTextLoss.weight = "bolder"; selfTextLoss.x = 10; selfTextLoss.y = 60; resultLayer.addChild(selfTextLoss); //显示平局次数 selfTextDraw = new LTextField(); selfTextDraw.text = "平局次数 : 0"; selfTextDraw.weight = "bolder"; selfTextDraw.x = 10; selfTextDraw.y = 80; resultLayer.addChild(selfTextDraw); }; function initClickLayer() { //创建操作层对象 clickLayer = new LSprite(); //绘制操作层,使用Sprite的graphic绘图 clickLayer.graphics.drawRect(4, ‘#ff8800‘, [0, 0, 300, 110], true, ‘#ffffff‘); //设置操作层显示坐标 clickLayer.x = 250; clickLayer.y = 275; //添加到游戏背景层 addChild(clickLayer); //添加“请出拳”文本 var msgText = new LTextField(); msgText.text = "请出拳:"; msgText.weight = "bolder"; msgText.x = 10; msgText.y = 10; clickLayer.addChild(msgText); //添加出拳按钮 var btnShitou = getButton("shitou"); btnShitou.x = 30; btnShitou.y = 35; clickLayer.addChild(btnShitou); var btnJiandao = getButton("jiandao"); btnJiandao.x = 115; btnJiandao.y = 35; clickLayer.addChild(btnJiandao); var btnBu = getButton("bu"); btnBu.x = 200; btnBu.y = 35; clickLayer.addChild(btnBu); }; //构造猜拳按钮 function getButton(value) { //LButton类来添加一个按钮,LButton ( upState , overState ,downState ,disableState ) ( 弹起状态,经过状态,按下状态,不可用状态) //LButton 弹起状态 var btnUp = new LBitmap(new LBitmapData(imglist[value])); //表示从注册点开始应用的对象的水平缩放比例(百分比)。默认注册点为 (0,0)。1.0 等于 100% 缩放。 btnUp.scaleX = 0.5; btnUp.scaleY = 0.5; //LButton 经过状态 var btnOver = new LBitmap(new LBitmapData(imglist[value])); //表示从注册点开始应用的对象的水平缩放比例(百分比)。默认注册点为 (0,0)。1.0 等于 100% 缩放。 btnOver.scaleX = 0.5; btnOver.scaleY = 0.5; btnOver.x = 2; btnOver.y = 2; //构造LButton var btn = new LButton(btnUp, btnOver); btn.name = value; return btn; };
finger-guessing game:3增加猜拳次数及猜拳按钮显示
标签:
原文地址:http://www.cnblogs.com/zgqys1980/p/4454175.html