actionscript-3flashflash-cs5.5

AS3 How to declare an object without the dreaded "Conflict Exists" error?


I am designing a simple game in Flash and have come across this error. I have no idea how to go about this in actionscript and would appreciate any help.

Basically, I have a switch statement which creates an object of different type depending on each case (as I would prefer not to duplicate the same ten lines of code for each case) and I am getting a "conflict exists with definition in namespace internal" compiler error and I think I understand why.

switch(power){
    case 1:
        var Pow:objectOne = new objectOne();
        break;
    case 2:
        var Pow:objectTwo = new objectTwo();
        break;
}

My question however is this - what is the proper way of going about this?

I initially thought of declaring the variable before the switch statement which results in an "implicit coercion of a value of type object(One/Two) to an unrelated type Class" error. What am I missing here?


Solution

  • Disclaimer: My AS3 is a little rusty ;)

    Of what type would the variable Pow be after the switch statement? objectOne or objectTwo? From the compiler's perspective objectOne and objectTwo could be totally different from each other (read: methods, fields,...)

    So:
    A) Keep variable name for both assignments but declare it before the switch-statement AND use a common base-type (object, MovieClip,...)
    B) Have 2 different variables: var PowOne: objectOne and var PowTwo: objectTwo

    I think option A would be preferable...