Be a Supporter!

object added to stage, can't see it

  • 346 Views
  • 14 Replies
New Topic Respond to this Topic
w7y7a7t7t
w7y7a7t7t
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 02
Programmer
object added to stage, can't see it Jan. 20th, 2013 @ 09:38 PM Reply

My problem is that I've added an object to the stage(myBalloon), and i can't see it! i've changed it's x,y so that it's in the middle(ish) of the stage, then i moved it to the very front, and i've used a trace to make sure it's there. and even though it says it is, it isn't! this is driving me insane. I'm really hoping someone here can help me...

here's my Game class (and yes, i've made sure that the objects i add after the balloon aren't on top of it)

package 
{

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;


	public class Game extends MovieClip
	{

		public static var count:int;
		private var fruitLimit:int = 0;
		private var treeLimit:int = 0;

		private var hoverUp:Boolean = false;
		public static var hoverTime:int = 0;

		private var kRight:Boolean;
		private var kLeft:Boolean;
		private var kUp:Boolean;
		private var kDown:Boolean;

		public static var myBalloon:Balloon = new Balloon  ;

		public function Game()
		{
			stage.addChild(myBalloon);

			if (myBalloon.stage)
			{
				trace("on stage");
			}
			else
			{
				trace(" not on stage");
			}
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
			stage.addEventListener(KeyboardEvent.KEY_UP,keyup);
			addEventListener(Event.ENTER_FRAME,Loop);
		}
		private function Loop(e:Event):void
		{
			count++;
			time.text = "score: " + count;


			if ((count >= fruitLimit))
			{
				var myFruit:Fruit = new Fruit  ;
				stage.addChild(myFruit);
				myFruit.x = Math.random() * 400;
				if ((fruitLimit <= 1800))
				{
					fruitLimit +=  120;
				}
				else
				{
					fruitLimit = 1800;
				}
			}
			if ((count >= treeLimit))
			{
				var myTree:Trees = new Trees  ;
				stage.addChild(myTree);
				var i:int;
				i = Math.random() * 2;
				if ((i > 1))
				{
					myTree.x = 400;
				}
				else
				{
					myTree.x = 0;
				}
				if ((treeLimit < 600))
				{
					treeLimit +=  120;
				}
				else if ((treeLimit >= 600))
				{
					treeLimit = 600;
				}
			}



			if (kUp)
			{
				myBalloon.y -=  3;
			}
			if (kDown)
			{
				myBalloon.y +=  3;
			}
			if (kLeft)
			{
				myBalloon.x -=  3;
			}
			if (kRight)
			{
				myBalloon.x +=  3;
			}
			if (myBalloon.x < 0)
			{
				myBalloon.x = 0;
			}
			if (myBalloon.x > 400)
			{
				myBalloon.x = 400;
			}



			hoverTime++;
			if ((hoverTime >= 0))
			{
				if ((hoverTime >= 72))
				{
					hoverTime = 0;
					if (hoverUp)
					{

						hoverUp = false;
					}
					else
					{

						hoverUp = true;
					}
				}
				if (hoverUp)
				{
					myBalloon.y++;

				}
				else
				{
					myBalloon.y--;

				}
			}
		}




		private function keydown(k:KeyboardEvent)
		{
			if (k.keyCode == 37)
			{
				kLeft = true;
			}
			if (k.keyCode == 39)
			{
				kRight = true;
			}
			if (k.keyCode == 38)
			{
				kUp = true;
			}
			if (k.keyCode == 40)
			{
				kDown = true;
			}
		}
		private function keyup(k:KeyboardEvent)
		{
			if (k.keyCode == 37)
			{
				kLeft = false;
			}
			if (k.keyCode == 39)
			{
				kRight = false;
			}
			if (k.keyCode == 38)
			{
				kUp = false;
			}
			if (k.keyCode == 40)
			{
				kDown = false;
			}
		}
	}

}

