Flash AS3 - using 'gotoAndPlay' with stage vars inside MC

A place for general discussions about anything and everything.

Flash AS3 - using 'gotoAndPlay' with stage vars inside MC

Postby Ivan-Aedler » Thu Jan 17, 2013 5:15 am

Hello all,
I think people already know me as the developer of Mario is Missing - Peach Untold Tale. I've been programming in AS2.
But I'm in the process of learning AS3 to code two of three games here on LoK. Also because I've hit the AS2 limits.

The problem is that I need to have special movie clips with 'gotoAndPlay' in certain frames according to stage (or root) variables.

I know that AS3 teaches you to code in a main scene1 frame, or as a best pratice, in a separate AS file. The problem is that I need to put a given MC, say, character, in the stage, using the IDE (direct placement with an instance name). Once there, this character has 50 poses. Each pose has 10 clothing options. So, the arm can have one of 10 glove types, the body can have one of 10 t-shirts, and so on.

It will be a hell to just name everything (put instances) then acess them from the AS file or Scene1 frame. Also because the character is loading/unloading movieclips according to their state, because every pose is a new MC. So, when walking, the correct outfit need to be there.. When jumping, I need to use another mcs that's instantiated automatically by FLASH because the main character just plans another frame for that. And so on.

So, to tell every sub clip to search for the current oufit, I code like that in AS2 for every body part that will be used in all 50 poses (each one uses up to 10 body parts each):

every code frame looping on itself.jpg
every code frame looping on itself.jpg (42.85 KiB) Viewed 1885 times

How to make that in AS3?

Then I decided to simpify this code and create a Game class (same name as the project), and I've put:

Code: Select All Code
package
  {
   import flash.display.*;
   import flash.text.*;

  public class Game extends MovieClip
   {
    public function Game ()   //contructor
   {
     var DressSel:String="Outfit1";
  }
 }
}


in SCENE1 frame (first main frame), I've put:

Code: Select All Code
var game:Game=new Game();
stage.addChild(Game);


Finally, in each part of the clothe frames (image above), I've put instead:
Code: Select All Code
gotoAndPlay(stage.game.DressSel);


But I'm not able to play those inner frames. They dont find 'stage.game.DressSel'. I also tried putting 'root.game.DressSel'.
The error: Scene 1, Layer 'LEVEL', Frame 1, Line 5 1119: Access of possibly undefined property game through a reference with static type flash.display:Stage.

Am I doing something wrong?
I really need to use that logic ('alive' frames that go to other frames according to a global variable).

I noticed people use to ADDED_TO_STAGE event, but I dont know where to go.
Oh about the book, I've bought "ActionScript 3 Game Programming University, by Gary Rosenzweig, but the book rather uses an external AS-only code, with minimal to no code in any timeline.

Thank you really.
Last edited by Ivan-Aedler on Thu Jan 17, 2013 5:44 am, edited 4 times in total.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 5:23 am

i think i figured out one problem looking at your code.
Code: Select All Code
package
      {
       import flash.display.*;
       import flash.text.*;

      public class Game extends MovieClip
       {
        public var DressSel:String; // <----------Variable is now available outside the constructor
        public function Game () //constructor
       {
              DressSel="Outfit1"; //<--------------- now set variable.
       }
     }
    }


Try this first.
Last edited by AnotherArrow on Thu Jan 17, 2013 5:29 am, edited 1 time in total.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 5:29 am

AnotherArrow Wrote:
Code: Select All Code
package
      {
       import flash.display.*;
       import flash.text.*;

      public class Game extends MovieClip
       {
        public var DressSel:String; // <----------Variable is now available outside the constructor
        public function Game () //constructor
       {
              DressSel="Outfit1"; //<--------------- now set variable.
       }
     }
    }

Try this first.

Thanks for the support!
Tried here, same problem.

Package
Code: Select All Code
package
  {
   import flash.display.*;
   import flash.text.*;

   public class Game extends MovieClip
   {
     var blushType:String="fade_in";
     var blushAux:String="fade_in";
     var DressSel:String="Outfit1";

    public function Game()   //contructor
   {
     blushType="fade_in";
     blushAux="fade_in";
     DressSel"Outfit1";
   }
 }
}

Scene1 frame:
Code: Select All Code
stop();
//main code
var game:Game=new Game();
stage.addChild(game);


Subclip code (lonely code in every odd frame) of each body part:
Code: Select All Code
gotoAndPlay(stage.game.DressSel);


Symbol 'Top', Layer 'Layer 2', Frame 1, Line 1 1119: Access of possibly undefined property game through a reference with static type flash.display:Stage.
Symbol 'Pants', Layer 'Layer 2', Frame 1, Line 1 1119: Access of possibly undefined property game through a reference with static type flash.display:Stage.
Symbol 'Gloves', Layer 'Layer 2', Frame 1, Line 1 1119: Access of possibly undefined property game through a reference with static type flash.display:Stage.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 5:43 am

if you take out the "gotoAndPlay(stage.game.DressSel);" in all frames.... do/will you still get the error?
If so, then its not that code...
If not, then.... well.. i won't say just yet.

its hard to figure out what exactly is causing that error (flashes debugger sucks ass).

but...

judging by the wording of the error... and the given code..hmm..
oh!
i could be wrong... but make sure
Code: Select All Code
var DressSel:String="Outfit1"
; is set to
Code: Select All Code
 public var DressSel:String="Outfit1";
I'm not 100% sure, but AS3 has been fussy with me when I don't define one's security (AS3 may be setting it to private, in which case, nothing but Game() can assess it).
And just for extra measure, take out the default assignments.
Code: Select All Code
var blushType:String="fade_in";
     var blushAux:String="fade_in";
     var DressSel:String="Outfit1";

to
Code: Select All Code
     public var blushType:String;
     public var blushAux:String;
     public var DressSel:String;
