1

I have a simple symbol

var button:graphic = new graphic();
    button.x=250;
    button.y=200;
    addChild(button);

I want this to come in middle :

var posX:number = stage.width/2
var posY:number = stage.height/2

button.x=posX
button.y=posY

This came into my mind ^ but when i run it it shows the following errors:

1046: Type was not found or was not a compile-time constant: number. //for pos X
1046: Type was not found or was not a compile-time constant: number. //for pos Y

I find this as the only solution but sadly this is not working I may be writing those lines wrong as i am new to whole programming thing .

Please correct my solution or if it is wrong Please tell the right one

4

1 回答 1

1

在您的示例中,您遇到了Number数据类型的案例问题。

同样,您应该引用stage.stageWidthstage.stageHeight属性。

var posX:Number = stage.stageWidth / 2;
var posY:Number = stage.stageHeight / 2;

您可能希望补偿符号的宽度和高度,如下所示:

var posX:Number = (stage.stageWidth / 2) - (button.width / 2);
var posY:Number = (stage.stageHeight / 2) - (button.height / 2);

最后,监听Event.RESIZE事件以处理舞台大小的变化:

import flash.events.Event;
import flash.display.StageScaleMode;
import flash.display.StageAlign;

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

stage.addEventListener(Event.RESIZE, resizeHandler);
stage.dispatchEvent(new Event(Event.RESIZE));

function resizeHandler(event:Event):void
{
    var posX:Number = (stage.stageWidth / 2) - (button.width / 2);
    var posY:Number = (stage.stageHeight / 2) - (button.height / 2);

    button.x = posX;
    button.y = posY;
}

例如,这里是Adob​​e Flash CS5 FLA 源代码HTML和编译的SWF

于 2013-09-20T19:02:39.817 回答