2 Replies - 1347 Views - Last Post: 31 January 2012 - 03:17 PM

#1 leafdj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-February 11

Stage dimensions messed up in my main method?

Posted 29 January 2012 - 07:01 PM

Hey everyone, so I'm making my first game for the Global Game Jam this year, and it's been going pretty smoothly, just until I put in a title screen just now.
The method looks like this:
var startButton:StartButton;
		var logo:Title;
		var restart:RestartButton;

public function EightyEightJazz() {	
			
			logo = new Title();
			logo.x = stage.width/2;
			logo.y = stage.height/5;
			addChild(logo);
			
			trace(stage.height + "  " +stage.width);
			startButton = new StartButton();
			startButton.x = stage.width/2;
			startButton.y = 300;
			addChild(startButton);
			
			startButton.addEventListener(MouseEvent.CLICK, function (e:MouseEvent){
										 jazz();
										 });
				
			}


And all it's doing is putting everything on the screen, and waiting for the user to use the start button before it calls the main game method.

For some reason though, my stage dimensions are the dimensions of my Title symbol. The window stays the same size, but if I trace(stage.width + " " + stage.height), or try to use the width and height to set the positions of my objects, then it gets all wonky.

The strangest thing though, is that as soon as it gets into the jazz() method, the stage gets fixed again.

This is the first flash thing I've ever done, so I've only been able to try a few things, but setting focus to the stage didn't help, and I can't seem to figure out why it's doing this.

Any help would be fantastic, and thanks for taking the time to read this.

Is This A Good Question/Topic? 0
  • +

Replies To: Stage dimensions messed up in my main method?

#2 leafdj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 05-February 11

Re: Stage dimensions messed up in my main method?

Posted 30 January 2012 - 05:09 AM

Additionally, in the jazz() method that gets called,
                                if (startButton)
				{
					removeChild(startButton);
				}
				if (logo)
				{
					removeChild(logo);
				} 



These work the first time, but the second time the jazz() method runs, it crashes, throwing

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at EightyEightJazz/jazz()
at Function/<anonymous>()

I kind of know what the problem is, but after scouring the internet, I still can't figure out why my main method is using the logo as a stage, even when all of my other methods are fine. Making an intro method that does what main should doesn't help either, I get the same errors.

However, this doesn't crash:
                                if (startButton.stage)
				{
					removeChild(startButton);
				}
				if (logo.stage)
				{
					removeChild(logo);
				} 
if (restart)
					removeChild(restart);



Where restart is created in a separate gameOver() method, and for some reason doesn't need to check it's stage?

This post has been edited by leafdj: 30 January 2012 - 05:16 AM

Was This Post Helpful? 0
  • +
  • -

#3 jasoncummer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-January 12

Re: Stage dimensions messed up in my main method?

Posted 31 January 2012 - 03:17 PM

The following link explains what is happen very well:
http://flexmusings.w...ge_stageheight/

if you have this code:
public function EiEiJazz() {	
				trace("1 " + stage.height + "  " +stage.width);
				logo = new Logo();
				trace("2 " + stage.height + "  " +stage.width);
				logo.x = stage.stageWidth/2;//300;
				trace("3 " + stage.height + "  " +stage.width);
				logo.y = stage.stageHeight/2;//100;
				trace("4 " + stage.height + "  " +stage.width);
				addChild(logo);
				// may be the stage is not initiated then adding a child startes the stage
				
				trace("5 " + stage.height + "  " +stage.width);
				startButton = new StartButton();
				trace("6 " + stage.height + "  " +stage.width);
				startButton.x = stage.stageWidth/2;//300;
				trace("7 " + stage.height + "  " +stage.width);
				startButton.y = 300;
				trace("8 " + stage.height + "  " +stage.width);
				addChild(startButton);
				trace("9 " + stage.height + "  " +stage.width);
				[code]

You get this output 
(*NOTE, this is dependent on the size of your symbols and their xy origins)

1 0 0
2 0 0
3 0 0
4 0 0
5 209 322.95
6 209 322.95
7 209 322.95
8 209 322.95
9 209 322.95[/code]

As you can see the stage.width and stage.height is at zero until we add something to its visual tree.
After the addChild(logo); you see that the stage.Width and stage.height change to reflect that there is now an item in the stage's visual tree. Something specific about my test file is that my logo is engulfs the startbutton so there is no resizing of the stage after it is added.

So what you need and I put in the above code is the use of stage.stageWidth and stage.stageHeigth rather then stage.width and stage.height.

One represents the visible stage area and one is the stuff in the stages visual tree. This is explained better in the link above.


Hope that helps, Cheers.









View Postleafdj, on 29 January 2012 - 07:01 PM, said:

Hey everyone, so I'm making my first game for the Global Game Jam this year, and it's been going pretty smoothly, just until I put in a title screen just now.
The method looks like this:
var startButton:StartButton;
		var logo:Title;
		var restart:RestartButton;

public function EightyEightJazz() {	
			
			logo = new Title();
			logo.x = stage.width/2;
			logo.y = stage.height/5;
			addChild(logo);
			
			trace(stage.height + "  " +stage.width);
			startButton = new StartButton();
			startButton.x = stage.width/2;
			startButton.y = 300;
			addChild(startButton);
			
			startButton.addEventListener(MouseEvent.CLICK, function (e:MouseEvent){
										 jazz();
										 });
				
			}


And all it's doing is putting everything on the screen, and waiting for the user to use the start button before it calls the main game method.

For some reason though, my stage dimensions are the dimensions of my Title symbol. The window stays the same size, but if I trace(stage.width + " " + stage.height), or try to use the width and height to set the positions of my objects, then it gets all wonky.

The strangest thing though, is that as soon as it gets into the jazz() method, the stage gets fixed again.

This is the first flash thing I've ever done, so I've only been able to try a few things, but setting focus to the stage didn't help, and I can't seem to figure out why it's doing this.

Any help would be fantastic, and thanks for taking the time to read this.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1