when Game() is constructed... these values will be set in there.


hmm...this is AS3 right?.. i don't code onto mc anymore...hmm.
I'm starting to think the Compile errors are strictly from calling Stage through the mc.... give me a few, I need to think a bit.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 5:50 am

AnotherArrow Wrote:if you take out the "gotoAndPlay(stage.game.DressSel);" in all frames.... do/will you still get the error?

Removing that line solves the problem. But I lose the advantage of using this approach to control many movie clips' frames at once :/

AnotherArrow Wrote:its hard to figure out what exactly is causing that error (flashes debugger sucks ass). but... judging by the wording of the error... and the given code..hmm..
oh! i could be wrong... but make sure
Code: Select All Code
var DressSel:String="Outfit1"
; is set to
Code: Select All Code
 public var DressSel:String="Outfit1";

Ok!

Code: Select All Code
package
  {
   import flash.display.*;
   import flash.text.*;

   public class Game extends MovieClip
   {
     public var blushType:String;
     public var blushAux:String;
     public var DressSel:String; //Outfit1,2,3,4,....10

    public function Game()   //contructor
   {
     blushType="fade_in";
     blushAux="fade_in";
     DressSel="outfit1";
   }
 }
}

Same error :/

AnotherArrow Wrote:hmm...this is AS3 right?.. i don't code onto mc anymore...hmm. I'm starting to think the Compile errors are strictly from calling Stage through the mc.... give me a few, I need to think a bit.

I only code in the timeline to gain those freedom.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 6:05 am

...backup everything.... and try this...

create an external flash file called Dress.as
enter the following code:
Code: Select All Code
package  {
   import flash.display.MovieClip;
   import flash.events.Event;
   
   public class Dress extends MovieClip{

      public function Dress() {
         this.addEventListener(Event.ENTER_FRAME, DressOnEnterFrame)
      }
      private function DressOnEnterFrame(e:Event):void{
         //gotoAndPlay((root as Game).DressSel);
         gotoAndStop( (root as Game).DressSel ); // Recommend over Play...
      }
   }
}


That code should work... its what I use in my game.

Click on the properties of each symbol that you are modding (from the errors, looks like "Top", "Pants", etc)... anyway
Enter "Dress" for the Base class let it auto assign the default class.
Hell, if you have a lot of symbols to assign this class to, select them all in the library, select properties, make sure export for actionscript is yes and base class is "Dress";

next... delete the actionscript layer on each symbol.... (thats why i told you to backup everything).

lastly....
i've never used code like:
Code: Select All Code
var game:Game=new Game();
stage.addChild(Game);

if its working... fine...
but for the sake of experimenting...
delete it and on the main stage... with nothing selected...
on the Properties Tab, where is says Class (or Document Class)... make it "Game"... if it isn't already.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 6:27 am

AnotherArrow Wrote:i've never used code like:
Code: Select All Code
var game:Game=new Game();
stage.addChild(Game);

Well, I've just thought you needed to add every class/code to the stage before using them and mostly, making the class call the stage variables.
The Class of the project is already set as 'Game'.
However, yes, I can just put a bunch of variables/arrays/objects I want to be 'global' there and reference them as stage.variable. This is good for those universal code that dont need classes.

AnotherArrow Wrote:...backup everything.... and try this...

Worked! Thanks! But! I only want to understand:

So this class is now alive (onenterframe).

But do I need to create (as a best pratice) a different class for every item? Like Dresses and Characters?
Isnt a way to create just one class for both? Also, those (root as Game) DressSel...so I can create this DressSel right into frame 1 of Scene1?
Then I can organize them as 'objects' at least, to make them like globals as below?

Scene1:
var stage.DressSel:String="outfit1";
var stage.LightsOn:Boolean=false;
///etc

And how about those body parts that use different checking for each frame?

e.g frame1 (with onEnterFrame to control those gotoAndStops):
Code: Select All Code
gotoAndStop(stage.DressSel[stage.DressNum]);


frame2:
Code: Select All Code
if (stage.PLAYER.clothes_vest_remove && stage.DressNum!=3)
   gotoAndStop("naked");
else
   gotoAndStop(stage.DressSel[stage.DressNum]);


Thats because, as the game progressed, I was on a need to check if a given clothing part was removed (e.g. she removed the shirt).
If so, even using a given outfit, only that part (torso) will be naked. Using only the default 'DressSelect[DressNumber]' will make her always clothed.
(btw, yes, I can use only one frame with gotoAndStop (instead of a empty frame and a code frame with gotoAndPlay))

So do you think its better to name an instance for all body parts in all frames/tweens, so I can control them in the inverse way (from the scene1)?
But I feel lost from now on. thats because i only want a way to deal with custom code in those frames. Every frame can have a different code :/ Can I check their labels using currentLabel and implement that logic inside the class?

Thanks really for that.
Last edited by Ivan-Aedler on Thu Jan 17, 2013 8:34 am, edited 1 time in total.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 7:14 am

Going to bed now, but thought i'd take moment to answer a few questions... i'll go deeper into detail tomorrow.

Basically, by assigning each symbol to that class, every time that symbol appears on the stage, it will auto construct... no need to "add child" and such.

Because AS3 doesn't use OnEnterFrame like AS2 does... at least not in the traditional sense... I have to create my own OnEnterFrame (in this case DressOnEnterFrame) using EventListener.
It does basically the same thing... just coded differently.

But do I need to create (as a best pratice) a different class for every item? Like Dresses and Characters? Isnt a way to create just one class for both?
hmm.. it depends... I, myself, probably go overboard with creating too many classes, but I feel i have more control over an object. The question is, does the Moviceclip (or Object) require code and special treatment? If so, then most likely you'll need to make a class to automate much of its functions. As for creating one class for both... i guess u could, but by making separate classes, you'll have more flexibility with them... hard to explain, since i feel I'd have to go into a whole lecture about inheritance and polymorphism that i don't want to get into.

