PDA

View Full Version : stage.stageWidth returning 0


salim_designer
11-19-2007, 05:07 AM
I am trying to get the stage height and width. But Flex is returning the value 0. However, if I write the code for mouse click it is working fine. I can not figure it out why it is not working.

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;

private function init():void
{
Alert.show("Stage Width"+stage.stageWidth)
}

]]>
</mx:Script>
<mx:VBox id="mainVB" creationComplete="init()">
<mx:Button label="Click Here" click="Alert.show('Stage Width '+stage.stageWidth)"/>
<mx:TextArea width="300" height="400"/>
</mx:VBox>
</mx:Application>

I simply want to center the VBox according to the stage

Daedalus
11-19-2007, 09:34 AM
as stage is a class dosent it start with a capital S?

Stage.stageWidth

just a thought

You could aviod the use of stage by using the vBoxes horizontalCenter and verticalCenter property, setting it to 0.

Also I think you can only access stage once the object you are accessing it from is on the stage. Maybe try putting the mxScript inside the vbox tags. I dont use MXML so I could be wrong here.

Insted of calling the init() function on creationComplete try using Event.ADDEDTOSTAGE, this might help.

salim_designer
11-19-2007, 10:09 AM
Thank you for your reply

Capital S for stage.stageWidth does not work. Yes I know we can use vBoxes with horizontalCenter and verticalCenter but I am working with a customized window which requires to become full screen on a button click and again resize to its original size and position on another button click. Unless I calculate the stagewidth and height I can not center the window(with Animation).

salim_designer
11-19-2007, 12:32 PM
Can anyone please let me know where I am making the mistake

dr_zeus
11-19-2007, 05:21 PM
Try listening for the stage's resize event. In some browsers, the stage resizes a couple times before it finally reaches the intended size.

salim_designer
11-20-2007, 03:51 AM
Ok this is fine but I would just like to know, why we can not get the value of stage width and heigth by using stage.StageWidth .....

Is this a bug or I am using wrong procedure.

salim_designer
11-20-2007, 05:43 AM
I tried using the stage's resize event but it is not working and I am getting the following error:

1) Call to a possibly undefined method addEventListener through a reference with static type Class

2) Access of undefined property showBox

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
[Bindable]
private var sWidth:int;

[Bindable]
private var sHeight:int;
stage.addEventListener(Event.RESIZE,showBox);

private function showBox(event:Event):void
{
mainVB.visible=true;
sWidth = stage.stageWidth;
sHeight = stage.stage.stageHeight;
mainVB.x = (sWidth/2)-(mainVB.width/2);
mainVB.y = (sHeight/2)-(mainVB.height/2);
}



]]>
</mx:Script>
<mx:Button x="800" y="0" label="Click" click="showBox(event)"/>
<mx:VBox id="mainVB" width="300" height="400" visible="false" backgroundColor="#cccccc">
<mx:Button label="Click Here" click="Alert.show('Stage Width '+stage.stageWidth)"/>
<mx:TextArea width="200" height="200"/>
</mx:VBox>
</mx:Application>

dr_zeus
11-20-2007, 05:36 PM
You're listening for the resize event in the wrong place. The stage variable is not defined as a static variable. Put it in a function, such as a handler for the applicationComplete event.

salim_designer
11-21-2007, 03:23 AM
Wow! That's great. Thank you dr_zeus :)