Here's the Balloon class:

package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	
	public class Balloon extends MovieClip {
		
		
		public function Balloon() {
			addEventListener(Event.ENTER_FRAME, balloonLoop);
			this.y = 550;
			this.x = 200;
		}
		private function balloonLoop(e:Event):void{
			this.parent.setChildIndex(this, this.parent.numChildren-1);
		}
	}
	
}

If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to object added to stage, can't see it Jan. 20th, 2013 @ 10:08 PM Reply

unit test the ever-living hell out of it. Start with something obvious that you know will work, like addChilding a simple sprite to the class, then work your way up from there. That's how you'll find the break.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
w7y7a7t7t
w7y7a7t7t
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 02
Programmer
Response to object added to stage, can't see it Jan. 20th, 2013 @ 10:24 PM Reply

At 1/20/13 10:08 PM, egg82 wrote: unit test the ever-living hell out of it. Start with something obvious that you know will work, like addChilding a simple sprite to the class, then work your way up from there. That's how you'll find the break.

*sigh*...I guess that'll be the only way. I was hoping there was some dumb problem i over looked and someone would just be like, "Oh you just forgot *insert thing i forgot here*". Oh well, at least this is a tiny project. thanks for the reply


If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to object added to stage, can't see it Jan. 21st, 2013 @ 01:53 AM Reply

It looks all good from here, but here's a few tips.

"public static var"
Never, never use static.

if (kLeft)
{
	myBalloon.x -=  3;
}
if (kRight)
{
	myBalloon.x +=  3;
}

Why not:
if (kLeft) myBalloon.x -= 3;
if (kRight) myBalloon.x += 3;

if (hoverUp)
{
	hoverUp = false;
}
else
{
	hoverUp = true;
}

To flip a boolean use
hoverUp = !hoverUp

"private function keydown(k:KeyboardEvent)"
Typecast your function, you should actually receive a warning for not doing this, a possible reason for not doing this is using the animation IDE Flash CS, if so, don't do this, use a coding IDE, I suggest FlashDevelop but other do exist such as FlashBuilder.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to object added to stage, can't see it Jan. 21st, 2013 @ 01:55 AM Reply

Oh and one more thing, never write logic in the constructor, if an error occurs in it then it cannot be debugged.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to object added to stage, can't see it Jan. 21st, 2013 @ 11:03 AM Reply

At 1/21/13 01:55 AM, MintPaw wrote: Oh and one more thing, never write logic in the constructor, if an error occurs in it then it cannot be debugged.

FD will set you up with a default Main with the lines

if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);

in the constructor.

just change it to

addEventListener(Event.ADDED_TO_STAGE, init);

which will prevent you from writing code coming from the constructor, since events break the flow of code.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to object added to stage, can't see it Jan. 21st, 2013 @ 03:02 PM Reply

At 1/21/13 01:53 AM, MintPaw wrote: It looks all good from here, but here's a few tips.

"public static var"
Never, never use static.

Bit of a bold statement.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to object added to stage, can't see it Jan. 21st, 2013 @ 07:25 PM Reply

At 1/21/13 03:02 PM, Sam wrote:
At 1/21/13 01:53 AM, MintPaw wrote: It looks all good from here, but here's a few tips.

"public static var"
Never, never use static.
Bit of a bold statement.

Never!
Well, I mean maybe static helper methods like Math.abs() and junk, but never integrate it into your game as a value the game relies on or anything.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to object added to stage, can't see it Jan. 21st, 2013 @ 07:41 PM Reply

At 1/21/13 07:25 PM, MintPaw wrote:
At 1/21/13 03:02 PM, Sam wrote:
At 1/21/13 01:53 AM, MintPaw wrote: It looks all good from here, but here's a few tips.

"public static var"
Never, never use static.
Bit of a bold statement.
Never!
Well, I mean maybe static helper methods like Math.abs() and junk, but never integrate it into your game as a value the game relies on or anything.