Also, if I'm understanding what you wrote after that, it sounds like the Game Class and its variable where typed onto the frame 1 of scene 1?... if so, you are treading in territory I've never tried. I've always had my code in external files. (You can simply copy said code into its own Game.as file just like the Dress Code);

So do you think its better to name an instance for all body parts in all frames/tweens, so I can control them in the inverse way (from the scene1)?
But I feel lost from now on. thats because i only want a way to deal with custom code in those frames. Every frame can have a different code :/ However I can check their labels using currentLabel and implement that logic inside the class.

If I read this write (i'm tired), you want to be able to turn off some parts of clothing... there is a way... I'll write it up tomorrow... requires a bit more code and setup then I have time. Check out my game in my sig and let me know if that's the effects you want, otherwise i think i've miss understood you on your goal.

-AA to bed
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 7:54 am

AnotherArrow Wrote:Going to bed now, but thought i'd take moment to answer a few questions... i'll go deeper into detail tomorrow.

No problem, thanks again! Well, I know Java. I've had lectures about polymorphism, inheritance, overload, aggregation, composite, and design patterns. But I just dont want all that complexity in a medium sized game. I'm making a 8 world game (like Mario classic), and I want to make another one (a parody) of Mario, but using a more isometric view, with 2d tricks. However, I'm so accustomed with 'live placement of objects in the stage' (you must agree it ease the task of aligning assets without having to create a separate level editor), that I also put code inside timelines. But, provided we can place objects in the stage, like a big 'ground' MC, a big 'background 'object, the character, the particles, and make all the code using external files, using tricks like currentlabel to every movie clip, thats fine and a very good approach (modularity!)

But, again, for those cases you have 50 poses (walking, jumping, fleeing, swimming, playing, sleeping...), each one with 10 or more body parts that gets instantiated at runtime, with no instance name (although I can enter in each pose and name each body part instance), in those cases you just prefer to control movieclips putting code inside its timeline, because they have a life on their own. They are the 'masters' of clothing, in the Dress case. However, provided I name every body part, them refer to them using onEnterFrame, events and currentLabel using only external AS files, yes its possible, and maybe faster in CPU resources.

As about in-line code (object code), I'm using that bad practice less and less in AS2. Thats because I use a lot of dialogue scripts. So I will end up creating a big IF or CASE to switch text and code depending on a given level (in AS3, stage.level), place (stage.currentPlace), Puzzle (stage.currentPuzzle) or the name of the character himself as 'if (stage[charName]=="Robert" then dialogScript1() )', in a given AS file , like a 'dialog meeting class'.

AnotherArrow Wrote:Basically, by assigning each symbol to that class, every time that symbol appears on the stage, it will auto construct... no need to "add child" and such.

You say, only this base class (which has the same name as the FLA). Because other classes, even being instantiated as 'extend MovieCip' will need add child. But as about auto construct, you say that, if I have a new moviecilp, say, level, inside Game, is level able to access game variables just because they auto construct? In this case, can we call 'broadcasting variables'?

AnotherArrow Wrote:Because AS3 doesn't use OnEnterFrame like AS2 does... at least not in the traditional sense... I have to create my own OnEnterFrame (in this case DressOnEnterFrame) using EventListener. It does basically the same thing... just coded differently.

But thats an interesting approach. Naming each body part's (instance name) required to have their dress changed accordingly (e.g. Dress class) will make the onEnterFrame act on all of them like magic, as this linking is like as adding an aggregation class, that is, a class that adds function to another one.

AnotherArrow Wrote:I, myself, probably go overboard with creating too many classes, but I feel i have more control over an object. The question is, does the Moviceclip (or Object) require code and special treatment? If so, then most likely you'll need to make a class to automate much of its functions.

I like some complexity, like several character types with several clothing options for each one. That exponential approach requires two kind of body MC's for every body part: the part itself (torso, leg, arm, forearm) and the clothes associated (torso_clothes, leg_clothes, arm_clothes). So I use the body part as the father of clother part.

[o][.][.][.] -> code (only one frame is needed - onenterframe). It uses stage.CharSelect, which is an array of character types.
[X][X][X][X] -> characters - Each frame is a new one.
[X..........] -> Clothes (one filled frame only). Inside it, there is the code I've shown in my first post (DressSel). So father and children are checking everytime for a given change.

I can even optimize this further, putting this below inside children enterframe (to change dress) and a slight different version for the body part itself (to change character), that is, charSel.
(Below, dress code change in AS2):

Code: Select All Code
if (!once)
{
once=true;
onEnterFrame=function()
 {
    if (currentlabel!=_root.DressSel[_root.DressNum])
    {
      if (_root.clothes_vest_remove and _root.DressNum!=3)
         gotoAndStop("naked");
      else
         gotoAndStop(_root.DressSel[_root.DressNum]);
     currentlabel=_root.DressSel[_root.DressNum];
   }
 }
}


So it wont be 'going and stopping' at every frame, but it will be stopped at once until currentlabel changes. I believe this can be done in AS3. And, as I've said before, using currentLabel in AS3 is a really good checking code. It gives resilience to the code (flexibility), once you have all MCs instance names you need.

AnotherArrow Wrote:Also, if I'm understanding what you wrote after that, it sounds like the Game Class and its variable where typed onto the frame 1 of scene 1?... if so, you are treading in territory I've never tried. I've always had my code in external files. (You can simply copy said code into its own Game.as file just like the Dress Code);

Yes, you are using the clean approach. FLA file with only assets and no code. AS files for code. Thats really good for portability and organization. But you will need to know everything about any movie clip, and that can be a real pain in the case of several character poses and clothes.

AnotherArrow Wrote:Check out my game in my sig and let me know if that's the effects you want, otherwise i think i've miss understood you on your goal.

I will check tomorrow (whops today). thanks really!! Java (coffee this time) doesnt work anymore around here. :lol:
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby OwnerOfSuccuby » Thu Jan 17, 2013 2:17 pm

Try this:

gotoAndPlay((root as MovieClip).DressSel[(root as MovieClip).DressNum]);

AS 2 _root. in AS 3 is (root as MovieClip).

You can go not to root but on 1 level upper then the object you are in:
(parent as MovieClip).

By the way AS2 is not so bad as you say ;)

