in

AS3 - error 1046

Hi,
I have a problem and i hope someone out there can help me.

I am creating a photo gallery with three different categories. I want to use movieclips that i import to the scene... I have written a code and i get a error message, error 1046 -  Type was not found or was not a compile-time constant: (zebra)

Can anyone help me?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
stop();

var zebra:MovieClip = new mc_zebra();
var bear:MovieClip = new mc_bear();
var owl:MovieClip = new mc_owl();
var owlTwo:MovieClip = new mc_owl2();
var empty:MovieClip = new mc_empty();

empty.name="picture";
zebra.name="picture";
bear.name="picture";
owl.name="picture";
owlTwo.name="picture";

i_zebra.addEventListener(MouseEvent.CLICK, showZebra);
i_bear.addEventListener(MouseEvent.CLICK, showBear);
i_owl.addEventListener(MouseEvent.CLICK, showOwl);
i_owlTwo.addEventListener(MouseEvent.CLICK, showSecondOwl);

i_ruta.addChild(empty);

function showZebra(e:Event) {
	var tempMC:MovieClip;
	tempMC = i_ruta.getChildByName("picture") as MovieClip;
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(zebra);
	}
	
function showBear(e:Event) {
	var tempMC:MovieClip;
	tempMC = i_ruta.getChildByName("picture") as MovieClip;
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(bear);
	}	
	
function showOwl(e:Event) {
	var tempMC:MovieClip;
	tempMC = i_ruta.getChildByName("picture") as MovieClip;
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(owl);
	}		
	
function showSecondOwl(e:Event) {
	var tempMC:MovieClip;
	tempMC = i_ruta.getChildByName("picture") as MovieClip;
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(owlTwo);
	}

Solution: AS3 - error 1046

Well, it is kind of hard not seeing all the layers and objects used; but I guessed at what was what and think I see the problem.  I revised your code to eliminate the redundancy.  Once you add a variable, you can't add another variable of the same name, which is one of the things occurring in your functions.  The prefix "var" is only needed to add the variable.  Once it is added, no prefix is needed and if you use it, it gives you an error because it thinks you're trying to add another.  So, every function you were re-adding the same variable.

Also, you redefine that variable each time with the same definition.  Sometimes this will cause an error as well, but not always.  Just depends on how it is done.  So, to be on the safe side I just moved the definition up with the addition, because it doesn't change anyway.



The final thing I saw is, you are adding and removing the child.  This is where I had trouble by not seeing what objects were involved.  Is there a reason you are adding and removing the child rather than just loading and unloading the URL?  I ask because loading the URL (and unloading) takes less resources than creating a child.  Less resources means faster speeds.  So, the error may stem from that as well, as the load and delete times for the child will continually increase as resources are used up.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
stop();

var zebra:MovieClip = new mc_zebra();
var bear:MovieClip = new mc_bear();
var owl:MovieClip = new mc_owl();
var owlTwo:MovieClip = new mc_owl2();
var empty:MovieClip = new mc_empty();
var tempMC:MovieClip;

tempMC = i_ruta.getChildByName("picture") as MovieClip;

empty.name="picture";
zebra.name="picture";
bear.name="picture";
owl.name="picture";
owlTwo.name="picture";

i_zebra.addEventListener(MouseEvent.CLICK, showZebra);
i_bear.addEventListener(MouseEvent.CLICK, showBear);
i_owl.addEventListener(MouseEvent.CLICK, showOwl);
i_owlTwo.addEventListener(MouseEvent.CLICK, showSecondOwl);

i_ruta.addChild(empty);

function showZebra(e:Event) {
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(zebra);
	}
	
function showBear(e:Event) {
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(bear);
	}	
	
function showOwl(e:Event) {
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(owl);
	}		
	
function showSecondOwl(e:Event) {
	i_ruta.removeChild(tempMC);
	i_ruta.addChild(owlTwo);
	}