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

A place for general discussions about anything and everything.

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

Postby OwnerOfSuccuby » Fri Jan 18, 2013 4:18 am

By the way - viewtopic.php?f=29&t=2370

I try to write a little topic about AS2 <-> AS3 - may be you will find some thing interesting - there are some changes in hitTest / working with php and with colours in AS3.
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

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

Postby Ivan-Aedler » Fri Jan 18, 2013 6:15 am

OwnerOfSuccuby Wrote:By the way - viewtopic.php?f=29&t=2370 I try to write a little topic about AS2 <-> AS3 - may be you will find some thing interesting - there are some changes in hitTest / working with php and with colours in AS3.

Thanks, I will check that!
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 » Fri Jan 18, 2013 5:17 pm

Well, in my experimenting, I found a way to find out how to identify a symbol without making multiple inherited class.

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{
       if(this is *VEST CLASS NAME*)
          DressVest();
       else if (this is *ARM CLASS NAME*)
          DressArm();
       else if (this is *FOOT CLASS NAME*)
          DressFoot();
                 else
                    gotoAndStop(GetDress());
      }
      protected function cRoot():Game{  // Convert root to game.
          return (root as Game);
      }
      protected function GetDress():String{
          return (cRoot().DressSel[ cRoot().DressNum ] as String);  // May have to try this code if return throws an error
      }
    
     protected function DressVest():void{
        if( currentFrameLabel != GetDress() ){
              if ( cRoot().clothes_vest_remove && cRoot().DressNum != 3)
                  gotoAndStop("naked");
              else
                  gotoAndStop(GetDress());
           }
     }
     protected function DressFoot():void{
        // Foot dress code
     }
     protected function DressArm():void{
        // Arm dress code
     }
   }
}

Change the *CLASS NAME* code to the AS Linkage string / class (not base class).
So, for example, a symbol called BodyClothes would have the Base Class "dress" and the class "BodyClothes". Therefore you would change the code to
Code: Select All Code
if(this is *VEST CLASS NAME*)
becomes
if(this is BodyClothes)
and so forth.

Learned something new :D
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 5:30 pm

AnotherArrow Wrote:Well, in my experimenting, I found a way to find out how to identify a symbol without making multiple inherited class.

You're THE man!!

Image
Now, its only a matter to optimize it further! How?
Avoiding it to run getDress at every frame, in case the global DressSel hasnt changed at all (e.g. the player is just moving the character without changing her outfits or making her lose one).

How I do this in AS2:
(first frame of any clothing part MC):
UPDATED and CORRECTED CODE -> its placed on the ver FIRST frame of every Clothes MC.
Code: Select All Code
//QUICK placement just before the frame is drawn!
if (_root.gloves_type=="None")
     gotoAndStop("Naked");
else
     gotoAndStop(_root.DressSel[_root.DressNum]);

//ABOVE must not be executed anymore while the SAME MC is acting (like in MOVING pose). Thats why I use this in a 'control' frame (currentFrame will never select it again.

if (!once)
{
once=true;
onEnterFrame=function()
 {
    if (currentlabel!=_root.DressSel[_root.DressNum])
    {
      if (_root.DressNum=="13")
       {
        if (_root.gloves_type=="None")
          gotoAndStop("Naked");
        else
         gotoAndStop(_root.DressSel[_root.DressNum]);
        }
       else
            gotoAndStop(_root.DressSel[_root.DressNum]);
     }
     currentlabel=_root.DressSel[_root.DressNum];
   }
 }


With AS3, we already have the function currentLabel, so that variable set is not required: 'currentlabel=_root.DressSel[_root.DressNum]'
So, as we know every frame will check the father (due to OnEnterFrame), we can make it call getDress ONLY if the currentLabel is different from the global DressNum: (root as Movieclip).DressSel.
Last edited by Ivan-Aedler on Fri Jan 18, 2013 6:20 pm, edited 3 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 » Fri Jan 18, 2013 6:03 pm

Avoiding it to run getDress at every frame, in case the global DressSel hasnt changed at all (e.g. the player is just moving the character without changing her outfits or making her lose one).