And is it really comfortable to use unport and package ? What it let you to do that you can not make with out it by the way ?
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 3:28 pm

Ok.
Just reread everything (still confused on some parts).
First, I need to know, as I was trying to decipher some code... in :
Code: Select All Code
gotoAndStop(stage.DressSel[stage.DressNum]);
... or even using OwnerOfSuccuby's version
Code: Select All Code
gotoAndPlay((root as MovieClip).DressSel[(root as MovieClip).DressNum]);
Where you are calling DressSel [ DressNum ]...
Isn't DressSel a String variable? Like we declared earlier? If so... what is DressNum? and how does this code work?
As I read this code, either DressSel is an Array and DressNum is the index in the array (which doesn't make sense because I though you defined it as a String)
or DressSel is a String and DressNum is the index in the string (which also doesn't make sense).

I like some complexity, like several character types with several clothing options for each one. That exponential approach requires two kind of body MC's for every body part: the part itself (torso, leg, arm, forearm) and the clothes associated (torso_clothes, leg_clothes, arm_clothes). So I use the body part as the father of clother part.

[o][.][.][.] -> code (only one frame is needed - onenterframe). It uses stage.CharSelect, which is an array of character types.
[X][X][X][X] -> characters - Each frame is a new one.
[X..........] -> Clothes (one filled frame only). Inside it, there is the code I've shown in my first post (DressSel). So father and children are checking everytime for a given change.


Ok, I think I'm doing the same thing in my game... need a few bits of information for confirmation or pic of the actual symbol setup to see... but...
1) I'm assuming this an example of a body part (i.e. the leg, arm, etc)
2) I'm assuming the Character Layer is the that contains that characters "naked" body part... or skin.
3) I'm assuming the Clothes Layer contains .... clothes...
If so, is the Characters Layer on top of the clothes layer, or the clothes on top of the character layer? Makes a difference...
But assuming the later... because we wear clothes on skin ( and not skin on clothes :))...

Try

BodyPart.as
Code: Select All Code
    package  {
       import flash.display.MovieClip;
       import flash.events.Event;
       
       public class BodyPart extends MovieClip{

          public function BodyPart() {
             this.addEventListener(Event.ENTER_FRAME, BodyPartOnEnterFrame)
          }
          private function BodyPartOnEnterFrame(e:Event):void{
             gotoAndStop( (root as Game).CharSelect);
          }
       }
    }
Assign this to all bodypart movieclips

Also update Dress Code
Dress.as
Code: Select All Code
package  {
   import flash.display.MovieClip;
   import flash.events.Event;
   
   public class Dress extends MovieClip{

      public function Dress() {
         this.addEventListener(Event.ENTER_FRAME, DressOnEnterFrame)
      }
      private function DressOnEnterFrame(e:Event):void{
          if( currentFrameLabel != cRoot().DressSel){
              if ( cRoot().clothes_vest_remove && cRoot().DressNum != 3)
                  gotoAndStop("naked");
              else
                  gotoAndStop(cRoot().DressSel);
           }
      }
      private function cRoot():Game{  // Convert root to game.
          return (root as Game);
      }
   }
}

Er... just realized what that Dress Code was for.... scratch that up.... go back to the original code.
(updating) brb....
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 3:55 pm

Ok, Try this:

Update
Dress.as
Code: Select All Code
package  {
   import flash.display.MovieClip;
   import flash.events.Event;
   
   public class Dress extends MovieClip{

      public function Dress() {
         this.addEventListener(Event.ENTER_FRAME, DressOnEnterFrame)
      }
      protected function DressOnEnterFrame(e:Event):void{
         gotoAndStop(cRoot().DressSel );
      }
      protected function cRoot():Game{  // Convert root to game.
          return (root as Game);
      }
   }
}


DressVest.as
Code: Select All Code
package  {
   import flash.display.MovieClip;
   import flash.events.Event;
   
   public class DressVest extends Dress{

      public function DressVest() {
   
      }
      override protected function DressOnEnterFrame(e:Event):void{
          if( currentFrameLabel != cRoot().DressSel){
              if ( cRoot().clothes_vest_remove && cRoot().DressNum != 3)
                  gotoAndStop("naked");
              else
                  gotoAndStop(cRoot().DressSel);
           }
      }
   }
}

Assign this code to the "Vest", "shirt", Torso... or whatever you called your symbol for the shirt.
Assign it by replacing the "base class" for that Movieclip from Dress to DressVest
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 4:05 pm

OwnerOfSuccuby Wrote:Try this:
gotoAndPlay((root as MovieClip).DressSel[(root as MovieClip).DressNum]);

Gotcha! You're the man! Thanks ;)
So is that called 'Calling MovieClip(root) from a dynamic MovieClip instance'?
Interesting that the default instance (the 'main' movieclip) needs to be reference as this.
Is 'root as MovieClip' the same as MovieClip(root)?
As I recall, Root as Movieclip is a way to make Root variables act as the top father of all instanciated movieclips.

OwnerOfSuccuby Wrote:AS 2 _root. in AS 3 is (root as MovieClip).

How about stage? Thats because I wanted to create variables there (to be global).

OwnerOfSuccuby Wrote:You can go not to root but on 1 level upper then the object you are in:(parent as MovieClip).

If I'm on level 2, can I use parent.parent as MovieClip? But is parent as Movieclip (in the case of root) the same as the stage?
Interesting enough, the book doesnt explain that :/

