Skip to main content
Known Participant
November 27, 2009
Question

a more effective way to write this ?

  • November 27, 2009
  • 1 reply
  • 866 views

Hi all,

Lately i've been trying to right in external classfiles to create everything and make everything work instead of stuffing everything in my timeline.

So now i have made a classfile that first creates all the movieclips i wan't on the stage and then defines what all the buttons do.

But when i want to acces a variable that has been created in "createMc" from a private function i get an error, so i have fixed this by giving the var's a name and then use getChildByName().

But i think this looks like a really innificiant way off writing something and was wondering if it was possible to write this in a better, shorter, and thus more effective way. Any ideas ?

Grts !

Here's the code:

package {
     import flash.display.MovieClip;
     import flash.events.*;
    
     public class createMc extends MovieClip {

          public function createMc() {
              
               var home:homeMc = new homeMc();
               home.x = 400;
               home.y = 265;
               addChild(home);
               home.name = "homeGeb";
              
               var contact:contactMc = new contactMc();
               contact.x = 400;
               contact.y = 265;
               addChild(contact);
               contact.visible = false;
               contact.name = "contGeb";
              
               var info:infoMc = new infoMc();
               info.x = 400;
               info.y = 265;
               addChild(info);
               info.visible = false;
               info.name = "infoGeb";
              
               var imp:impsMc = new impsMc();
               imp.x = 400;
               imp.y = 265;
               addChild(imp);
               addChild(imp);
               imp.visible = false;
               imp.name = "impGeb";
              
               var mulKnop:mulKnopMc = new mulKnopMc();
               mulKnop.x = 400;
               mulKnop.y = 50;
               mulKnop.buttonMode = true;
               addChild(mulKnop);
              
               var homeBtn:homeKnopMc = new homeKnopMc();
               homeBtn.x = 179.15;
               homeBtn.y = 569.5;
               homeBtn.buttonMode = true;
               addChild(homeBtn);
              
               var impBtn:impsKnopMc = new impsKnopMc();
               impBtn.x = 293.3;
               impBtn.y = 569.5;
               impBtn.buttonMode = true;
               addChild(impBtn);
              
               var infBtn:infoKnopMc = new infoKnopMc();
               infBtn.x = 452.7;
               infBtn.y = 569.5;
               infBtn.buttonMode = true;
               addChild(infBtn);
              
               var contBtn:contKnopMc = new contKnopMc();
               contBtn.x = 574.2;
               contBtn.y = 569.5;
               contBtn.buttonMode = true;
               addChild(contBtn);
              
               homeBtn.addEventListener(MouseEvent.CLICK, homeClick);
              
               impBtn.addEventListener(MouseEvent.CLICK, impClick);
              
               infBtn.addEventListener(MouseEvent.CLICK, infClick);

               contBtn.addEventListener(MouseEvent.CLICK, contClick);

              
          }
         
          private function homeClick(evt:MouseEvent):void {
               trace("geklikt");
              
               var home: MovieClip = MovieClip(getChildByName("homeGeb"));
               var imps: MovieClip = MovieClip(getChildByName("impGeb"));
               var info: MovieClip = MovieClip(getChildByName("infoGeb"));
               var contact: MovieClip = MovieClip(getChildByName("contGeb"));
              
               home.visible = true;
               imps.visible = false;
               info.visible = false;
               contact.visible = false;
          }
         
          private function impClick(evt:MouseEvent):void {
               trace("geklikt");
              
               var home: MovieClip = MovieClip(getChildByName("homeGeb"));
               var imps: MovieClip = MovieClip(getChildByName("impGeb"));
               var info: MovieClip = MovieClip(getChildByName("infoGeb"));
               var contact: MovieClip = MovieClip(getChildByName("contGeb"));
              
               home.visible = false;
               imps.visible = true;
               info.visible = false;
               contact.visible = false;
          }
         
          private function infClick(evt:MouseEvent):void {
               trace("geklikt");
              
               var home: MovieClip = MovieClip(getChildByName("homeGeb"));
               var imps: MovieClip = MovieClip(getChildByName("impGeb"));
               var info: MovieClip = MovieClip(getChildByName("infoGeb"));
               var contact: MovieClip = MovieClip(getChildByName("contGeb"));
              
               home.visible = false;
               imps.visible = false;
               info.visible = true;
               contact.visible = false;
          }
         
          private function contClick(evt:MouseEvent):void {
               trace("geklikt");
              
               var home: MovieClip = MovieClip(getChildByName("homeGeb"));
               var imps: MovieClip = MovieClip(getChildByName("impGeb"));
               var info: MovieClip = MovieClip(getChildByName("infoGeb"));
               var contact: MovieClip = MovieClip(getChildByName("contGeb"));
              
               home.visible = false;
               imps.visible = false;
               info.visible = false;
               contact.visible = true;
          }


     }
}

