上一篇,我已经模仿as,加入了LBitmap和LBitmapData类,并且用它们实现了静态图片的显示。
这次用Sprite来动态显示图片。
依然遵循上一篇对显示对象的处理的思路,添加LSprite类,并追加show方法,如下:

function LSprite(){
var self = this;
self.type = "LSprite";
self.x = 0;
self.y = 0;
self.visible=true;
self.childList = new Array()
}
LSprite.prototype = {
show:function (cood){
if(cood==null)cood={x:0,y:0};
var self = this;
if(!self.visible)return;
LGlobal.show(self.childList,{x:self.x+cood.x,y:self.y+cood.y});
},
addChild:function (DisplayObject){
var self = this;
self.childList.push(DisplayObject);
}
}

因为Sprite上可以有图片等其他的可显示对象,所以我在其构造函数里,添加了childList,用来保存它上面的所有对象。然后在调用它本身的show方法的时候,将其LGlobal循环现实其子对象。
这样一来,我们上一篇中显示图片的代码,也可以利用Sprite来显示了,代码如下:

function main(){
loader = new LLoader();
loader.addEventListener(LEvent.COMPLETE,loadBitmapdata);
loader.load("1.png","bitmapData");
}
function loadBitmapdata(event){
var bitmapdata = new LBitmapData(loader.content);
var mapimg = new LBitmap(bitmapdata); var backLayer = new LSprite();
addChild(backLayer);
backLayer.addChild(mapimg);
}

我们知道,actionscript中的Sprite可以添加EnterFrame事件,用来动态显示图片,我这里也来模仿一下,因为在LSprite类中show方法是不断循环的,所以,我只需要在show方法中不断调用一个方法,就能让其循环。
我假设有一个数组,里面存储了所有不断循环的所有方法,然后我就可以在show方法中循环这个数组,这样就达到了所有方法的循环,看下面

function LSprite(){
var self = this;
self.type = "LSprite";
self.x = 0;
self.y = 0;
self.visible=true;
self.childList = new Array()
self.frameList = new Array();
}
LSprite.prototype = {
show:function (cood){
if(cood==null)cood={x:0,y:0};
var self = this;
if(!self.visible)return;
LGlobal.show(self.childList,{x:self.x+cood.x,y:self.y+cood.y});
self.loopframe();
},
loopframe:function (){
var self = this;
var key;
for(key in self.frameList){
self.frameList[key]();
}
},
addChild:function (DisplayObject){
var self = this;
self.childList.push(DisplayObject);
}
}

光假设当然是不行的,我们需要有添加这个循环事件的方法,所以我们还需要addEventListener方法,以及移除这个事件的removeEventListener方法

addEventListener:function (type,listener){
var self = this;
if(type == LEvent.ENTER_FRAME){
self.frameList.push(listener);
}
},
removeEventListener:function (type,listener){
var self = this;
var i,length = self.frameList.length;
for(i=0;i<length;i++){
if(type == LEvent.ENTER_FRAME){
self.frameList.splice(i,1);
break;
}
}
}

该添加的都添加了,接下来,就来简单实现一个人物的行走图
先来给BitmapData类添加几个方法,用来改变图片显示的区域位置等

LBitmapData.prototype = {
setProperties:function (x,y,width,height){
var self = this;
self.x = x;
self.y = y;
self.width = width;
self.height = height;
},
setCoordinate:function (x,y){
var self = this;
self.x = x;
self.y = y;
}
}

好了,现在准备一张人物的行走图,这就让它动起来

var list = new Array();
var index = 0;
var mapimg;
var loader
var imageArray;
var animeIndex = 0;
var dirindex = 0;
var dirarr = new Array({x:0,y:1},{x:-1,y:0},{x:1,y:0},{x:0,y:-1});
function main(){
loader = new LLoader();
loader.addEventListener(LEvent.COMPLETE,loadBitmapdata);
loader.load("1.png","bitmapData");
}
function loadBitmapdata(event){
var bitmapdata = new LBitmapData(loader.content,0,0,70,92);
imageArray = LGlobal.divideCoordinate(bitmapdata.image.width,bitmapdata.image.height,8,8);
mapimg = new LBitmap(bitmapdata);
mapimg.x = 100;
mapimg.bitmapData.setCoordinate(0,0);
index = 0;
var backLayer = new LSprite();
addChild(backLayer);
backLayer.addChild(mapimg);
backLayer.addEventListener(LEvent.ENTER_FRAME, onframe)
} function onframe(){
index++;
if(index >= imageArray[0].length){
index = 0;
}
mapimg.bitmapData.setCoordinate(imageArray[dirindex][index].x,imageArray[dirindex][index].y); mapimg.x += dirarr[dirindex].x*3;
mapimg.y += dirarr[dirindex].y*3;
if(animeIndex++ > 20){
dirindex++;
if(dirindex > 3)dirindex = 0;
animeIndex = 0;
}
}

效果看下面的url,看不到效果的请下载支持html5的浏览器
http://fsanguo.comoj.com/html5/jstoas01/index.html
源码的话,直接用浏览器就可以查看了,地球人都知道