OwnerOfSuccuby Wrote:By the way AS2 is not so bad as you say ;)

But I'm not saying AS2 is bad, terrible and everybody should change.
I just want to upgrade the current coding skills necessary to make a bunch of interesting features with the speed and flexibility of AS3, like the use of currentLabel (not available in AS2), possibility of different FPS in every movielip, usage of MIDI music, possibility to change MP3 pitch, tempo and other functions as flanger/chorus, a better usage of levels and dephts (display lists), making me abandon the use of an special movieclip in AS2 to a given layer to force monter spawn in that layer (avoiding the 'monster in front of everything' problem). Finally, the LALEM bug (level after level mangling due to an unproper/buggy unload function in AS2 due to excess of laid movieclips in a given FLA, requiring me to create an empty frame before the level frame) is not present in AS3.

OwnerOfSuccuby Wrote:And is it really comfortable to use unport and package ? What it let you to do that you can not make with out it by the way ?

Yes I can use only base classes. But as I recall, its require for you to use a default package.


AnotherArrow Wrote:Ok. Just reread everything (still confused on some parts). First, I need to know, as I was trying to decipher some code... in :
Code: Select All Code
gotoAndStop(stage.DressSel[stage.DressNum]);
... or even using OwnerOfSuccuby's version
Code: Select All Code
gotoAndPlay((root as MovieClip).DressSel[(root as MovieClip).DressNum]);
Where you are calling DressSel [ DressNum ]...
Isn't DressSel a String variable? Like we declared earlier? If so... what is DressNum? and how does this code work?

I havend explained well, sorry. Here is the declarations:

(AS3)
var DresNum:int = 0; //default (naked)
var DressSel:Array = new Array("Naked", "Princess", "Fire Princess", "Raccoon", "Frog", "Sling Bikini", "Striker", "Daisy Striker", "Daisy Dress", "Rosa Dress", "Mario", "Hammer Bros", "X-naut", "Nurse", "Shadow Queen");

So, I use DressSel[DresNum] to select a given clothing.
I would like to use them as stage variables (global), so thats why I'm using them inside Scene1 frame. But If I understand you, putting that in the game class (default class), it will be also a stage variable there (or a root variable in regard to that FLA). I know, global <> root. And I know, in AS3 you dont have a 'global' but the stage itself.

AnotherArrow Wrote:Ok, I think I'm doing the same thing in my game... need a few bits of information for confirmation or pic of the actual symbol setup to see... but...
1) I'm assuming this an example of a body part (i.e. the leg, arm, etc)

Yes! So thats why some errors are being thrown in each body part. Body parts use CharSel[CharNum] and clothing parts of this body part use DressSel[DressNum].

AnotherArrow Wrote:2) I'm assuming the Character Layer is the that contains that characters "naked" body part... or skin.

Yes. Skin1, Skin2...

AnotherArrow Wrote:3) I'm assuming the Clothes Layer contains .... clothes...

Yes. Only one frame in all character frames. If I have 30 character types, I can use all the clothing avaiable on them. That is, the body_clothes can be used in all of them. Thats why this MC is a child of that body part.

AnotherArrow Wrote:If so, is the Characters Layer on top of the clothes layer, or the clothes on top of the character layer? Makes a difference...

The clothes layer is always on top, because the clothes are supposed to hide the naked skin. ;)

AnotherArrow Wrote:But assuming the later... because we wear clothes on skin ( and not skin on clothes :))...Try

Thanks, I'll analysing it now ^_^ So If I need to make a bit more complex code for different bodypart MCs (arm, leg, body) or clothing outfits MC (outfit1,2,3), I just need to update those classes, putting additional code according to currentlabel?

But wait! Now I have three classes? BodyPart, Dress and DressVest? And DressVest , as I've seen above, is just a variance of Dress code. Why not putting the IF/CASE code of DressVest inside the same Dress code? But I see the concept, you're overriding it (a better approach than adding complexity to a same class).
So if I have other different code in , say, foot clothes, I just need to override the same Dress class, putting a new code, call this class DressFeet, then change 'foot clothes MC's base class to DressFeet?
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 4:39 pm

Ok... things make sense now!

updating code:
Dress.as
Code: Select All Code
package  {
   import flash.display.MovieClip;
   import flash.events.Event;
   
   public class Dress extends MovieClip{

      public function Dress() {
         this.addEventListener(Event.ENTER_FRAME, DressOnEnterFrame)
      }
      protected function DressOnEnterFrame(e:Event):void{
         gotoAndStop(GetDress());
      }
      protected function cRoot():Game{  // Convert root to game.
          return (root as Game);
      }
      protected function GetDress():String{
           return cRoot().DressSel[ cRoot().DressNum ];
           // return (cRoot().DressSel[ cRoot().DressNum ] as String);  // May have to try this code if return throws an error
      }
   }
}


DressVest.as
Code: Select All Code
package  {
   import flash.display.MovieClip;
   import flash.events.Event;
   
   public class DressVest extends Dress{

      public function DressVest() {
   
      }
      override protected function DressOnEnterFrame(e:Event):void{
          if( currentFrameLabel != GetDress() ){
              if ( cRoot().clothes_vest_remove && cRoot().DressNum != 3)
                  gotoAndStop("naked");
              else
                  gotoAndStop(GetDress());
           }
      }
   }
}


But wait! Now I have three classes? BodyPart, Dress and DressVest? And DressVest , as I've seen above, is just a variance of Dress code. Why not putting the IF/CASE code of DressVest inside the same Dress code?

Ok, so Class body part will take care of controlling which frame that movie clip needs to be on.

I made two Dress classes because I don't know how your Dress symbols are setup.
I assumed each clothing part was setup the same (in other words, no dress movieclips have unique labels.)