It's often misused but I wouldn't say "never". It's a powerful feature in a language, but the keyword seems to get tossed around for the wrong reasons.

X-Tender
X-Tender
  • Member since: Aug. 28, 2005
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to object added to stage, can't see it Jan. 22nd, 2013 @ 03:58 AM Reply

I try to answer to your question, did you've setted the Balloon Class as a BaseClass or just as a Class in Flash for your Balloon Graphic? In the Case you're using you have to place it in the "Class" field Otherwise you only create an Instance of The Clas without an assigned Graphic/Movieclip


I code Flash Games! Check out the Stealth Pervert and VOID

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to object added to stage, can't see it Jan. 22nd, 2013 @ 06:06 AM Reply

At 1/21/13 01:53 AM, MintPaw wrote: "public static var"
Never, never use static.

Never say never.
Static variables are good for storing colour codes like this:

public class Color {
    static public const RED:int         = 0xFFFF0000;
    static public const GREEN:int    = 0xFF00FF00;
    static public const BLUE:int       = 0xFF0000FF;
    static public const YELLOW:int = 0xFFFFFF00;
    static public const PURPLE:int  = 0xFFFF00FF;
    static public const CYAN:int       = 0xFF00FFFF;
}

And anything else that you'll only ever have one of (such as the speed of light in a vacuum, should your game/app require it).

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to object added to stage, can't see it Jan. 22nd, 2013 @ 11:15 AM Reply

At 1/22/13 06:06 AM, Diki wrote:
At 1/21/13 01:53 AM, MintPaw wrote: "public static var"
Never, never use static.
Never say never.
Static variables are good for storing colour codes like this:

public class Color {
static public const RED:int = 0xFFFF0000;
static public const GREEN:int = 0xFF00FF00;
static public const BLUE:int = 0xFF0000FF;
static public const YELLOW:int = 0xFFFFFF00;
static public const PURPLE:int = 0xFFFF00FF;
static public const CYAN:int = 0xFF00FFFF;
}

And anything else that you'll only ever have one of (such as the speed of light in a vacuum, should your game/app require it).

Static constants for the height, width, half height and half width are also really useful. Saves the time it takes to look up stage.stageWidth, and the calculation to get half of that.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to object added to stage, can't see it Jan. 22nd, 2013 @ 12:00 PM Reply

At 1/22/13 11:15 AM, Sam wrote: Static constants for the height, width, half height and half width are also really useful. Saves the time it takes to look up stage.stageWidth, and the calculation to get half of that.

i'm much less comfortable with that. The stage's size can change, you know.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to object added to stage, can't see it Jan. 22nd, 2013 @ 12:14 PM Reply

At 1/22/13 12:00 PM, egg82 wrote:
At 1/22/13 11:15 AM, Sam wrote: Static constants for the height, width, half height and half width are also really useful. Saves the time it takes to look up stage.stageWidth, and the calculation to get half of that.
i'm much less comfortable with that. The stage's size can change, you know.

In fullscreen scenarios and with certain scale modes set, yes. But in any application that's built the constants you may use change depending on what is and isn't going to happen. I'm not always going to need constants for colours. It just so happens that the applications I build rarely have need for fullscreen, and the constants lend themselves really well.

Anything for a bit more performance, eh?

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to object added to stage, can't see it Jan. 22nd, 2013 @ 12:38 PM Reply

At 1/22/13 12:14 PM, Sam wrote: In fullscreen scenarios and with certain scale modes set, yes. But in any application that's built the constants you may use change depending on what is and isn't going to happen. I'm not always going to need constants for colours. It just so happens that the applications I build rarely have need for fullscreen, and the constants lend themselves really well.

Anything for a bit more performance, eh?

if they open your swf in a flash player exe, they can resize the window. If it's AIR, it's automatically put in an exe. I like to account for as many variables as I can.

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

:3


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature