2

我正在尝试编写一个带有按钮的主菜单。我有一个名为 MenuAchievementsButton 的类,在它的构造函数中,它从文档类向下传递舞台宽度和舞台高度。当我追踪它时,它是正确的宽度/高度。

我试图让按钮的高度成为舞台高度/100。我尝试过这样做。高度 = 舞台高度/100,但是每当我这样做时,即使我进入,它也会给我想要的结果的 4 倍在像 5 这样的数字中并在 Photoshop 中测量 px。

此外,当我尝试移动对象时,存在类似的问题:当我希望它向右和向下移动 20 像素时,结果却是向左和向下移动 80 像素。

任何帮助表示赞赏!

    package menus {

import flash.display.MovieClip;


public class MenuAchievementsButton extends MovieClip {
    private var _stageWidth, _stageHeight:int;

    public function MenuAchievementsButton(stageWidth:int, stageHeight:int) {
        _stageWidth = stageWidth;
        _stageHeight = stageHeight;

        alpha = 1;
        rescaleMe();
        repositionMe();
        trace(_stageWidth, _stageHeight);
    }

    private function rescaleMe():void {
        var oldWidth = this.width;
        var oldHeight = this.height;

        this.height = _stageHeight/100;
        this.width = (this.height * oldWidth) / oldHeight;
    }

    private function repositionMe():void {
        this.x = 20;
        this.y = 20;
        trace(x,y,width,height);
        trace("Stage height is: ",_stageHeight,"Stage height divided by 100 is: ", _stageHeight/100, "And the object's height, which should be stageheight / 100 is:", this.height);
        //Gives Stage height is:  800 Stage height divided by 100 is:  8 And the object's height, which should be stageheight / 100 is: 8
        //Actually, though, it's 36px!
    }
}

}

4

1 回答 1

0

代码似乎正确。那里没有问题。

是否在舞台上添加了类的实例?还是在其他物体上?

看看你是否不改变父母的规模,像这样:

var parentMC:MovieClip=new MovieClip();
addChild(parentMC);

var rectangle:Shape = new Shape; // initializing the variable named rectangle
rectangle.graphics.beginFill(0xFF0000); // choosing the colour for the fill, here it is red
rectangle.graphics.drawRect(0, 0, 100,100); // (x spacing, y spacing, width, height)
rectangle.graphics.endFill(); // not always needed but I like to put it in to end the fill
parentMC.addChild(rectangle); // adds the rectangle to the stage

trace(rectangle.width) // result 100

parentMC.scaleX=2;
trace(rectangle.width) // result 100 altough it looks like it has 200
于 2013-08-19T12:07:13.647 回答