And yet another question:

I have created this: http://semagames.be/testplaats/portHome.swf

Practically without any code (just using the bone tool for the movement of the strings).

But what i want now is that when you lift up the strings and release them is that they under influence of the gravity swing back to their original places. Just like a pendulum or something like that.

How can that be achieved ?

Grts !

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
November 27, 2009

I am not well-versed in class matters, but I'll offer the following.  In your createMC function you declare all of the ovjects anddefine them.  You even aassign name properties to some of tjem.  I believe you should pull all of the "var" declarations out of the function and make them vars that are accessible to all functions of the class.

public var home:homeMc;

public var contact:contactMc;

public var info:infoMc;

etc...

public function createMc() {
              
               home = new homeMc();
               home.x = 400;
               home.y = 265;
               addChild(home);
               // home.name = "homeGeb";  you should not need this

Notice that I am removing the name assignment.  The var name itself will be used.  And for the other functions, to reduce and combine some of the work I am creating a new hideAll function, so that each time a button is clicked, first the hideAll function is executed, and then the desired object is made visible....

          private function hideAll():void { // new function

               home.visible = false;
               imps.visible = false;
               info.visible = false;
               contact.visible = false;
          }

          private function homeClick(evt:MouseEvent):void { // your updated function
               hideAll();

               home.visible = true;
          }

Known Participant
November 28, 2009

Hi,

Thx M8!

This is already a hell of a lot better

Grts !

November 28, 2009

It looks like you're setting the visibility of the non clicked buttons to false and the clicked one to true? You could do something like this:

package
{
     import flash.display.MovieClip;
     import flash.utils.getDefinitionByName;
     import flash.events.*;

     public class CreateMC extends MovieClip
     {
          var btns:Array;
          
          public function CreateMC()
          {

                        //add as many buttons as needed, and their x,y positions
               btns = new Array( [ "homeKnopMc", 179, 269 ], [ "impsKnopMc", 293, 269 ], [ "infoKnopMc", 453, 269 ] );
               
                for (var i:int = 0; i < btns.length; i++) {
                     var c:Class = getDefinitionByName(btns[0]) as Class;
                     var a:MovieClip = new(c);
                     addChild(a);
                     a.x = btns[1];
                     a.y = btns[2];
                     a.buttonMode = true;
                     a.btnIndex = i; //store the array index in the button
                     a.addEventListener(MouseEvent.CLICK, buttonClicked, false, 0, true);
                     btns[3] = a; //store the button reference in the array
                }
          }

                //set visibility depending on which button was clicked
          private function buttonClicked(e:MouseEvent):void
          {
               var clickIndex:int = e.currentTarget.btnIndex;
               for (var i:int = 0; i < btns.length; i++) {
                    if (btns[3].btnIndex == clickIndex) {
                         btns[3].visible = true;
                    }else {
                         btns[3].visible = false;
                    }
               }
          }
     }     
}

I think this should work for what you're doing.