PDA

View Full Version : Noob question: Shared listeners



stupidsaint
June 6th, 2009, 01:33 PM
I have this code:



var numOfLinks = 4
for (var i=0; i<numOfLinks; i++) {
var breakImg = new breaker();
breakImg.x = 0
breakImg.y = stage.stageHeight/numOfLinks*i
addChild(breakImg);
breakImg.addEventListener(Event.ENTER_FRAME, mover);
}

function mover(e:Event){
var thim:MovieClip=MovieClip(e.target);
thim.x++
}


I want all of them to move to the right but it keeps returning this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@ebc1629 to flash.display.MovieClip.
at as3_navigation_fla::MainTimeline/texterMover()

I just need all of the movie clips to have the same listener

Can anyone help

BenBart
June 6th, 2009, 01:55 PM
var thim:MovieClip=MovieClip(e.target);


When you use the Method MovieClip(), you are casting the variable type of what's inside the () as a MovieClip, but you are adding the eventListener to a TextField. So, just change this line to:


var thim = e.target ;


and it will work for both. Although, it's not exactly best practice. I can't remember offhand what the best practice is. Maybe someone else knows?