View Full Version : Stage object?
theonlycarmire
May 12th, 2007, 10:49 PM
Is there an as3 version of the Stage object? I want to trace Stage.width and Stage.height... how would I do that?
stage (not to be confused with Stage) in as3 only returns a portion of the screen.... it confuses me...
dthought
May 12th, 2007, 11:26 PM
That's because 'tis
stage.stageWidth
and
stage.stageHeight
Enjoy! :)
Edit: P.S., if the Bandwidth Profiler is displayed when testing your SWF, the stageHeight reported will be 100px less - this "bug" has been around for quite a few versions of Flash.
theonlycarmire
May 12th, 2007, 11:41 PM
Hmmm, ok so here's some code I have:
function clickState() {
trace("get clicked");
}
function Main() {
for (var i = 0; i < 200; i++) {
var newCircle:MC = new MC();
var randomValue:Number = Math.random()*1;
newCircle.x = Math.random()*stage.stageWidth;
newCircle.y = Math.random()*stage.stageHeight;
newCircle.buttonMode=true;
newCircle.scaleX = newCircle.scaleY = randomValue;
newCircle.alpha = 1-randomValue;
newCircle.addEventListener(MouseEvent.MOUSE_UP, clickState);
this.addChild(newCircle);
//trace(newCircle.alpha);
//trace(newCircle.scaleX);
}
}
Main();
What is the scope that I need to have for this? the line that says
newCircle.addEventListener(MouseEvent.MOUSE_UP, clickState);
is in a for loop, so how do I relate to the clickStage() function that is in the root? What is the parameter?
Is there an as3 equivalent of _root.clickState()?
dthought
May 13th, 2007, 12:27 AM
function clickState (anEvent:Event)
{
trace("get clicked");
}
function main ()
{
for (var i:uint = 0; i < 200; i++)
{
var newCircle:Sprite = new Sprite ();
newCircle.x = Math.random() * stage.stageWidth;
newCircle.y = Math.random() * stage.stageHeight;
newCircle.graphics.beginFill(0x000000);
newCircle.graphics.drawCircle(0, 0, 40);
newCircle.buttonMode = true;
// Yes, you can save one line, but it's harder to read
newCircle.scaleX = Math.random();
newCircle.scaleY = newCircle.scaleX;
newCircle.alpha = 1 - newCircle.scaleX;
newCircle.addEventListener(MouseEvent.MOUSE_UP, clickState);
addChild(newCircle);
}
}
main();
There you go. I fiddled with a few things - eg. multiplying by 1 is unnecessary as x * 1 == x, where x is any number. Also, I made it actually draw a circle and defined its type as Sprite (MC is not a valid class).
Event listener functions such as clickState must accept an Event passed to them.
It is not recommended that you use starting capital letters for functions or class instances, only class definitions should start with capital letters.
An equivalent scope of _root is simply stage, although it's not quite the same. In this case if you do not provide scope, and there is no local function called clickState within the main function, it traverses up the scope chain and finds the defined clickState function.
If you want to refer to the items on the display list by their name, you will need to add a .name property to each newCircle and then use getChildByName, but beware that this incurs a pretty severe performance hit over storing a numeric reference, eg. getChildAt
I haven't yet figured out the scope of addChild though - for example, addChild is not the same as stage.addChild.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.