AS3:ScaleX / ScaleY和宽度/高度的问题

时间:2013-04-10 01:51:57

标签: actionscript-3 flash class sprite

我正在使用FlashDevelop来测试我想要用于游戏的菜单式系统,而且我在解决宽度和高度或者ScaleX和ScaleY(我不是很确定)时遇到了严重的问题Sprite子类的哪个集合导致问题)。我已经阅读了许多似乎是我的问题的解决方案,包括在我添加任何子项后调整sprite子类的大小,但似乎没有工作。现在,我有3个单独的类。

主:

public class Main extends Sprite 
{

public function Main():void 
{
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void 
{
    removeEventListener(Event.ADDED_TO_STAGE, init);
    // entry point
    var BF:BFi = new BFi;
    this.addChild(BF);
    this.width = 640;
    this.height = 480;

    trace(this + "Dimensions: " + width + ", " + height);
    trace(this + "Coordinates: " + x + ", " + y);
}

}

BFI:

public class BFi extends Sprite
{
    public function BFi() 
    {
        var BFMenu:BFiMenu = new BFiMenu;
        addChild(BFMenu);
        trace(getChildAt(0));
        this.x = this.y = 0;
        this.width = 640;
        this.height = 480;
        BFMenu.x = this.y = 0;
        BFMenu.width = 640;
        BFMenu.height = 480;
        trace(this + "Dimensions: " + width + ", " + height);
        trace(this + "Coordinates: " + x + ", " + y);
    }

}

和BFiMenu:

public class BFiMenu extends Sprite
{
    [Embed(source = "../Assets/Button1.png")]private var Button1:Class;

    public function BFiMenu() 
    {
        var Bounds:Sprite = new Sprite;
        Bounds.graphics.lineStyle(2, 0, 1);
        Bounds.graphics.drawRect(0, 0, 640, 480);
        addChild(Bounds);

        var NewButton:ComplexButton = new ComplexButton(220, 405, 200, 50, Button1, "PLAY", "Block", 50, 0x000000, function Button1(e:MouseEvent):void { /*parent.removeChildAt(0);*/ } );
        //var NewButton:ComplexButton = new ComplexButton(0, 0, 200, 50, Button1, "PLAY", "Block", 50, 0x000000, function Button1(e:MouseEvent):void { parent.removeChildAt(0); } );
        addChild(NewButton);

        this.width = 640;
        this.height = 480;

        trace(this + "Dimensions: " + width + ", " + height);
        trace(this + "Coordinates: " + x + ", " + y);
        trace(NewButton + "Dimensions: " + NewButton.width + ", " +NewButton.height);
        trace(NewButton + "Coordinates: " + NewButton.x + ", " + NewButton.y);
    }
}

名为“Bounds”的矩形具有我想要的高度和宽度(640x480),但是我放置的按钮,应该具有约450的y坐标,而是具有接近800的y值,并且显然是扭曲的,但仍然在屏幕上,即使高度只有480。

调用的“ComplexButton”类是我自己的按钮类,我已经测试了足以知道它工作正常。当我运行这个程序时,它会扭曲一切,没有什么是我想要的。任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

很少注意到:

每次创建显示对象时,初始x和y坐标默认为零,因此您无需自己将其初始化为0。

更改空显示对象的宽度和高度将不起作用。如果显示对象具有图形内容,那么它将缩放该内容,使其与所需的宽度和高度匹配,这也会改变比例因子。

如果要在Disp.Object中绘制形状以赋予其宽度和高度,则不需要明确设置这些值。

尝试删除您明确设置这些值的3个地方。