下一篇,该研究鼠标事件了

用仿ActionScript的语法来编写html5——第二篇,利用Sprite来实现动画的更多相关文章

  1. 用仿ActionScript的语法来编写html5——终篇,LegendForHtml5Programming1.0开源库件

    一,LegendForHtml5Programming1.0库件是什么?它是一个javascript库,它模仿了ActionScript的语法,用于html5的开发,目前实现的功能相对较少,还不能称之 ...

  2. 用仿ActionScript的语法来编写html5——第九篇,仿URLLoader读取文件

    第九篇,仿URLLoader读取文件 先看看最后的代码 function readFile(){ urlloader = new LURLLoader(); urlloader.addEventLis ...

  3. 用仿ActionScript的语法来编写html5——第一篇,显示一张图片

    第一篇,显示一张图片 一,代码对比 as代码: public var loader:Loader; public function loadimg():void{ loader = new Loade ...

  4. 用仿ActionScript的语法来编写html5——第八篇,图片处理+粒子效果

    用仿ActionScript的语法来编写html5系列开发到现在,应该可以做出一些东西了,下面先来研究下图片的各种效果预览各种效果看下图效果和代码看这里,看不到效果的请下载支持html5的浏览器 ht ...

  5. 用仿ActionScript的语法来编写html5——第五篇,Graphics绘图

    用仿ActionScript的语法来编写html5——第五篇,Graphics绘图 canvas本身就是一个Graphics,可以直接进行绘图在actionscript里面,每个Sprite都有一个G ...

  6. 用仿ActionScript的语法来编写html5——第六篇,TextField与输入框

    一,对比1,html5中首先看看在html5的canvas中的文字显示 var canvas = document.getElementById("myCanvas"); var ...

  7. 用仿ActionScript的语法来编写html5——第三篇,鼠标事件与游戏人物移动

    第三篇,鼠标事件与游戏人物移动 一,假设假设,所有可添加鼠标事件的对象,都有一个mouseEvent方法,添加的鼠标事件同过这个mouseEvent来调用.这样的话,添加鼠标事件,其实只需要给canv ...

  8. 用仿ActionScript的语法来编写html5——第七篇,自定义按钮

    第七篇,自定义按钮这次弄个简单点的,自定义按钮.其实,有了前面所定义的LSprite,LBitmap等类,定义按钮就很方便了.下面是添加按钮的代码, function gameInit(event){ ...

  9. 用仿ActionScript的语法来编写html5——第四篇,继承与简单的rpg

    第四篇,继承与简单的rpg 这次用继承自LSprite的类来实现简单的rpg的demo先看一下最后的代码与as的相似度 var backLayer; //地图 var mapimg; //人物 var ...

随机推荐

  1. C1编译器的实现

    总览 词法.语法分析 分析方案 词法 语法 符号表 类型系统 AST 语义检查 EIR代码生成器 MIPS代码生成器 寄存器分配 体系结构相关特性优化 使用说明 编译 运行 总览 C1语言编译器及流程 ...

  2. windows下安装vundle

    windows下安装vundle ## 前言 windows下安装vundle和linux下稍微有些不一样,虽然官网给出了 安装说明,但是有些问题的. E117: Unknown function: ...

  3. PHP学习笔记(1)数组函数

    1.数组的键值操作函数: $arr = array("小明" => 98, "小红" => 76, "小黑" => 66, ...

  4. Struts2的简单使用

    一.准备工作及实例 1.解压struts-2.1.6-all.zip apps目录:struts2自带的例子程序 docs目录:官方文档. lib 目录:存放所有jar文件. Src 目录:源文件存放 ...

  5. JVM Specification 9th Edition (4) Chapter 4. The class File Format

    Chapter 4. The class File Format Table of Contents 4.1. The ClassFile Structure 4.2. Names 4.2.1. Bi ...

  6. css 制作三角形图标 不支持IE6

    .triangle { width: 10px; height: 10px; overflow: hidden; border-left: 4px solid rgba(, , , ); border ...

  7. mysql information_schema 数据库简介:

    .CHARACTER_SETS 表 CREATE TEMPORARY TABLE `CHARACTER_SETS` ( `CHARACTER_SET_NAME` varchar() NOT NULL ...

  8. python 学习笔记 if语句

    一.if语句的格式 语句块必须有相同的缩进. 语句块必须比if,elif,else多一层缩进 # 如果条件成立则执行语句块1, # 否则 如果条件2成立则执行语句块2 # 其他情况执行语句块3 # e ...

  9. Visual Studio 2010自动添加头部注释信息

    在日常的开发中我们经常需要为我们的类库添加注释和版权等信息,这样我们就需要每次去拷贝粘贴同样的文字,为了减少这种重复性的工作,我们可以把这些信息保存在Visual Studio 2010类库模版文件里 ...

  10. page coloring小结

    页着色是一种通过选择性物理页分配来实现把虚存映射到特定cache位置的软件方法. 最早引入页着色概念是在计算机体系结构,是为了解决地址别名问题引入的. 首先对cache是使用虚拟地址还是物理地址的问题 ...