Reputation: 25
TypeError: Error #1034: Type Coercion failed: cannot convert "Um1" to flash.display.MovieClip
Um1, Um2, Um3..... MovieClip Object in my Stage
var Um: Array = new Array();
for (var i: int = 0; i < 10; i++) {
Um[i] = "Um" + Number(i + 1);
}
this.addEventListener(Event.ENTER_FRAME, HitUm);
function HitUm(event: Event) {
for (var i: int = 0; i < 10; i++) {
if(MovieClip(Um[i]).hitTestObject(car_mc.rabond_mc)) {
trace(Um[i]);
}
}
}
Upvotes: 2
Views: 66
Reputation: 14406
This line here:
Um[i] = "Um" + Number(i + 1);
is populating your Um
array with a string value. You later use that value like it's a MovieClip (which it isn't), so you get that error. Most likely your trace(Um[i])
line results in "Um1", "Um2" etc. when it should be "[Object MovieClip]"
Assuming you have instance names in the same scope you are trying to get references to, you can do one of the following.
Use getChildByName:
Um[i] = getChildByName("Um" + (i + 1));
OR use the current timeline (this) like a dictionary:
Um[i] = this["Um" + (i + 1)];
Upvotes: 4