I can't think of a way ATM, cause you're still doing the "child ask parent" method. Even if you have a boolean variable like "HasDressChange" as a global, the Code is still going to have to ask the parent (or in this case the root) "do the clothes need to be changed".

How I do this in AS2:
(first frame of any clothing part MC):

That code is at the beginning of every clothing part?.... or is that an example of the gloves?
I ask because as I read it, it says
Code: Select All Code
For Function OnEnterFrame
   If current label is not the current dress
        check if the gloves are  worn
             if no, make "naked"
        if so, set clothes
    else (current label is the current dress)
         put that dress back on again
The way i read that, if gloves are not worn, the entire character would be naked. Thats why I ask, but if thats just the Glove Code, then:
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{
       if(this is *VEST CLASS NAME*)
          DressVest();
       else if (this is *Glove CLASS NAME*)
          DressGloves();
       else if (this is *FOOT CLASS NAME*)
          DressFoot();
       else
          if( currentFrameLabel != GetDress() )
               gotoAndStop(GetDress());
      }
    
      protected function cRoot():Game{       return (root as Game);}
      protected function GetDress():String{   return (cRoot().DressSel[ cRoot().DressNum ] as String);}
    
     protected function DressVest():void{
        if( currentFrameLabel != GetDress() ){
              if ( cRoot().clothes_vest_remove && cRoot().DressNum != 3)
                  gotoAndStop("naked");
              else
                  gotoAndStop(GetDress());
           }
     }
     /************** NEW CODE HERE **************************/
     protected function DressGloves():void{
         if( currentFrameLabel != GetDress() ){
              if ( cRoot().gloves_type == "None")
                  gotoAndStop("naked");
              else
                  gotoAndStop(GetDress());
           }
      }
     /******************************************************/
      protected function DressFoot():void{
        // Foot dress code
     }
   }
 }
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 6:13 pm

AnotherArrow Wrote:I can't think of a way ATM, cause you're still doing the "child ask parent" method. Even if you have a boolean variable like "HasDressChange" as a global, the Code is still going to have to ask the parent (or in this case the root) "do the clothes need to be changed".

Yes, I'm still calling the father, but I'm calling them only to ask if a given variable is equal to another, instead of activating gotoAndPlay/Stop.

The code above is for every clothing part, sorry, I havent explained. It uses only one frame code, as onEnterframe.
So, 'naked' means 'that body part will be naked. As I use the 'sub movie clip Dress' for every body part, I 'play' with the outfits for that given part.
So the body can be naked, the torso not. The legs can have pink or blue garter, the arm can have black or brown gloves. The head can be naked.