I can't really use a if/case statement in such a way unless the parent tells me that THAT symbol(movieclip) is of a certain nature.
For example... all dress parts are assigned to "Dress"... but how does that symbol know its a "pant" or "arm" or "torso"... I simply assigned a class to it... that changes its frame based on values from the root class... even asking the root or parent "hey, what am I", more then likely, without proper coding, the symbol simply doesn't know what it is... other then its a dress.. or part of one.

However, by making a child class (god its been forever... whats the proper term? inherited class? derived class?...)... I can set this class to have all the functions of the original, but modify certain code to make it unique and react different. So now... all body parts use Dress... except the Vest now uses DressVest class.So if you ever ran a function in Bodypart that need to tell all of its children (in this case the I'm going to stop right there... i'm getting ahead of myself.

So if I have other different code in , say, foot clothes, I just need to override the same Dress class, putting a new code, call this class DressFeet, then change 'foot clothes MC's base class to DressFeet?

Yup... this is the simplest method I could come up with not know everything about your code.

If you wanted one class to cover all parts, you would need to setup your symbols slightly differently then what I believe you have...
Heres code of my BodyPart Class that I use for comparison of the complexity that I make my class undertake...
CHAR_BODYPART.as
Code: Select All Code
package  {
   
   import flash.display.MovieClip;
   
   public class CHAR_BODYPART extends AT_MOVIECLIP {
      public var currentOutfit:CHAR_OUTFIT;
      
      public function CHAR_BODYPART() {
         currentOutfit = new CHAR_OUTFIT;
         this.stop();
      }
      public function UpdateOutfit(outfitData:CHAR_OUTFIT):void{
         if(outfitData != null){
            ChangeOutfit(outfitData.outfitName);
            ChangeStates(outfitData);
         }
      }
      private function ChangeOutfit(newOutfit:String){
         if(this.gotoLabel(newOutfit)){
            currentOutfit.outfitName = newOutfit;
         }
      }
      private function ChangeStates(outfitData:CHAR_OUTFIT){
         for(var i:int = 0; i < this.numChildren; i++){
            if(this.getChildAt(i) != null){
               if(this.getChildAt(i) is CHAR_OUTFITPART){
                  (this.getChildAt(i) as CHAR_OUTFITPART).changeOutfitState(outfitData.outfitState);
                  currentOutfit.outfitState = outfitData.outfitState;
               }
               if(this.getChildAt(i) is CHAR_UNDERWEARPART){
                  (this.getChildAt(i) as CHAR_UNDERWEARPART).changeUnderwearState(outfitData.underwearState);
                  currentOutfit.underwearState = outfitData.underwearState;
               }
            }
         }
      }
   }
   
}
In my code... i tell my children (clothes) when to update... rather then have them update on every frame.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 5:11 pm

AnotherArrow Wrote:Ok... things make sense now! updating code:

Thanks, this works so far! I understand the logic!

AnotherArrow Wrote:I made two Dress classes because I don't know how your Dress symbols are setup. I assumed each clothing part was setup the same (in other words, no dress movieclips have unique labels.)

Yes, every clothing frame inside 'XXX Clothes' MC has the same labels. When I add a new clothing outfit, I just increase the frame by 1, putting a new layer name in everyone.

AnotherArrow Wrote:I can't really use a if/case statement in such a way unless the parent tells me that THAT symbol(movieclip) is of a certain nature. For example... all dress parts are assigned to "Dress"... but how does that symbol know its a "pant" or "arm" or "torso"... I simply assigned a class to it... that changes its frame based on values from the root class... even asking the root or parent "hey, what am I", more then likely, without proper coding, the symbol simply doesn't know what it is... other then its a dress.. or part of one.

If I understand, we're into the current case (the green 'extend' is only the way I understood, that is, we're 'adding code' (onenterframe) in those movieclips). Please click to enlarge.

model 1.jpg

So, using only one class (Dress) may be possible, if we know which MC is accessing it on execution time. But I dont have their instance names.

AnotherArrow Wrote:However, by making a child class (god its been forever... whats the proper term? inherited class? derived class?...)

Inherited class!

AnotherArrow Wrote:Yup... this is the simplest method I could come up with not know everything about your code.

Yes it makes sense ;)

AnotherArrow Wrote:If you wanted one class to cover all parts, you would need to setup your symbols slightly differently then what I believe you have...
Heres code of my BodyPart Class that I use for comparison of the complexity that I make my class undertake...
CHAR_BODYPART.as

Its complex, yes! Are you using those CHAR_OUTFIT etc as alises for your MC instance name?
But you are using a child function to check the opposite side (parent checking child) instead of my case (child checking parent).
This should work better for me, I think ;) Because I will only need a Body class, so in the picture above, the blue MC's of BODY PARTS (that arent shown) will use the red class BODY, which, in turn, will be extended to body_part, making body MC and clothes MC (which is a child MC of body) act accordingly.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 6:12 pm

Good to hear you're getting it.

Its complex, yes! Are you using those CHAR_OUTFIT etc as alises for your MC instance name?
But you are using a child function to check the opposite side (parent checking child) instead of my case (child checking parent).


CHAR_OUTFIT are data containers i use to pass a lot of variables. It holds the type of outfit, the state of that outfit, the type of underwear, and the underwear state.
Rather then passing 4 variables in a function like, i can pass just one
Code: Select All Code
function foo( value1 , value2 , value3 , value 4)

vs

function foo( dataContainer )
This way when a parent needs to update a child, I just pass all the data along in one variable.

I do the parent setting child method vs child asking parent because of the difference...
Child will always be asking what it needs every frame. Imagine (DADDY , DADDY , DADDY, DADDY,......) non stop. Effective in the short term, but not efficient. The child is eating up the parents time asking to be dressed the same way. ;)
Now, in my case, The parent will only set / modify the child if something changes...

Your pic of the classes gave me the idea of sharing my setup. Maybe it will give you some ideas on how to approach future classes in your game.
Classes.png
Classes.png (34.46 KiB) Viewed 1801 times


