[Solved, Thanks Aaants] WG Game(parent).ServiceExitToMain Menu(); [AS3]

Subscribe to [Solved, Thanks Aaants] WG Game(parent).ServiceExitToMain
Menu(); [AS3] 11 posts

avatar for Mond Mond 749 posts
Flag Post

Well…this is embarrassing, but….what does this code do? Been working on this game for three years and can’t remember why I wrote this code…

WG_Game(parent).ServiceExitToMainMenu();

WG_Game is my game Class and ServiceExitToMainMenu is a non-static public function within WG_Game. This line of code is in my WG_Pause Class and is executed when the player leaves the game during a pause.

private function MaskFadeIn(e:Event):void
{
	if (fadeMask.alpha > 0.5) fadeMask.alpha += 0.1; else fadeMask.alpha += 0.15;
	if (fadeMask.alpha > 1.0)
	{
		fadeMask.alpha = 1.0;
		fadeMask.removeEventListener(Event.ENTER_FRAME, MaskFadeIn);
		exitToMainMenu = true;
		if (destFrame == "ReStart") replay = true;
		WG_Game(parent).ServiceExitToMainMenu();
	}
}

More specifically…what does the (parent) do that enables me to call a function in the WG_Game Class from the WG_Pause Class? and the function being called is not static.

 
avatar for Aaants Aaants 158 posts
Flag Post

You’re accessing the parent property of your pause class and casting it to a WG_Game object, granting you access to that public function.

Class(object) attempts to treat the object as that type of class.

 
avatar for NineFiveThree NineFiveThree 1370 posts
Flag Post

Just use a Tween engine if you want to do fading.

For that parent part, you do know Events?

 
avatar for Mond Mond 749 posts
Flag Post

Thanks Aaants. The parent property of my WG_Pause Class instance…casting it to WG_Game…and calling the function.

I changed it to this.parent.ServiceExitToMainMenu(); and it wouldn’t compile…


C:\Program Files\FlashDevelop3.3.4\Work\*\src\WG_Pause.as(239): col: 17 Error: Call to a possibly undefined method ServiceExitToMainMenu through a reference with static type flash.display:DisplayObjectContainer.
this.parent.ServiceExitToMainMenu();
^
Build halted with errors (fcsh).
(fcsh)
Done(1)

So parent must be of type DisplayObjectContainer rather than WG_Game. Now it makes sense. Thanks again. I must need a break…

 
avatar for Mond Mond 749 posts
Flag Post

Three years ago I didn’t know a ENTER_FRAME from a MouseEvent. This is an unusual exit from the game so I expect some Class cross-contamination…especially since I didn’t use Custom Events in this game.

 
avatar for Draco18s Draco18s 6860 posts
Flag Post

So parent must be of type DisplayObjectContainer

RTFM

 
avatar for Mond Mond 749 posts
Flag Post

But a trace(parent); results in [object WG_Game] not [object DisplayObjectContainer] so it is a wee bit confusing since we can’t all have the manual memorized.

 
avatar for NineFiveThree NineFiveThree 1370 posts
Flag Post

As3 is strong typed

parent being typed to DisplayObject container is the only thing that makes sense, because being a container grants the ability to be a parent of something.

 
avatar for Mond Mond 749 posts
Flag Post

A lot of developers love Tween Engines (reference 953’s comment above). But the only thing they learn is the Tween Engine interface. It will never teach them how to use AS3 to accomplish a goal. It is analogous to using Stencyl rather than writing your own code.

But, since I have been coding a while, I can see the advantage of coding your own framework or using a Tween Engine. Especially if you are working to a customer’s deadline.

 
avatar for NineFiveThree NineFiveThree 1370 posts
Flag Post

The point of a library is not to learn the implementation behind it.

 
avatar for Draco18s Draco18s 6860 posts
Flag Post
Originally posted by NineFiveThree:

The point of a library is not to learn the implementation behind it.

Don’t reinvent the wheel unless your goal is to build a better wheel.