Skip to main content
January 29, 2016
Answered

Best Practices for something like this

  • January 29, 2016
  • 1 reply
  • 341 views

Working on a project, after learning about "Garbage Collecting" and a couple other things from this forum (Thank you all) I was just wondering if someone could look over this bit of code and tell me if Im using best practices or if it is just sloppy as hell and im going about it all wrong.

ALSO, I understand to remove eventListeners and children added to the stage dynamically. But do you need to remove things such as Tweens, and Timers..

If I instantiate something using identifer = new something ();  Do I always need to remove this no matter what it is to keep everything clean?

package {

  import flash.display.MovieClip;

  import flash.utils.Timer;

  import flash.events.*;

  import fl.transitions.Tween;

  import fl.transitions.easing.Elastic;

  import fl.transitions.easing.Back;

  import fl.transitions.TweenEvent;

  public class ShooterProtoMAIN extends MovieClip {

  //MAIN MENU ASSETS

  public var MainMenu:mcMainMenu;

  public var MenuCredits:mcCredits;

  public var StartGameBttn:bttnStartGame;

  public var MainMenuTitle:mcTitle;

  public var MainMenuTitleTween:Tween;

  public var MenuCreditsTween:Tween;

  public var StartGameBttnTween:Tween;

  //PLAYER ASSETS

  public var Player:mcPlayer;

  public var PlayerReticle:mcReticle;

  public var ReadyCheck:mcReadyCheck;

  public var PlayerBase:mcBase;

  public var PreRoundTimer:Timer;

  public var PlayerControlEnabled:Boolean = false;

  public const PlayerMoveSpeed:Number = 3;

  public function ShooterProtoMAIN () {

  // constructor code

  }

  //MAIN MENU BEFORE GAME BEGINS

  private function MainMenuScreen () {

  //BUILD OUR MENU SCREEN

  MainMenu = new mcMainMenu ();

  stage.addChild (MainMenu);

  MainMenu.x = stage.stageWidth / 2;

  MainMenu.y = stage.stageHeight / 2;

  MainMenuTitle = new mcTitle ();

  stage.addChild (MainMenuTitle);

  MainMenuTitle.x = stage.stageWidth / 2;

  MainMenuTitleTween = new Tween(MainMenuTitle,"y",Elastic.easeOut,-50,100,1,true);

  MenuCredits = new mcCredits ();

  stage.addChild (MenuCredits);

  MenuCredits.x = stage.stageWidth / 2;

  MenuCreditsTween = new Tween(MenuCredits,"y",Elastic.easeOut,850,750,1,true);

  StartGameBttn = new bttnStartGame();

  stage.addChild (StartGameBttn);

  StartGameBttn.y = stage.stageHeight / 2;

  StartGameBttnTween = new Tween(StartGameBttn,"x",Elastic.easeOut,-50,stage.stageWidth / 2,1,true);

  StartGameBttn.addEventListener (MouseEvent.CLICK, StartTheShow);

  }

  private function StartTheShow (event:MouseEvent):void {

  MainMenuTitleTween = new Tween(MainMenuTitle,"y",Back.easeIn,100,-50,1,true);

  MenuCreditsTween = new Tween(MenuCredits,"y",Back.easeIn,750,850,1,true);

  StartGameBttnTween = new Tween(StartGameBttn,"x",Back.easeIn,stage.stageWidth / 2,-150,1,true);

  StartGameBttnTween.addEventListener (TweenEvent.MOTION_FINISH, MainMenuTweenFinished);

  }

  private function MainMenuTweenFinished (event:TweenEvent):void {

  MainMenuRinse ();

  BeginGame ();

  }

  //CLEANER FUNCTION FOR MAIN MENU;

  private function MainMenuRinse () {

  stage.removeChild (MainMenu);

  MainMenu = null;

  stage.removeChild (MenuCredits);

  MenuCredits = null;

  StartGameBttn.removeEventListener (MouseEvent.CLICK, StartTheShow);

  StartGameBttnTween.removeEventListener (TweenEvent.MOTION_FINISH, MainMenuTweenFinished);

  stage.removeChild (StartGameBttn);

  StartGameBttn = null;

  }

This topic has been closed for replies.
Correct answer kglad

most likely that will have no problems, but it's not (imo) the best way to setup your project if that's your document class.

from the coding that looks like it should be a MainMenuScreen class that handles the display and removal of the main menu display.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 29, 2016

most likely that will have no problems, but it's not (imo) the best way to setup your project if that's your document class.

from the coding that looks like it should be a MainMenuScreen class that handles the display and removal of the main menu display.

January 29, 2016

You are exactly correct, I have a pre loader on frame 1, frame 2 - 59 is a intro movie, and on frame 60 I have actions on the frame:

stop();

MainMenuScreen();

and that runs the main menu screen. I believe im supposed to put as much as possible into their respective class.as but for now Its easier for me to get things accomplished writing everything in the main document class