This should work better for me, I think ;) Because I will only need a Body class, so in the picture above, the blue MC's of BODY PARTS (that arent shown) will use the red class BODY, which, in turn, will be extended to body_part, making body MC and clothes MC (which is a child MC of body) act accordingly.


Yup!
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby OwnerOfSuccuby » Thu Jan 17, 2013 7:23 pm

Ivan-Aedler Wrote:
OwnerOfSuccuby Wrote:Try this:
gotoAndPlay((root as MovieClip).DressSel[(root as MovieClip).DressNum]);

Gotcha! You're the man! Thanks ;)
So is that called 'Calling MovieClip(root) from a dynamic MovieClip instance'?
Interesting that the default instance (the 'main' movieclip) needs to be reference as this.
Is 'root as MovieClip' the same as MovieClip(root)?
As I recall, Root as Movieclip is a way to make Root variables act as the top father of all instanciated movieclips.

OwnerOfSuccuby Wrote:AS 2 _root. in AS 3 is (root as MovieClip).

How about stage? Thats because I wanted to create variables there (to be global).

OwnerOfSuccuby Wrote:You can go not to root but on 1 level upper then the object you are in:(parent as MovieClip).

If I'm on level 2, can I use parent.parent as MovieClip? But is parent as Movieclip (in the case of root) the same as the stage?
Interesting enough, the book doesnt explain that :/

OwnerOfSuccuby Wrote:By the way AS2 is not so bad as you say ;)

But I'm not saying AS2 is bad, terrible and everybody should change.
I just want to upgrade the current coding skills necessary to make a bunch of interesting features with the speed and flexibility of AS3, like the use of currentLabel (not available in AS2), possibility of different FPS in every movielip, usage of MIDI music, possibility to change MP3 pitch, tempo and other functions as flanger/chorus, a better usage of levels and dephts (display lists), making me abandon the use of an special movieclip in AS2 to a given layer to force monter spawn in that layer (avoiding the 'monster in front of everything' problem). Finally, the LALEM bug (level after level mangling due to an unproper/buggy unload function in AS2 due to excess of laid movieclips in a given FLA, requiring me to create an empty frame before the level frame) is not present in AS3.

OwnerOfSuccuby Wrote:And is it really comfortable to use unport and package ? What it let you to do that you can not make with out it by the way ?

Yes I can use only base classes. But as I recall, its require for you to use a default package.



Oh my god =))) So much text :shock: :mrgreen: I hope i translate it right :mrgreen:

After you get to ret layer you can use all thats childs like:

(root as MovieClip).stage.
(root as MovieClip).mygirltratata.ass.pants and etc.
The same for variables / MovieObjects and etc. So from one object you can call any object if you know where it is.

I did not know that it was not current frame in AS2 sorry - i think only Frame Rate =( Do not use it for to long :roll: - but i really love AS2 :mrgreen:
But for example you always can make variable and send its value to root layer - that will let you know current frame :mrgreen: :roll:

(parent.parent as MoviClip) - works the same. But if write in in object that is in 10 objects it will not be root ;) it will be object before object here it is.

It is better to try - then read a book =) You can always make a little example and test it - i always do such way. Becouse some times there are very strange things in books that do not work in real life - and are a little strange :mrgreen: But some times there are some good ideas :mrgreen: :roll:

I do not know is stage parent to object thats parent is root - it can be in root - i think it is there but it can be like parallel object - so i think you will say root.stage - and it will not see it like parent.
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Thu Jan 17, 2013 10:11 pm

AnotherArrow Wrote:CHAR_OUTFIT are data containers i use to pass a lot of variables. It holds the type of outfit, the state of that outfit, the type of underwear, and the underwear state.
Rather then passing 4 variables in a function like, i can pass just one [code]function foo( value1 , value2 , value3 , value 4)

I didnt know we can do this. So its like XML parsing.

AnotherArrow Wrote:I do the parent setting child method vs child asking parent because of the difference...

Then, in our movieclip case, for me to make the parent MC ask something for child MC, I'll have to instantiate every body part in every frame (some poses has more than 100 frames) of every pose (50 total). This can yield 10x100x50 changes! (with mass swap, 10x50). I can also find other quick paths to do that, but I think its better to do that way. Of course, I'll have to reinforce that every MC will be 'movie clip', not 'shape/image'. So its better to create instances to every MC, correct? Or its a best pratice to do that only in MCs you will use?

AnotherArrow Wrote:The child is eating up the parents time asking to be dressed the same way. ;) Now, in my case, The parent will only set / modify the child if something changes...

You're right!

AnotherArrow Wrote:Your pic of the classes gave me the idea of sharing my setup. Maybe it will give you some ideas on how to approach future classes in your game.

Thanks for sharing! Yes, that what I want to. Its better to focus ourselves in the 'mc layout', then code them using one or more classes. And we can use design patterns, like 'observer'. But this is only interesting in a far more complex game.

AnotherArrow Wrote:
Ivan Wrote:This should work better for me, I think ;) Because I will only need a Body class, so in the picture above, the blue MC's of BODY PARTS (that arent shown) will use the red class BODY, which, in turn, will be extended, making body MC and clothes MC (which is a child MC of body) act accordingly.

Yup!

I'll make some simple tests (Its expected for me to start this way), then I will improve.
But, as for each part, I will then name everyone (put an instance name), in every pose , then I can make their father call them without worrying, and as they all use the same instance names (e.g. leg_part in MOVING will have the same name (leg_part) as JUMPING, SLEEPING), it will work as expected, correct?

Thanks again!

OwnerOfSuccuby Wrote:You can go not to root but on 1 level upper then the object you are in:(parent as MovieClip).

