1

我在课堂上很新。下面是创建一个 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);

但它没有出现。我有没有提到“这个”。错误的?

4

2 回答 2

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);
于 2013-03-28T15:54:15.267 回答
-1

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

于 2013-03-28T14:59:49.323 回答