Html5游戏框架createJs组件--EaselJS(一)

   现在html5小游戏越来越火爆了,由于公司业务的需要,也开发过几款微信小游戏,用canvas写的没有利用什么框架,发现性能一直不怎么好,所以楼主就只能硬着头皮去学习比较火的Adobe公司出的CreateJS框架,找了很久资料比较少,基本没什么

中文文档,很多都是英文文档(想想我这才过四级,只能借用有道,一点一点看,一点一点翻译学习),今天我就写个引子(也是在慢慢学习),写的不好,大家就不要见笑。

        EaselJS是CreateJS Suite中的一个js库,它可以让canvas的使用变得简单,它提供了一个直接了当的创建html5 canvas中使用富媒体、富交互应用的解决方案。

        (一)如何使用

        先去下载EaselJS框架,然后创建一个舞台canvas元素的舞台stage对象stage=new createjs.Stage('demo'),增加显示的子对象到舞台上英文就是DisplayObject对象stage.addChild(),然后

渲染render就用stage.update()就可以了。EaselJS 支持功能常用的createjs下相对应的对象:
        
        1.使用图像就用Bitmap位图对象。
        
        2.矢量图形就使用Shape和Graphics对象。

        3.动画Animated 位图bitmaps 使用 SpriteSheet 和 Sprite 对象。

        4.使用文本实例就用Text。

        5.包含说有DisplayObjects对象的容器就用Container。

        6.控制HTML DOM 元素的使用DOMElement。

        所有显示对象可以添加到舞台stage作为child,或直接吸引到画布canvas。

        使用说明:

        所有的在舞台上的显示对象DisplayObject(除了DOMElement),当使用mouse或touch时可以绑定事件。EaselJS支持 hover、 press和 release events,同时很容易使用drag和drop拖拽的模型。

 

        让我们看看基本的例子吧:
        
        你可以现在 illustrates里面先去实现模拟,然后去用EaselJS创建Stage和shape去实现效果。

  //Create a stage by getting a reference to the canvas
  //创建一个阶段通过引用到画布上
    stage = new createjs.Stage("demoCanvas");
        
    //Create a Shape DisplayObject.
        //创建形状显示对象
    circle = new createjs.Shape();
    circle.graphics.beginFill("red").drawCircle(0, 0, 40);
        
    //Set position of Shape instance.
        //设置形状实例的圆心坐标
    circle.x = circle.y = 50;
        
    //Add Shape instance to stage display list.
        //增加形状实例显示在舞台列表上
    stage.addChild(circle);
        
    //Update stage will render next frame
        //更新舞台渲染
    stage.update();

  

    基本的监听绑定事件,简单交互

displayObject.addEventListener("click", handleClick);
 function handleClick(event){
     // Click happenened
 }

 displayObject.addEventListener("mousedown", handlePress);
 function handlePress(event) {
     // A mouse press happened.
     // Listen for mouse move while the mouse is down:
     event.addEventListener("mousemove", handleMove);
 }
 function handleMove(event) {
     // Check out the DragAndDrop example in GitHub for more
 }

结合上面创建的cricle显示对象即:

                        //监听圆形显示对象,增加事件  displayObject.addEventListener("click", handleClick);
                         circle.addEventListener("click", handleClick);
                         function handleClick(event){
                                 // 点击时发生
                                 alert(11);
                         }

                         circle.addEventListener("mousedown", handlePress);
                         function handlePress(event) {
                                alert('鼠标按下了')
                                 // 鼠标按下时发生
                                 // 监听当鼠标按下移动时发生的事件
                                 event.addEventListener("mousemove", handleMove);
                         }
                         function handleMove(event) {
                                 // Check out the DragAndDrop example in GitHub for more
                                 console.log('鼠标移动了')
                         }                

     基本的动画例子                

 //Update stage will render next frame
    createjs.Ticker.addEventListener("tick", handleTick);

    function handleTick() {
     //Circle will move 10 units to the right.
    //圆将向右边以10为单位移动
        circle.x += 10;

        //Will cause the circle to wrap back
        //移动的距离大于舞台的距离,将重新开始
        if (circle.x > stage.canvas.width) { circle.x = 0; }
        stage.update();
    }

  

最后,EaselJS 也还有其他的功能,也简单的说一下吧:

        1.canvas阴影和CompositeOperation等特性

        2.Ticker, a global heartbeat that objects can subscribe to(有点不太懂)
        
        3.滤镜,就是像ps里面的一下滤镜遮罩,颜色通道这些。
        
        4.按钮button的功能,可以很简单的创建按钮交互。
        
        5.SpriteSheetUtils 和 SpriteSheetBuilder帮助我们构建和管理SpriteSheet 在运行的时候。