This I understand. But as 'stage' is not the same as 'root' (because 'root' in this case is related to Display Lists of MCs), and we can use variables in 'root' level, why we use 'stage' for variables, then?
I've read somewhere that, in multiple imported FLA's, we have different roots according to the base MC of each of these FLA's, so its better to place variables in the main class, which uses 'stage' as the 'main father'. However, in some implementations, I see that, on every of those imported FLA's, actually the stage will be the 'ROOT' of each of them, while 'root' is, in fact, the 'main father' of all those FLA's.

OwnerOfSuccuby Wrote:After you get to root layer you can use all thats childs like:
(root as MovieClip).stage.
(root as MovieClip).mygirltratata.ass.pants and etc.
The same for variables / MovieObjects and etc. So from one object you can call any object if you know where it is.

Ok, so , in this case, stage is the 'main father' of (root as MovieClip).
root as movieclip->stage->level MC->playerMC->BodypartMC->DressMC

OwnerOfSuccuby Wrote:I did not know that it was not current frame in AS2 sorry - i think only Frame Rate =( Do not use it for to long :roll: - but i really love AS2 :mrgreen:

AS2 doenst support currentLabel but yes, you can make a variable, say, state="sleeping", go to the layer "sleeping" in case you use gotoAndPlay(state). So its just a fact of creating another variable, say, 'currentState' in this timeline, so you can retrieve that later.

OwnerOfSuccuby Wrote:(parent.parent as MoviClip) - works the same. But if write in in object that is in 10 objects it will not be root ;) it will be object before object here it is.

Yes, yes, I'm aware of that. But I use to create special variables, like _level, which only exists in _root.level. So, if I'm use _parent._parent.level for a given object, thinking that i will only have _root.level.player.clothes', it will work, but in some situations (special occurrence), I can create, say, _root.level.gallery.player.clothes. Then, a call for _parent_parent._level iin a given MC will refer to _root.level.gallery._level (which is wrong) but it wont execute anyway because I can check if _level exists before doing that.
But the best pratice is to make the father refer to the child.

OwnerOfSuccuby Wrote:It is better to try - then read a book =) You can always make a little example and test it - i always do such way

Interesting enough, some books dont teach what you need, because they usually give examples of a transactional program, a database, a slot machine... generally things you will not use. And the book I'm reading focus on a 'single main class', instead of many smaller classes.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby AnotherArrow » Thu Jan 17, 2013 11:51 pm

Then, in our movieclip case, for me to make the parent MC ask something for child MC, I'll have to instantiate every body part in every frame (some poses has more than 100 frames) of every pose (50 total). This can yield 10x100x50 changes! (with mass swap, 10x50). I can also find other quick paths to do that, but I think its better to do that way. Of course, I'll have to reinforce that every MC will be 'movie clip', not 'shape/image'. So its better to create instances to every MC, correct? Or its a best pratice to do that only in MCs you will use?

um... no.. wait... no...
Unless I'm mistaken, when a parent ask it children to do something, its asking only the children on that frame... not the entire movie clip ( if that were the case, yea... i'd go back to child asking parent to save the headache).

If i'm right (i may not be, but assume i am)... using bodypart as an example
when body part tells its children to do something at a certain frame, it going to tell just the skin and the clothes what to do on that frame... so.. that frame only as 2 children.... not the 10+ in the other frames..

Ok.. just ran a trace on my code to make sure I wasn't taking out of my ass... but yea. When a parent looks for children, its only going to find the children present on that frame, not the entire movieclip.

Still, a child asking the parent is easier to code. Its how I coded my first AT game.

I didnt know we can do this. So its like XML parsing.

Ya know, i've never done XML parsing so I wouldn't know... I call it a data container, buts its just a class (not extended from anything). Think of it as my own String or Number class.

But, as for each part, I will then name everyone (put an instance name), in every pose , then I can make their father call them without worrying, and as they all use the same instance names (e.g. leg_part in MOVING will have the same name (leg_part) as JUMPING, SLEEPING), it will work as expected, correct?
I moved away from using instance names in symbols only because i would lose track of them...and if i made any big changes to code, i'd have to worry about renaming or removing the instance name. Going back to your formula, editing 10 symbols and 100frames each and yada yada yada... is worse then telling some code to find the bugger for me :)
There may have been another reason... I don't think you can have same instance names across the same symbol... maybe...hmm...don't remember.
User avatar
AnotherArrow
 
Joined: Fri Oct 29, 2010 3:22 am

Re: Flash AS3 - using 'gotoAndPlay' with stage vars inside M

Postby Ivan-Aedler » Fri Jan 18, 2013 12:36 am

AnotherArrow Wrote:um... no.. wait... no...

No???

Image

AnotherArrow Wrote:Unless I'm mistaken, when a parent ask it children to do something, its asking only the children on that frame... not the entire movie clip ( if that were the case, yea... i'd go back to child asking parent to save the headache).

This i know. Yes, thats why I use 'child asking father' approach. But I can also put gotoAndStop with a given outfit variable (class Dress, variable 'currentOutfit' and create another variable inside this timeline, like this.currentOutfit). If class variable currentOutfit is different from movieclip currentOutfit, we can send gotoAndStop to the desired frame.

AnotherArrow Wrote:I moved away from using instance names in symbols only because i would lose track of them...and if i made any big changes to code, i'd have to worry about renaming or removing the instance name. Going back to your formula, editing 10 symbols and 100frames each and yada yada yada... is worse then telling some code to find the bugger for me :)
There may have been another reason... I don't think you can have same instance names across the same symbol... maybe...hmm...don't remember.

No, you cant have same names (well, you can, and Flash may even alert you and let you pass, but any call will activate both objects). But you can have two or more same names if you're using in different KEYFRAMES. Using the same name in all keyframes for a given object is a good pratice indeed, because its expected for it to be 'the same object all the time'.
Last edited by Ivan-Aedler on Fri Jan 18, 2013 1:51 pm, edited 1 time in total.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Next

Return to General



Who is online

Users browsing this forum: No registered users