上课 - 新手上课“这个。”

时间:2013-03-28 14:48:46

标签: actionscript-3

我在课堂上很新。下面是尝试创建一个StageObject类,我可以设置宽度,高度,xy和背景颜色。

 package 
    {
        import flash.display.MovieClip;



public class StageObjects extends MovieClip
    {


        public function StageObjects()
        {
            // constructor code
        }
        public function setUpStageObject(w:int, h:int, X:int, Y:int, color:Number):void
        {
            this.width = w;
            this.height = h;
            this.x = X;
            this.y = Y;

            this.cacheAsBitmap = true;
            this.graphics.beginFill(color,1);
            this.graphics.drawRect(0,0,w,h);
            this.graphics.endFill();
            this.opaqueBackground = color;
            trace("parameters: " + w + " - " + h + " - " + X + " - " + Y + " - " + color);


        }

        /*~~~ adjust position and scale functions ~~~*/
        public function adjustXY(ch:Object, par:Object):void
        {
            var w = par.width;
            var h = par.height;

            ch.x = par.x + (w - ch.width) / 2;
            ch.y = par.y + (h - ch.height) / 2;
        }

        public function adjustWH(ch:Object, par:Object):void
        {
            var w = par.width;
            var h = par.height;

        }

    }

}

在主时间轴(Flash)中,我这样做:

var titleBkg:StageObjects = new StageObjects();
titleBkg.setUpStageObject(imageBoxWidth, titleBkgHeight, -1, imageBoxHeight +1, 0x589199);
this.addChild(titleBkg);

但它没有出现。我引用了“这个”吗?错?

2 个答案:

答案 0 :(得分:1)

您没有使用addChild正确创建和绘制图形。

有效地,您的舞台看起来像这样:

Stage ¬
    0: MainTimeline:MovieClip ¬
        0: instance1:StageObjects

它需要看起来像这样:

Stage ¬
    0: MainTimeline:MovieClip ¬
        0: instance1:StageObjects ¬
            0: instance1:Shape

应该在形状上调用图形调用,而不是动画片段。您也可以在第一次通话中使用一行而不是两行进行此设置。

package {
    import flash.display.MovieClip;

    public class StageObjects extends MovieClip {
        public function StageObjects(w:int, h:int, X:int, Y:int, color:uint) {
            // Constructor
            this.x = X;
            this.y = Y;

            var rect:Shape = new Shape();
            rect.graphics.beginFill(color,1);
            rect.graphics.drawRect(0,0,w,h);
            rect.graphics.endFill();
            addChild(rect);
            trace("parameters: " + w + " - " + h + " - " + X + " - " + Y + " - " + color);
        }


        public function adjustXY(ch:Object, par:Object):void {
            // adjust position and scale functions
            var w = par.width;
            var h = par.height;

            ch.x = par.x + (w - ch.width) / 2;
            ch.y = par.y + (h - ch.height) / 2;
        }

        public function adjustWH(ch:Object, par:Object):void {
            var w = par.width;
            var h = par.height;
        }
    }
}

创建对象将简化为:

var titleBkg:StageObjects = new StageObjects(imageBoxWidth, titleBkgHeight, -1, imageBoxHeight +1, 0x589199);
this.addChild(titleBkg);

答案 1 :(得分:-1)

我想你已经在构造函数中声明了宽度,高度,图形等。在我的课程代码中,我从不使用'this。'。如果变量声明为类私有/公共编译器将不允许您声明具有相同名称的另一个变量。所以你不必使用'this。',但是当你使用'this'时它更具可读性。 (你会知道它的类变量)。
你有没有为舞台添加你的图形(不是titleBkg,而是你在titleBkg对象中创建的对象)? ;)