0
\$\begingroup\$

I want to limit the creation of objects acording to the stage width.

My method is the following:

for (var i:int = 0; i<7; i++){  

If I put something like this, it won't work

for (var i:int = 0; i<(stage.width); i++){  

What I'm doing wrong?

Full code:

    [SWF(width = 350, height = 600, frameRate = 60)]  
import com.actionsnippet.qbox.*;  
var sim:QuickBox2D = new QuickBox2D(this);  
sim.createStageWalls();  
// make a heavy circle  
sim.addCircle({x:3, y:3, radius:0.4, density:1});  
// create a few platforms  

// make 26 dominoes  
for (var i:int = 0; i<7; i++){  
//End
     sim.addCircle({x:1 + i * 1.5, y:18, radius:0.1, density:0});
     sim.addCircle({x:2 + i * 1.5, y:17, radius:0.1, density:0});
     sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
     sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});

//Mid end

      sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
      sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});

}  
sim.start();  
sim.mouseDrag();  
\$\endgroup\$
2
\$\begingroup\$

The property you want to use is stage.stageWidth.

Update: If your code throws an error, it could be that stage is not available or not yet available. In Flash CS5, the stage property should always be available. In "pure" Actionscript or Flex projects (using Flex Builder or FlashDevelop), you should make sure that your class inherits from a flash class that has the stage property and you'll also have to wait for stage to be available. A save way to make sure stage is available, is listening to the ADDED_TO_STAGE event. Eg. something like this:

public function Constructor()
{
    super();
    addEventListener(Event.ADDED_TO_STAGE, onInit)
}

private function onInit(evt:Event):void
{
    removeEventListener(evt.type, onInit);

    // stage should be accessible here. Initialize your code here!
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.