- 注册时间
- 2016-11-21
- 最后登录
- 2017-10-28
- 阅读权限
- 30
- 积分
- 231
- 精华
- 0
- 帖子
- 65
|
在一个 a = new LSprize()注册了一个监听事件以后, a.x =100
a.addEventListener(LMouseEvent.MOUSE_UP, a.onClick);
此时 a的监听不在已经偏移的100 这个位置,而是还在原来的那个地方。导致了点击无效
这个 是在 chrome 模拟ipad 的情况下出现- $timeout(function(){
- width = $(window).width();
- height = $(window).height();
- g_rem = parseFloat($('html').css('font-size').replace('px',''));
- ratio = g_rem/37.5
- if (!D.can_load){
- init(50 ,"legend",width,height,main_fake);
- D.can_load = true
-
- }
- if(LGlobal.os == OS_ANDROID){
- LGlobal.preventDefault = false;
- }
- function main_fake(){}
- D.go_go = function(){
- var imgBmpd;
- /** 游戏层 */
- var position = document.getElementById('legend_canvas').getBoundingClientRect()
- var stageLayer, gameLayer, overLayer;
- /** 拼图块列表 */
- var blockList;
- /** 是否游戏结束 */
- var isGameOver;
- /** 用时 */
- var startTime, time, timeTxt;
- /** 步数 */
- var steps, stepsTxt;
-
- function main () {
- /** 全屏设置 */
- if (LGlobal.mobile) {
- LGlobal.stageScale = LStageScaleMode.SHOW_ALL;
- }
- LGlobal.screen(LGlobal.FULL_SCREEN);
-
- /** 添加加载提示 */
- var loadingHint = new LTextField();
- loadingHint.text = "资源加载中……";
- loadingHint.size = 20;
- loadingHint.x = (LGlobal.width - loadingHint.getWidth()) / 2;
- loadingHint.y = (LGlobal.height - loadingHint.getHeight()) / 2;
- addChild(loadingHint);
-
- /** 加载图片 */
- LLoadManage.load(
- [
- {name : "img", path : "http://s2.d2scdn.com/2016/11/12/f483a188-e959-41cc-a93c-495617c20b15/img.jpg"}
- ],
- null,
- function (result) {
- /** 移除加载提示 */
- loadingHint.remove();
-
- /** 保存位图数据,方便后续使用 */
- imgBmpd = new LBitmapData(result["img"]);
-
- gameInit();
- }
- );
- }
- function Block (index, x, y) { //根据传入的 x(横轴),y(数轴)
- LExtends(this, LSprite, []);
-
- var bmpd = imgBmpd.clone();
- bmpd.setProperties(x * 130, y * 130, 130, 130);
- this.bmp = new LBitmap(bmpd);
- this.bmp.scaleX = 0.97*ratio;
- this.bmp.scaleY = 0.97*ratio;
- this.addChild(this.bmp);
- // this.graphics.drawRect(2, "red", [0, 0, 130*0.97*ratio, 130*0.97*ratio]);
- var border = new LShape();
- border.graphics.drawRect(1, "#CCCCCC", [-position.left, 0, 130*0.97*ratio, 130*0.97*ratio]);
- this.addChild(border);
-
- this.index = index;
- this.addEventListener(LMouseEvent.MOUSE_UP, this.onClick);
- }
-
- Block.getBlock = function (x, y) {
- return blockList[y * 3 + x];
- };
-
- Block.isGameOver = function () {
- var reductionAmount = 0, l = blockList.length;
-
- /** 计算还原度 */
- for (var i = 0; i < l; i++) {
- var b = blockList[i];
-
- if (b.index == i) {
- reductionAmount++;
- }
- }
-
- /** 计算是否完全还原 */
- if (reductionAmount == l) {
- /** 游戏结束 */
- // gameOver();
-
- D.score = time/1000
- D.game_end(D.score)
- }
- };
-
- Block.exchangePosition = function (b1, b2) {
- var b1x = b1.locationX, b1y = b1.locationY,
- b2x = b2.locationX, b2y = b2.locationY,
- b1Index = b1y * 3 + b1x,
- b2Index = b2y * 3 + b2x;
-
- /** 在地图块数组中交换两者位置 */
- blockList.splice(b1Index, 1, b2);
- blockList.splice(b2Index, 1, b1);
-
- /** 交换两者显示位置 */
- b1.setLocation(b2x, b2y);
- b2.setLocation(b1x, b1y);
-
- /** 判断游戏是否结束 */
- Block.isGameOver();
- };
-
- Block.prototype.setLocation = function (x, y) {
- this.locationX = x;
- this.locationY = y;
-
- this.x = x * 130*0.97*ratio;
- // this.bmp.x -= position.left
- this.y = y * 130*0.97*ratio;
- };
-
- Block.prototype.onClick = function (e) {
- var self = e.currentTarget;
-
- if (isGameOver) {
- return;
- }
-
- var checkList = new Array();
- console.log (self)
- /** 判断右侧是否有方块 */
- if (self.locationX > 0) {
- checkList.push(Block.getBlock(self.locationX - 1, self.locationY));
- }
-
- /** 判断左侧是否有方块 */
- if (self.locationX < 2) {
- checkList.push(Block.getBlock(self.locationX + 1, self.locationY));
- }
-
- /** 判断上方是否有方块 */
- if (self.locationY > 0) {
- checkList.push(Block.getBlock(self.locationX, self.locationY - 1));
- }
-
- /** 判断下方是否有方块 */
- if (self.locationY < 2) {
- checkList.push(Block.getBlock(self.locationX, self.locationY + 1));
- }
-
- for (var i = 0, l = checkList.length; i < l; i++) {
- var checkO = checkList[i];
-
- /** 判断是否是空白拼图块 */
- if (checkO.index == 8) {
- steps++;
- updateStepsTxt();
-
- Block.exchangePosition(self, checkO);
-
- break;
- }
- }
- };
- function gameInit (e) {
- /** 初始化舞台层 */
- stageLayer = new LSprite();
- stageLayer.graphics.drawRect(0, "", [0, 0, LGlobal.width, LGlobal.height], true, "#EFEFEF");
- addChild(stageLayer);
-
- /** 初始化游戏层 */
- gameLayer = new LSprite();
- stageLayer.addChild(gameLayer);
- /** 初始化最上层 */
- overLayer = new LSprite();
- stageLayer.addChild(overLayer);
-
- /** 添加开始界面 */
- startGame();
- }
- /*
- function addBeginningUI () {
- var beginningLayer = new LSprite();
- beginningLayer.graphics.drawRect(0, "", [0, 0, LGlobal.width, LGlobal.height], true, "#EDEDED");
- stageLayer.addChild(beginningLayer);
-
- var title = new LTextField();
- title.text = "拼图游戏";
- title.size = 50;
- title.weight = "bold";
- title.x = (LGlobal.width - title.getWidth()) / 2;
- title.y = 160;
- title.color = "#FFFFFF";
- title.lineWidth = 5;
- title.lineColor = "#000000";
- title.stroke = true;
- beginningLayer.addChild(title);
-
- var hint = new LTextField();
- hint.text = "- 点击屏幕开始游戏 -";
- hint.size = 25;
- hint.x = (LGlobal.width - hint.getWidth()) / 2;
- hint.y = 370;
- beginningLayer.addChild(hint);
-
- beginningLayer.addEventListener(LMouseEvent.MOUSE_UP, function () {
- beginningLayer.remove();
-
- startGame();
- });
- }
- */
-
- function startGame () {
- isGameOver = false;
-
- /** 初始化时间和步数 */
- startTime = (new Date()).getTime();
- time = 0;
- steps = 0;
- /** 初始化拼图块列表 */
- initBlockList();
- /** 打乱拼图 */
- max_block = blockList.pop()
- getRandomBlockList();
- blockList.push(max_block)
- /** 显示拼图 */
- showBlock();
- /** 显示缩略图 */
- showThumbnail();
- /** 显示时间 */
- addTimeTxt();
- /** 显示步数 */
- addStepsTxt();
-
- stageLayer.addEventListener(LEvent.ENTER_FRAME, onFrame);
- }
-
- function initBlockList () {
- blockList = new Array();
-
- for (var i =0 ; i < 9; i++) {
- /** 根据序号计算拼图块图片显示位置 */
- var y = (i / 3) >>> 0, x = i % 3;
-
- blockList.push(new Block(i, x, y));
- }
- }
-
- function getRandomBlockList () {
- /** 随机打乱拼图 */
- blockList.sort(function () {
- return 0.5 - Math.random();
- });
-
-
- /** 计算逆序和 */
- var reverseAmount = 0;
-
- for (var i = 0, l = blockList.length, preBlock = null; i < l; i++) {
- for (var j =i+1; j<l; j++){
- if (blockList[i].index>blockList[j].index){
- reverseAmount ++
- }
- }
- }
- /** 检测打乱后是否可还原 */
- if (reverseAmount % 2 != 0) {
- /** 不合格,重新打乱 */
- getRandomBlockList();
- }
-
- }
-
- function showBlock() {
- for (var i = 0, l = blockList.length; i < l; i++) {
- var b = blockList[i];
-
- /** 根据序号计算拼图块位置 */
- var y = (i / 3) >>> 0, x = i % 3;
-
- b.setLocation(x, y);
-
- gameLayer.addChild(b);
- }
- }
-
- function showThumbnail() {
- var thumbnail = new LBitmap(imgBmpd);
- thumbnail.scaleX = 130 / imgBmpd.width *0.97*ratio;
- thumbnail.scaleY = 130 / imgBmpd.height *0.97*ratio;
- thumbnail.x = 3.4*g_rem;
- thumbnail.y = 11.5*g_rem;
- overLayer.addChild(thumbnail);
- }
-
- function addTimeTxt () {
- timeTxt = new LTextField();
- timeTxt.stroke = true;
- timeTxt.lineWidth = g_rem/11;
- timeTxt.lineColor = "#54D9EF";
- timeTxt.color = "#FFFFFF";
- timeTxt.size = 0.4*g_rem;
- timeTxt.x = 0.45*g_rem;
- timeTxt.y = 12.5*g_rem;
- overLayer.addChild(timeTxt);
-
- updateTimeTxt();
- }
-
- function updateTimeTxt () {
- timeTxt.text = "时间:" + getTimeTxt(time);
- }
-
- function getTimeTxt () {
- var d = new Date(time);
-
- return d.getMinutes() + " : " + d.getSeconds();
- };
-
- function addStepsTxt () {
- stepsTxt = new LTextField();
- stepsTxt.stroke = true;
- stepsTxt.lineWidth = g_rem/11;
- stepsTxt.lineColor = "#54D9EF";
- stepsTxt.color = "#FFFFFF";
- stepsTxt.size = 0.45*g_rem;
- stepsTxt.y = 12.5*g_rem;
- overLayer.addChild(stepsTxt);
-
- updateStepsTxt();
- }
-
- function updateStepsTxt () {
- stepsTxt.text = "步数:" + steps;
-
- stepsTxt.x = LGlobal.width - stepsTxt.getWidth() - 20;
- }
-
- function onFrame () {
- if (isGameOver) {
- return;
- }
-
- /** 获取当前时间 */
- var currentTime = (new Date()).getTime();
-
- /** 计算使用的时间并更新时间显示 */
- time = currentTime - startTime;
- updateTimeTxt();
- }
-
- main()
- }
- },10);
复制代码 |
|