Leandri
Leandri

Reputation: 125

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?

Upvotes: 1

Views: 151

Answers (2)

Anil
Anil

Reputation: 2554

Aside from the compiler error you are experiencing, another problem here is that you are planning on using the pow variable later in your code, yet they are of different types. My suggestion is to use the benefits of Inheritance in OOP and create a base class that your two custom classes can inherit from. That way they are both technically of the same base type, while still giving you the freedom to customize each custom class, while keeping similar functionality in the base class.

Remember, OOP is here to always help you and is there to avoid issues like the one you have come across, but here is how I would do it, and I tested the following implementation in Flash CC 2014 and it compiled successfully:

Example .FLA:

var pow:BaseClass;
var power = 1;

switch(power){
    case 1:
        pow = new ObjectOne();
        break;
    case 2:
        pow = new ObjectTwo();
        break;
}

pow.whichObjectAmI();    // this will simply trace what object pow is

Base Class

package  {

    public class BaseClass {

        public function BaseClass() {
            // constructor code
        }

        public function whichObjectAmI() {
            trace("I am the base class");
        }

    }

}

Object One

package  {

    public class ObjectOne extends BaseClass {

        public function ObjectOne() {
            // constructor code
        }

        override public function whichObjectAmI() {
            trace("I am Object One!");
        }

    }

}

Object Two

package  {

    public class ObjectTwo extends BaseClass {

        public function ObjectTwo() {
            // constructor code
        }

        override public function whichObjectAmI() {
            trace("I am Object Two!");
        }

    }

}

You can always inherit from any of ActionScript's classes as well like MovieClip, Button, etc. And by doing so, you're adding custom functionality on top of their functionality so 1) you don't have to rebuild a bunch of functionality, and 2) giving you the chance to reuse their functionality while adding your own custom code!

Upvotes: 6

David Rettenbacher
David Rettenbacher

Reputation: 5120

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...

Upvotes: 2

Related Questions