美文网首页
egret基础

egret基础

作者: coffee1949 | 来源:发表于2019-07-07 11:59 被阅读0次

    显示对象:

    具体的显示出来的对象,就是我们在页面上看到的东西:
    比如:

        一个矩形区域
        一句话,一段文本
        一条线
        一张图片
        等等...
    

    显示对象公共属性

    x:显示对象x轴坐标
    y:显示对象y轴坐标
    width:显示对象宽
    height:显示对象高
    scaleX:显示对象x轴缩放比例
    scaleY:显示对象y轴缩放比例
    alpha:显示对象透明度
    rotation:旋转角度
    skewY:斜切-扭曲
    skewX:斜切-扭曲
    visible:是否可见
    

    显示对象的父类

    就是用来创建显示对象的类

    egret.Bitmap:位图显示和操作:显示一张已有图片
    egret.Shape:矢量图绘制的显示对象:自己绘制、设计的图片
    egret.TextField:文本操作
    egret.TextInput:文本操作
    
    

    创建矩形显示对象

    // 创建一个矩形
    let topMask = new egret.Shape();
            // 绘制矩形区域,开始填充
            topMask.graphics.beginFill(0xff0000, 0.5);
            // 高度是172
            topMask.graphics.drawRect(0, 0, stageW, 172);
            topMask.graphics.endFill();
            // topMask.y = 33;
            this.addChild(topMask);
    

    创建文本显示对象

    // 创建一个文本
    //new egret.TextField()
    // 填充文字:hello egret
            let colorLabel = new egret.TextField();
            // 文字颜色
            colorLabel.textColor = 0xffffff;
            // 文字的可用宽
            colorLabel.width = stageW - 172;
            // 对齐方式
            colorLabel.textAlign = "center";
            // 文字内容
            colorLabel.text = "Hello Egret";
            // 文字大小
            colorLabel.size = 24;
            // 这些x,y都是相对于0,0坐标
            colorLabel.x = 172;
            colorLabel.y = 80;
            this.addChild(colorLabel);
    

    创建mask遮罩

    class Main extends egret.DisplayObjectContainer{
        public constructor(){
            super()
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.start,this)
        }
    
        private start(event:egret.Event){
            // 创建一个矩形
            let rect:egret.Shape = new egret.Shape()
            rect.graphics.beginFill(0xff0000)
            rect.graphics.drawRect(0,0,200,200)
            rect.graphics.endFill()
            rect.alpha = 0.3
            this.addChild(rect)
    
            // 创建一个圆形
            let cir:egret.Shape = new egret.Shape()
            cir.graphics.beginFill(0x00ff00)
            cir.graphics.drawCircle(0,0,50)
            cir.graphics.endFill()
            cir.x = 50
            cir.y = 50
            this.addChild(cir)
    
            // 创建一个遮罩层
            let mask:egret.Rectangle = new egret.Rectangle(50,50,100,100)
            // 把遮罩层当做rect的遮罩
            rect.mask = mask
        }
    }
    

    碰撞检测

    class Main extends egret.DisplayObjectContainer{
        public constructor(){
            super()
            // 监听添加舞台事件
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.start,this)
        }
    
        // 初始化函数
        private start(event:egret.Event){
            // 这里要的
            this.drawTetx()
    
            let shp:egret.Shape = new egret.Shape()
            shp.graphics.beginFill(0xff0000)
            shp.graphics.drawRect(0,0,100,100)
            shp.graphics.endFill()
            this.addChild(shp)
            
            // 碰撞检测最关键的代码:hitTestPoint(10,10)
            let isHit:boolean = shp.hitTestPoint(99,82)
            // let isHit:boolean = shp.hitTestPoint(99,82,true)
            this.infoText.text = '碰撞检测:' + isHit
        }
    
    
        // 私有属性:是一个文本类型
        private infoText:egret.TextField
        // 绘制文本方法,给infoText赋值并显示在舞台上
        private drawTetx(){
            this.infoText = new egret.TextField()
            this.infoText.y = 150
            this.infoText.text='碰撞检测'
            this.addChild(this.infoText)
        }
    }
    

    自定义显示对象类

    自定义显示对象类需要继承自DisplayObject的具体子类,而不是DisplayObject类

    那么DisplayObject的具体子类有哪些呢:

    egret.Bitmap:位图显示和操作:显示一张已有图片
    egret.Shape:矢量图绘制的显示对象:自己绘制、设计的图片
    egret.TextField:文本操作
    egret.TextInput:文本操作

    例如:自定义显示对象类:MyCard

    class MyCard extends egret.Shape{
        public constructor(){
            super()
            this.drawCard()
        }
    
        private drawCard(){
            this.graphics.beginFill(0x00ff00)
            this.graphics.drawRect(0,0,50,50)
            this.graphics.drawRect(50,50,50,50)
            this.graphics.beginFill(0x00ff00)
            this.graphics.drawRect(0,50,50,50)
            this.graphics.drawRect(50,0,50,50)
            this.graphics.endFill()
        }
    }
    

    使用自定义的显示对象类:MyCard类

    class Main extends egret.DisplayObjectContainer{
        public constructor(){
            super()
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.init,this)
        }
    
        private init(event:egret.Event){
            let rect:MyCard = new MyCard()
            this.addChild(rect)
    
            let rect2:MyCard = new MyCard()
            rect2.y = 100
            rect2.x = 100
            this.addChild(rect2)
        }
    }
    

    锚点

    class Main extends egret.DisplayObjectContainer{
        public constructor(){
            super()
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.init,this)
        }
    
        private init(event:egret.Event){
            let shp:egret.Shape = new egret.Shape()
            shp.graphics.beginFill(0xff0000)
            shp.graphics.drawRect(0,0,100,100)
            shp.graphics.endFill()
    
            // 向右移动50
            // shp.anchorOffsetX = -50
    
            // 向左移动50
            // shp.anchorOffsetX = 50
            this.addChild(shp)
        }
    }
    

    相关文章

      网友评论

          本文标题:egret基础

          本文链接:https://www.haomeiwen.com/subject/cqpyhctx.html