easeljs的基础

首先创建html文件,引入esealjs文件,添加canvas标签

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>easelJs的容器</title>
    <script src="easeljs-0.8.1.min.js"></script>
    <script src="childContainer.js"></script>
    <script src="circle.js"></script>
    <style>
        #gameView{
            background-color: #0088bb;
        }
    </style>
</head>
<body>
<canvas id="gameView" width="400px" height="400px"></canvas>
<script src="container.js"></script>

</body>
</html>
1)esealjs的容器的使用

创建container.js文件

//创建stage
var stage=new createjs.Stage("gameView");
//创建container
var gameView=new createjs.Container();
//移动container的坐标位置
gameView.x=100;
gameView.y=100;
//将container添加到stage中
stage.addChild(gameView);
//创建childContainer对像
var c=new childContainer();
//将图形添加到container中
gameView.addChild(c);


stage.update();
创建childContainer.js文件

function childContainer(){
    var Rect=new createjs.Shape();
    Rect.graphics.beginFill("#FF0000");
    Rect.graphics.drawRect(0,0,50,50);
    Rect.graphics.endFill();
    var Text=new createjs.Text("1","30px Arial","#ffffff");
    this.addChild(Rect);
    this.addChild(Text);
}
childContainer.prototype=new createjs.Container();
效果如下


2)绘图

创建childContainer.js文件

//创建stage
var stage=new createjs.Stage("gameView");
//创建container
var gameView=new createjs.Container();
//将container添加到stage中
stage.addChild(gameView);
//创建子视图对像
var cl=new Circle();
cl.setCircleType(Circle.TYPE_GREEN);
//将图形添加到container中
gameView.addChild(cl);


stage.update();


创建circle.js子视图文件

function Circle(){
    createjs.Shape.call(this);
    this.setCircleType=function(type){
        switch(type){
            case Circle.TYPE_RED:
                this.setColor("#FF0000");
                break;
            case Circle.TYPE_GREEN:
                this.setColor("#00ff00");
                break;
        }
    }
    this.setColor=function(color){
        this.graphics.beginFill(color);
        this.graphics.drawCircle(100,100,50);
        this.graphics.endFill();
    }
    this.setCircleType(Circle.TYPE_RED);
}
Circle.prototype=new createjs.Shape();
Circle.TYPE_RED=1;
Circle.TYPE_GREEN=2;
效果如下:

3)事件:

以计时器事件为例

创建tick.js文件

//创建stage
var stage=new createjs.Stage("gameView");
//创建container
var gameView=new createjs.Container();
//将container添加到stage中
stage.addChild(gameView);
    var Rect=new createjs.Shape();
    Rect.graphics.beginFill("#ffffff");
    Rect.graphics.drawRect(0,0,100,100);
    Rect.graphics.endFill();

gameView.addChild(Rect);
stage.update();
    createjs.Ticker.setFPS(20);
    createjs.Ticker.addEventListener("tick",handlerTick);

var speedX=10;
function handlerTick(){
    if(Rect.x<=0){
        speedX=Math.abs(speedX);
    }
    if(Rect.x>=300){
        speedX=-Math.abs(speedX);
    }
    Rect.x +=speedX;
    stage.update();

}

效果如下:白色矩形框在画布来回滚动



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值