Erget 显示对象

核心显示类:

描述
DisplayObject 显示对象基类,所有显示对象均继承自此类
Bitmap 位图,用来显示图片
Shape 用来显示矢量图,可以使用其中的方法绘制矢量图形
TextField 文本类
BitmapText 位图文本类
DisplayObjectContainer 显示对象容器接口,所有显示对象容器均实现此接口
Sprite:DisplayObjectContainer 带有矢量绘制功能的显示容器
Stage:DisplayObjectContainer 舞台类

根节点:  

  Egret 的教程说 “每个Egret应用有且只有一个stage对象”,直接看 Egret 的 Player 播放器代码:

            p.init = function (container, options) {
                var option = this.readOption(container, options);
                var stage = new egret.Stage();
                stage.$screen = this;
                stage.$scaleMode = option.scaleMode;
                stage.$orientation = option.orientation;
                stage.$maxTouches = option.maxTouches;
                stage.frameRate = option.frameRate;
                stage.textureScaleFactor = option.textureScaleFactor;
                var buffer = new egret.sys.RenderBuffer(undefined, undefined, true);
                var canvas = buffer.surface;
                this.attachCanvas(container, canvas);
                var webTouch = new web.WebTouchHandler(stage, canvas);
                var player = new egret.sys.Player(buffer, stage, option.entryClassName);
                var webHide = new egret.web.WebHideHandler(stage);
                var webInput = new web.HTMLInput();
                player.showPaintRect(option.showPaintRect);
                if (option.showFPS || option.showLog) {
                    player.displayFPS(option.showFPS, option.showLog, option.logFilter, option.fpsStyles);
                }
                this.playerOption = option;
                this.container = container;
                this.canvas = canvas;
                this.stage = stage;
                this.player = player;
                this.webTouchHandler = webTouch;
                this.webInput = webInput;
                this.webHide = webHide;
                egret.web.$cacheTextAdapter(webInput, stage, container, canvas);
                this.updateScreenSize();
                this.updateMaxTouches();
                player.start();
            p.initialize = function () {
                var rootClass;
                if (this.entryClassName) {
                    rootClass = egret.getDefinitionByName(this.entryClassName);
                }
                if (rootClass) {
                    var rootContainer = new rootClass();
                    this.root = rootContainer;
                    if (rootContainer instanceof egret.DisplayObject) {
                        this.stage.addChild(rootContainer);
                    }
                    else {
                        DEBUG && egret.$error(1002, this.entryClassName);
                    }
                }
                else {
                    DEBUG && egret.$error(1001, this.entryClassName);
                }

  可以清楚地看到,egret 将 Main 这个 DisplayObjectContainer 作为了播放器的 rootContainer,并在初始化时就为 Main.stage 创建了舞台对象。

显示对象的基本概念:

  • 坐标:x, y
  • 缩放比例:scaleX, scaleY
  • 透明通道:alpha
  • 旋转操作:rotation
  • 设置锚点:anchorOffsetX, anchorOffsetY
  • 斜切:skewX, skewY

显示容器:

  可以包含多个 DisplayObject。

    private createGameScene():void
    {
        console.log(this.stage.stageWidth);
        console.log(this.stage.stageHeight);

        console.log("Runtime start.");

        var testCGrid = new MyGrid();
        testCGrid.drawGrid();
        this.addChild(testCGrid);

        var testContainer = new MyContainer();
        testContainer.drawGrid();
        this.addChild(testContainer);

        console.log("Runtime end.");
    }
}

class MyGrid extends egret.Shape
{
    public constructor()
    {
        super();
        this.drawGrid();
    }

    public drawGrid()
    {
        this.graphics.beginFill(0x0000ff);
        this.graphics.drawRect(0,0,50,50);
        this.graphics.endFill();
        this.graphics.beginFill(0x0000ff);
        this.graphics.drawRect(50,50,50,50);
        this.graphics.endFill();
        this.graphics.beginFill(0xff0000);
        this.graphics.drawRect(50,0,50,50);
        this.graphics.endFill();
        this.graphics.beginFill(0xff0000);
        this.graphics.drawRect(0,50,50,50);
        this.graphics.endFill();
    }
}

class MyContainer extends egret.DisplayObjectContainer
{
    public constructor()
    {
        super();
        this.drawGrid();
    }

    public drawGrid() {
        var myGrid = new egret.Shape();
        myGrid.graphics.beginFill(0x00ff00);
        myGrid.graphics.drawRect(200,200,50,50);
        myGrid.graphics.endFill();
        myGrid.graphics.beginFill(0x00ff00);
        myGrid.graphics.drawRect(250,250,50,50);
        myGrid.graphics.endFill();
        myGrid.graphics.beginFill(0xff0000);
        myGrid.graphics.drawRect(250,200,50,50);
        myGrid.graphics.endFill();
        myGrid.graphics.beginFill(0xff0000);
        myGrid.graphics.drawRect(200,250,50,50);
        myGrid.graphics.endFill();
        this.addChild(myGrid);

        var myRect = new egret.Shape();
        myRect.graphics.beginFill(0xc000c0);
        myRect.graphics.drawRect(200,0, 100, 50);
        myRect.graphics.endFill();
        this.addChild(myRect);
    }
class MySprite extends egret.Sprite {
    public constructor() {
        super();
        this.drawGrid();
    }

    private drawGrid() {
        this.graphics.beginFill(0x00ff00);
        this.graphics.drawRect(200,200,50,50);
        this.graphics.endFill();
        this.graphics.beginFill(0x00ff00);
        this.graphics.drawRect(250,250,50,50);
        this.graphics.endFill();
        this.graphics.beginFill(0xff0000);
        this.graphics.drawRect(250,200,50,50);
        this.graphics.endFill();
        this.graphics.beginFill(0xff0000);
        this.graphics.drawRect(200,250,50,50);
        this.graphics.endFill();

        var myRect = new egret.Shape();
        myRect.graphics.beginFill(0xc000c0);
        myRect.graphics.drawRect(200,0,100,50);
        myRect.graphics.endFill();
        this.addChild(myRect);
    }
}
  • 第一个是一个自定义对象类;
  • 第二个是一个自定义容器类,包含两个自定义对象类。
  • DisplayContainer 是 Sprite 的话,由于其实现了 graphics 也是可以直接被根容器添加并显示的,它的child也会被显示。

访问容器子对象与深度:

  • getChildByName()
  • getChildAt()

  按官方说法,推荐用显示层级来获取对象效率更高。

  值得注意的是,如果对象是一个容器,容器内又包含多个对象时的层次处理。

时间: 2024-08-24 19:47:25

Erget 显示对象的相关文章

AS3 调用gotoAndStop后,显示对象为null的解决方法

再使用flash制作UI资源时通常我们会在一个MC的不同帧中引入其他的MC(有可能引入的MC中还包含了MC),并为这些引入的MC设置实例名称.通过使用gotoAndStop到第几帧来引用里面的资源...可惜有的时候会报:引入的这个显示对象为null....这该如何是好呢?? 接下来就是我在使用FB与IDEA进行实际工作的时候找到的一些解决办法... 第一种: 有时候我们使用的已经是别人做好的UI...或者是自己通过反编译出来的UI... 用flash打开后导入IDEA中如果使用gotoAndSt

显示对象的像素级碰撞检测

mc2.gotoAndStop(1); var bmd1 = new BitmapData(mc1.width, mc1.height, true, 0); bmd1.draw(mc1); var bmd2 = new BitmapData(mc2.width, mc2.height, true, 0); bmd2.draw(mc2); stage.addEventListener(Event.ENTER_FRAME,frameFunc); function frameFunc(e:Event)

AS3显示对象继承关系图(转)

要学好AS3,先得把继承关系搞清楚.下面就是AS3的继承关系 DisplayObject InteractiveObject              Bitmap Shape    Video     AVMIMovie    StaticText   MorpShape DisplayObjectContainer     SimpleButton    TextField Sprite                                Stage                

Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析

上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种场合,不是做为参数就是做为返回值,那么这一章节我们就去看看这个Node类到底在Cocos2d-x里处于一个什么样的地位. 直接进入主题,我们打开CCNode.h文件.我去,这个文件有1500行,这么长怎么看啊,放松一下整体看过一遍,松了一口气,还好,还没那么糟,文件虽然大,注释占了有90%的篇幅,代

3, 容器内部显示对象相对源的切换 和 事件的添加

class Main extends egret.DisplayObjectContainer { /** * 入口文件, 最先执行的构造方法 * 这会实例化一个和手机屏幕一样大的舞台 */ public constructor() { super(); this.once( egret.Event.ADDED_TO_STAGE, this.onAddToStage, this ); } /** * 入口文件加载成功后执行的方法 * 也是逻辑的开始 */ private onAddToStage

Arcgis for Js之鼠标经过显示对象名的实现

在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来说是很实用的,本文讲述在Arcgis for Js中,用两种不同的方式来实现该效果. 为了有个直观的概念,先给大家看看实现后的效果: 百度地图的效果 效果1 效果2 直观的看到了效果,下面说说在Arcgis for Js中实现的两种方式.在实现给效果的时候,有layer的两个事件,mouse-over和mouse-out事件,鼠标经过显示对象名称,鼠标移除清除显示. 1.通过TextSymbol和GraphicMar

【HTML5 Canvas】计算元件/显示对象经过Matrix变换后在上级/舞台上的bounds(边界矩形rect)

如上图所示,这样的一个简单矩形,边界矩形是(x:-28, y:-35, width:152, height:128),这是在这个元件/显示对象自己的坐标空间的范围. 那么把这个放到父元件(舞台)中,再做一定变换.如下图所示,白色区域就是舞台,蓝色矩形中的白色十字架标记,就是世界坐标的(0,0)点.       在舞台这个世界坐标系中,边界区域又是什么呢?我们的目标就是计算下图中的红色区域. 其实算法,很简单,在放到舞台之前,在蓝色矩形自己的局部坐标系中,边界是(x:-28, y:-35, wid

ListBox之类控件的Item项显示对象的两个属性

wpf项目中,ListBox绑定对象集合,ListBoxItem要显示对象的两个属性,例如:显示员工的工号和姓名. 之前我的做法是在Employee员工类中添加一个"NumAndName",属性,给员工对象的工号属性赋值.姓名属性赋值时,同时给“NumAndName”属性 赋值为“工号”+“姓名”两个属性拼接的字符串,ListBox绑定员工的集合,ListBox的Item绑定"NumAndName"属性,这样就同时显示了工号和姓名. 今天,发现了一个简单的方法,即:

Flex4的可视化显示对象

flex3中用addChild(child:DisplayObject) 增加显示对象,flex4中用addElement(element:IVisualElement).绝大多数的flex3显示控件都是从DispalyObject继承的,所以在flex3中增加显示对象非常容易.一旦到了flex4,要显示sprite shape Bitmap都会报错,因为都没有实现IVisualElement接口.习惯了flex3,再用flex4的时候还真有点不适应了. 从adobe帮助文档可以看到直接实现IV