简体   繁体   中英

how to make a symbol come in the middle of the screen even if the viewer resizes it in Flash

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

In your example, you are experiencing a case issue with the Number data type.

As well, you should reference stage.stageWidth and stage.stageHeight properties.

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

You may wish to compensate for the width and height of your symbol, as in:

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

Finally, listen for Event.RESIZE events to handle changes to stage size:

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;
}

As an example, here is Adobe Flash CS5 FLA source code , HTML , and compiled SWF .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM