0

My problem is that I can't (don't know) make work my switch. Here in my first case, I input "hache", and it doesn't pass through. Strangely, in my trace(traget); [Object hache] or [Object extincteur] (depending on which mc I click on) comes out... Why doesn't it go through the first case? I have no clue. I tried removing the " ".

package cem
{
    import flash.display.MovieClip;

    public class actionObjets{
        
        /*--inventaire--*/
        private static var inventaireHache:Boolean = false;
        private static var inventaireExtincteur:Boolean = false;
        
        private var objetClique:MovieClip;

        public function actionObjets(target) {
            this.objetClique = target;
            switch(objetClique){
                case "hache":
                    inventaireHache = true;
                    ajouterInventaire(objetClique);
                    break;
                case "extincteur":
                    inventaireExtincteur = true;
                    ajouterInventaire(objetClique);
                    break;
            }
            trace(target);
        }
        private function ajouterInventaire(objetEnlever):void{
            objetClique.parent.removeChild(objetClique);
            trace(inventaireHache + " - Hache");
            trace(inventaireExtincteur + " - Extincteur");
        }
        
    }
    
}

by the way, target is the movieClip I clicked on a.k.a. Object extincteur, or Object hache.

1 Answer 1

1

The problem is that objetClique isn't a string. You probably want to do something like switch (objetClique.name).

If you want to understand what's going on, rewrite the code this way:

if (objetClique == "hache") {
  // ...
} else if (objetClique == "extincteur") {
  // ...
}

I hope this illustrates more clearly why the switch doesn't work. objetClique couldn't be equal to the string "hache", because it's not a string. From the looks of it objetClique refers to a DisplayObject and they have a property called name, which is what you want to compare:

if (objetClique.name == "hache") {
  // ...
} else if (objetClique.name == "extincteur") {
  // ...
}

that code would work, and it's equivalent to a switch that looks like this:

switch (objetClique.name) {
  case "hache":
    // ...
    break;
  case "extincteur":
    // ...
    break;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Hummmm! That's pretty much what I need. Now, my problem is: it traces instance7, instance8 etc... Is there a way to name the instances with real names?
nvm... I just did some research. whatiwant.name = "nameiwant". Haha what a noob I am! Thx a lot!! You're a great help! I accept your answer! hehe.
If this is Flash you can click on the element on the stage and fill in the name in the name field in one of the inspectors. Otherwise you can just to myDisplayObject.name = "helloworld" wherever you have a display object you want to name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.