But, in a normal approach, every new frame WILL trigger getDress, even if not needed. Using the currentFrameLabel, we trigger getDress once in a given animation.
That because, when the character is stopped, even with animation (tween ones), all body , legs, arms instances will be affected, even if the timeline has 100, 1000 frames.
Using onEnterFrame without currentFrameLabel, it will always set getDress there, which is not necessary (I've tested here in AS2).

AnotherArrow Wrote: /************** NEW CODE HERE **************************/
protected function DressGloves():void{
if( currentFrameLabel != GetDress() ){
if ( cRoot().gloves_type == "None")
gotoAndStop("naked");
else
gotoAndStop(GetDress());
}
}
/******************************************************/

Thats it!! Now we are calling the father ONLY to ask if currentFrameLabel is the current dress. If not, new dress were worn!
But, in order to avoid blinking in the first frame, we can put ' gotoAndStop(GetDress())' in the beginning, outside onEnterFrame, in a way its played only once in each dress MC. In AS2, using the code above I've shown you, I use gotoAndStop(DressSel[DressNum]) once, because it needs to point to the correct frame just before its drawn on the screen. After this, the code is not executed anymore because its OUTSIDE the onEnterFrame. (sorry I havent put the code above there).

UPDATED and CORRECTED CODE -> its placed on the ver FIRST frame of every Clothes MC.
Code: Select All Code
//QUICK placement just before the frame is drawn!
if (_root.gloves_type=="None")
     gotoAndStop("Naked");
else
     gotoAndStop(_root.DressSel[_root.DressNum]);

//ABOVE must not be executed anymore while the SAME MC is acting (like in MOVING pose). Thats why I use this in a 'control' frame (currentFrame will never select it again.
//I use 'once' to ENSURE it wont be executed again, just in an ALIEN case that this frame is called again in the same instance (e.g. player keeping the character MOVING all the time)
if (!once)
{
once=true;
onEnterFrame=function()
 {
    if (currentlabel!=_root.DressSel[_root.DressNum])
    {
      if (_root.DressNum=="13")
       {
        if (_root.gloves_type=="None")
          gotoAndStop("Naked");
        else
         gotoAndStop(_root.DressSel[_root.DressNum]);
        }
       else
            gotoAndStop(_root.DressSel[_root.DressNum]);
     }
     currentlabel=_root.DressSel[_root.DressNum];
   }
 }


one frame.png
one frame.png (33.04 KiB) Viewed 1225 times
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 » Fri Jan 18, 2013 6:24 pm

But, in order to avoid blinking in the first frame, we can put ' gotoAndStop(GetDress())' in the beginning, outside onEnterFrame, in a way its played only once in each dress MC. In AS2, using the code above I've shown you, I use gotoAndStop(DressSel[DressNum]) once, because it needs to point to the correct frame just before its drawn on the screen. After this, the code is not executed anymore because its OUTSIDE the onEnterFrame. (sorry I havent put the code above there).


i always forget about that glitch.... here... modify the constructor
Code: Select All Code
 public function Dress() {
         this.addEventListener(Event.ADDED_TO_STAGE, DressOnEnterFrame)
         this.addEventListener(Event.ENTER_FRAME, DressOnEnterFrame)
      }
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 6:28 pm

AnotherArrow Wrote:i always forget about that glitch.... here... modify the constructor

Please read above again, I've updated some text and I've put an image to clarify, thanks ;)

Can I use ADDED_TO_STAGE in AS2? Is there a 'addedToStage()' function or something like that?
If not, nevermind. ;) Thats because its simple to just add core in a control frame (the very first frame of every MC), which will never be executed again while in the same pose.

Well, I think my logic is interesting. The child will keep asking the parent, yes, but it wont be activating getDress in the same character pose all the time, if the outfit for all body parts arent modified.
Last edited by Ivan-Aedler on Sat Jan 19, 2013 2:45 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 » Fri Jan 18, 2013 7:08 pm

Please read above again, I've updated some text and I've put an image to clarify, thanks

Reread... but maybe i'm still missing the problem. You may need to highlight the questions in the post to help me... I was never good at English.

1) Are we coding for as2 or as3? Because i haven't touch as2 in 3 years... and don't recall much of its methods... so I don't know if there is an AddToStage functions. There might be,
try...
Code: Select All Code
onAddtoStage=function(){
//code.....
}


2)
But, in a normal approach, every new frame WILL trigger getDress, even if not needed. Using the currentFrameLabel, we trigger getDress once in a given animation.
That because, when the character is stopped, even with animation (tween ones), all body , legs, arms instances will be affected, even if the timeline has 100, 1000 frames.
Using onEnterFrame without currentFrameLabel, it will always set getDress there, which is not necessary (I've tested here in AS2).

Is there a question here? You are trying to not run the gotoAndStop over and over, right?
In the DressOnEnterFrame
Code: Select All Code
protected function DressOnEnterFrame(e:Event):void{
       if(this is *VEST CLASS NAME*)
          DressVest();                                                //<------ runs if symbol is a vest
       else if (this is *Glove CLASS NAME*)
          DressGloves();                 //<------------ runs if symbol is a glove
       else if (this is *FOOT CLASS NAME*)
          DressFoot();                    // <----------- runs if symbol is a boot
       else
          if( currentFrameLabel != GetDress() )           // <----------- this should prevent it from calling gotoAndStop over and over.
               gotoAndStop(GetDress());
      }


3)
But, in order to avoid blinking in the first frame, we can put ' gotoAndStop(GetDress())' in the beginning, outside onEnterFrame, in a way its played only once in each dress MC. In AS2, using the code above I've shown you, I use gotoAndStop(DressSel[DressNum]) once, because it needs to point to the correct frame just before its drawn on the screen. After this, the code is not executed anymore because its OUTSIDE the onEnterFrame. (sorry I havent put the code above there).

The new listener I added to the constructor will run once the symbol enters the stage... so when the character first spawns there, it will the DressOnEnterFrame before rendering to screen prevent the "blink".
or.. as i continue to reread that statement... Are you asking how to stop onEnterFrame code entirely from running once its at the correct frame? I don't know if thats possible in as 3.....hmm... i just had some thoughts, but that code is going far and beyond what you would need it for... You're just trying to stop all the calls to "gotoAndStop", right? the onEnterframe should do that.
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 9:41 pm

AnotherArrow Wrote:Reread... but maybe i'm still missing the problem. You may need to highlight the questions in the post to help me... I was never good at English.

I've PM you ;)

AnotherArrow Wrote:1) Are we coding for as2 or as3?

AS3. I've used AS2 examples to illustrate who I do things so far here.
I tried to google it. No onAddToStage (no any alternative) but I dont mind.

AnotherArrow Wrote:You are trying to not run the gotoAndStop over and over, right?

Yes! The child will still talk to its parents, but they will check if a given variable is set (aka. Changed outfits while in the same pose using currentFrameLabel). Like we already know, it the currentFrameLabel is not the current DressSel[DressNum], it will call gotoandStop. Of course, if the player is changing poses too much (say, one per frame), the currentFrameLevee checking is useless, the same with AddToStage, because every new pose is a new 'live instantiation', as we know. So the pointer will be always start on frame1, requiring it to be set correctly. But we also know the player is not maniac like this, so he stops playing to breathe, he keeps pressed the jump button, he swims a bit... so we have more excuses to use functions that avoids re-use of getDress all the time.

AnotherArrow Wrote:In the DressOnEnterFrame
Code: Select All Code
protected function DressOnEnterFrame(e:Event):void{
       if(this is *VEST CLASS NAME*)
          DressVest();                                                //<------ runs if symbol is a vest
       else if (this is *Glove CLASS NAME*)
          DressGloves();                 //<------------ runs if symbol is a glove
       else if (this is *FOOT CLASS NAME*)
          DressFoot();                    // <----------- runs if symbol is a boot
       else
          if( currentFrameLabel != GetDress() )           // <----------- this should prevent it from calling gotoAndStop over and over.
               gotoAndStop(GetDress());
      }


If it's a DressVest, Dressgloves, DressFoot, etc, each of them will still executes their protected functions that already have a GetDress checking. But if I understand, this ELSE is an exception. It just tells the program not do keep 'gotoingAndStop' if the affected part isnt a Dress part (glove, foot, vest clothes and so on).

AnotherArrow Wrote:The new listener I added to the constructor will run once the symbol enters the stage... so when the character first spawns there, it will the DressOnEnterFrame before rendering to screen prevent the "blink".

Gotcha! Thats what we need ;)

AnotherArrow Wrote:or.. as i continue to reread that statement... Are you asking how to stop onEnterFrame code entirely from running once its at the correct frame?

No, no, onEnterFrame will still execute, no problem. But in AS2, I put 'ONCE' just to ensure that frame (in this case, a frame with 'code' label) will not execute in any moment in the same pose. But it's not required, really. DressSel will never call 'code' anyway, and future poses will put the pointer back to frame1, because it will be a new onEnterFrame anyway. ONCE will not exist.

AnotherArrow Wrote:You're just trying to stop all the calls to "gotoAndStop", right? the onEnterframe should do that.

Thats it! Once onEnterFrame detects the currentFrameLabel still points to the same dress, it wont activate gotoAndStop.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Previous

Return to General



Who is online

Users browsing this forum: No registered users