createJS的学习是系列教程,喜欢就请继续关注下期……一起成长

转载于:https://www.cnblogs.com/pingfan1990/p/4021970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
this.createjs=this.createjs||{},function(){"use strict";var a=function(a,b,c,d){this.initialize(a,b,c,d)},b=a.prototype=new createjs.Container;a.INDEPENDENT="independent",a.SINGLE_FRAME="single",a.SYNCHED="synched",b.mode,b.startPosition=0,b.loop=!0,b.currentFrame=0,b.timeline=null,b.paused=!1,b.actionsEnabled=!0,b.autoReset=!0,b.frameBounds=null,b._synchOffset=0,b._prevPos=-1,b._prevPosition=0,b._managed,b.Container_initialize=b.initialize,b.initialize=function(b,c,d,e){this.mode=b||a.INDEPENDENT,this.startPosition=c||0,this.loop=d;var f={paused:!0,position:c,useTicks:!0};this.Container_initialize(),this.timeline=new createjs.Timeline(null,e,f),this._managed={}},b.isVisible=function(){return!!(this.visible&&this;.alpha>0&&0!=this.scaleX&&0!=this.scaleY)},b.Container_draw=b.draw,b.draw=function(a,b){return this.DisplayObject_draw(a,b)?!0:(this._updateTimeline(),this.Container_draw(a,b),!0)},b.play=function(){this.paused=!1},b.stop=function(){this.paused=!0},b.gotoAndPlay=function(a){this.paused=!1,this._goto(a)},b.gotoAndStop=function(a){this.paused=!0,this._goto(a)},b.getLabels=function(){return this.timeline.getLabels()},b.getCurrentLabel=function(){return this._updateTimeline(),this.timeline.getCurrentLabel()},b.clone=function(){throw"MovieClip cannot be cloned."},b.toString=function(){return"[MovieClip (name="+this.name+")]"},b.Container__tick=b._tick,b._tick=function(b){this.paused||this.mode!=a.INDEPENDENT||(this._prevPosition=this._prevPos<0?0:this._prevPosition+1,this._updateTimeline()),this.Container__tick(b)},b._goto=function(a){var b=this.timeline.resolve(a);null!=b&&(-1==this._prevPos&&(this._prevPos=0/0),this._prevPosition=b,this._updateTimeline())},b._reset=function(){this._prevPos=-1,this.currentFrame=0},b._updateTimeline=function(){var b=this.timeline,c=this.mode!=a.INDEPENDENT;if(b.loop=null==this.loop?!0:this.loop,c?b.setPosition(this.startPosition+(this.mode==a.SINGLE_FRAME?0:this._synchOffset),createjs.Tween.NONE):b.setPosition(this._prevPos<0?0:this._prevPosition,this.actionsEnabled?null:createjs.Tween.NONE),this._prevPosition=b._prevPosition,this._prevPos!=b._prevPos){this.currentFrame=this._prevPos=b._prevPos;for(var d in this._managed)this._managed[d]=1;for(var e=b._tweens,f=0,g=e.length;g>f;f++){var h=e[f],i=h._target;if(i!=this&&!h.passive){var j=h._stepPosition;i instanceof createjs.DisplayObject?this._addManagedChild(i,j):this._setState(i.state,j)}}var k=this.children;for(f=k.length-1;f>=0;f--){var l=k[f].id;1==this._managed[l]&&(this.removeChildAt(f),delete this._managed[l])}}},b._setState=function(a,b){if(a)for(var c=a.length-1;c>=0;c--){var d=a[c],e=d.t,f=d.p;for(var g in f)e[g]=f[g];this._addManagedChild(e,b)}},b._addManagedChild=function(b,c){b._off||(this.addChildAt(b,0),b instanceof a&&(b._synchOffset=c,b.mode==a.INDEPENDENT&&b.autoReset&&!this._managed[b.id]&&b._reset()),this._managed[b.id]=2)},b.Container__getBounds=b._getBounds,b._getBounds=function(a,b){var c=this.DisplayObject_getBounds();return c||(this._updateTimeline(),this.frameBounds&&(c=this._rectangle.copy(this.frameBounds[this.currentFrame]))),c?this._transformBounds(c,a,b):this.Container__getBounds(a,b)},createjs.MovieClip=a;var c=function(){throw"MovieClipPlugin cannot be instantiated."};c.priority=100,c.install=function(){createjs.Tween.installPlugin(c,["startPosition"])},c.init=function(a,b,c){return c},c.step=function(){},c.tween=function(b,c,d,e,f,g){return b.target instanceof a?1==g?f[c]:e[c]:d},c.install()}();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值