A Flash Developer Resource Site

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Member
    Join Date
    Apr 2006
    Posts
    77

    [CS3] ActionScript 3: how do I access the stage properties from a .as file?

    I'm restarting from square one with AS 3, since I got over my head with all the OOP stuff when I first dove in, and I want to do this right. I want to write a very simple app as a first exercise. I have a .fla file (Keepaway_applet.fla) whose Document Class is set to "Keepaway". In this Flash file's library (not on the stage) is a symbol which has been given the Class "Background". This symbol is just a rectangle. I also have a file "Keepaway.as", in the same folder as the .fla file.

    I want Keepaway.as to put an instance of Background onto the stage and size it to the stage, but I can't access the stage properties of Keepaway_applet.fla from the .as file. When I run the following code in Keepaway.as, the Background symbol properly appears at the upper left of the stage, but at its original size. That is, its width and height are not being set to the stage width and height.

    Code:
    package {
    	
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	
    	public class Keepaway extends MovieClip {
    		
    		public function Keepaway() {
    			
    			var theBackground:Background = new Background;
    			addChild(theBackground);
    			theBackground.x = theBackground.y = 0; // put reg at upper left of stage
    			theBackground.width = stage.width;
    			theBackground.height = stage.height;
    			
    		}
    	}
    }
    I also tried

    Code:
    theBackground.width = theBackground.stage.width;
    theBackground.height = theBackground.stage.height;
    My understanding was that you had to access the stage through some display object on the stage, which is why I tried putting the AddChild() statement before doing the sizing. But I'd rather size the rectangle to the stage size first, then add it to the Display list.

    Can anyone tell me what I'm doing wrong?

    As always, thanks!

  2. #2
    Member
    Join Date
    Jul 2008
    Posts
    94
    Hi buddy,

    Massive apologies as i've only had time to skim read your script, not sure if its more complex than this, but i always access the stage height/width with:

    PHP Code:
    stage.stageWidth
    stage
    .stageHeight 
    When using AS3, hopefully this will be of some help!!

  3. #3
    Member
    Join Date
    Apr 2006
    Posts
    77
    Sheepysheep60,

    No apologies necessary! You solved the problem, and I'm feeling pretty sheepish myself. I don't know why they would use a different property name for stage height and width, but it's exactly the kind of thing that can "get you."

    Thanks a ton!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts




Click Here to Expand Forum to Full Width


HTML5 Development Center