• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Create Variables based on Instance Name

New Here ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

I have a class called twoPosition that I'm using on several different objects in my project. They're all two position objects (switches and circuit breakers) where I hide or display the alternate graphic for the object. What I'd like to do is instead of using my "twoPosition" variable, I would like it to be a dynamic variable that changes based on the instance name of the object in question. I'm using the "this.posOne.visible = true;" as there is an instance name of a movie clip inside the object that matches posOne and posTwo. I'm assuming there's some way to do a this.instanceName or something along those lines, but I'm not sure. If I set the instance name of the circuit breaker to "cb1", I'd like the variable to change to "cb1". This way I can use the same code for all of my electrical components, but be able to track them individual to simulate current flow on the drawing.

"package classes.comps

{

          import flash.events.MouseEvent;

          import flash.display.*;

          public class twoPosition extends MovieClip

          {

                    public var twopos:Boolean = true;

 

                    public function twoPosition()

                    {

                              // constructor code

                              addEventListener(MouseEvent.MOUSE_DOWN, twoPosClick);

                              buttonMode = true;

 

                    }

                    function twoPosClick(evt:MouseEvent):void

                    {

                              if (twopos)

                              {

                                        this.posOne.visible = false;

                                        this.posTwo.visible = true;

                                        twopos = false;

                              }

                              else

                              {

                                        this.posOne.visible = true;

                                        this.posTwo.visible = false;

                                        twopos = true;

                              }

                    }

          }

}"

TOPICS
ActionScript

Views

997

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 19, 2012 Dec 19, 2012

because, from what i can determine, all your symbols have child instances posOne and posTwo and they all function the same, none of them need their own class.  they can all have the same base class (eg, TwoClass):

:

package  {

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    public class TwoClass extends MovieClip{

        public function TwoClass() {

// each of your symbols has a posOne and a posTwo instance.

// i'm not sure which instance reflects what should be the initial

...

Votes

Translate

Translate
Community Expert ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

what's wrong with the code you're using now

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

There's nothing "wrong" with my code, but it lacks the functionality of using a different variable for each object. I want to be able to track each component that uses this class. Right now, they're all using the "twopos" variable. I need some way to track each one individual via its own instance name so that I can relay that information to the main file.

For example, if I have one switch and two circuit breakers, when they're all closed, it creates a signal path to ground. I want to illustrate this with a red line running through them. Whenever one of them is open, the path is black. I need to be able to change the variable of each component so that the rest of the drawing can know the individual states of the components.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

see if you get what you want by making each of your symbols have the same base class TwoClass:

package  {

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    public class TwoClass extends MovieClip{

        public function TwoClass() {

            this.posOne.visible = false;

            this.addEventListener(MouseEvent.MOUSE_DOWN, posTwoClick);

            this.buttonMode = true;

        }

        private function posTwoClick(e:MouseEvent):void{

            this.posOne.visible = !this.posOne.visible;

            this.posTwo.visible = !this.posTwo.visible;

        }

public function getPositionF():String{

if(this.posOne.visible){

return this.posOne.name;  // or, return "off"

} else {

return this.posTwo.name;  // or, return "on"

}

}

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

Could you break it down a little for me? I'm most definitely not a strong programmer. 🙂 By break down I just mean explain what you're doing in each section. I get most of it. I guess it's the GetPositionF section at the bottom that I'm confused about.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

because, from what i can determine, all your symbols have child instances posOne and posTwo and they all function the same, none of them need their own class.  they can all have the same base class (eg, TwoClass):

:

package  {

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    public class TwoClass extends MovieClip{

        public function TwoClass() {

// each of your symbols has a posOne and a posTwo instance.

// i'm not sure which instance reflects what should be the initial state of each symbol but if that is posTwo, use the following.  if the initial state is posOne, use this.posTwo.visible=false in the constructor.

            this.posOne.visible = false;

// each sybmol should have a mouse down listener with button mode enabled

            this.addEventListener(MouseEvent.MOUSE_DOWN, posTwoClick);

            this.buttonMode = true;

        }

        private function posTwoClick(e:MouseEvent):void{

// the following two lines of code toggle the visible property of posOne and posTwo.

            this.posOne.visible = !this.posOne.visible;

            this.posTwo.visible = !this.posTwo.visible;

        }

// because you want the "position" to be accessible outside this class, getPositionF is public and returns a string reflecting whether posOne or posTwo is visible (or whether this symbol is "off" or "on").

public function getPositionF():String{

if(this.posOne.visible){

return this.posOne.name;  // or, return "off"

} else {

return this.posTwo.name;  // or, return "on"

}

}

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

OH I see what you're saying. I was already using one class for all of them. Haha. I'm using the one class, but the issue is that I want to generate a variable dynamically for each component that is used so that I can track that status of whether or not a particular component's value is true or false. Does that make sense?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2012 Dec 19, 2012

Copy link to clipboard

Copied

LATEST

yes, that makes sense to use one base class.  (each symbol needs a distinct class.)

and, the code i suggested does return each components "off"/"on" or which pos item is visible or can return true/false if that's what you want.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines