Repeat a Mouse Over event

actionscriptactionscript-3flash

Hi I have an actionscript that moves a box across the stage when the mouse is over a left or right arrow shaped button . The script below does just that. BUT what I want to do is have the box repeatedly move until the mouse is moved off the arrow button . I have tried all ways can anybody please point me in the right direction .I have removed a lot of the code but hope this is enough to get my point across. Thanks Mick

right_arrow.addEventListener(MouseEvent.mouseOver, moveR) ;
left_arrow.addEventListener(MouseEvent.mouseOver, moveL) ;

function moveL(e:MouseEvent)    {
box_image.x = box_image.x - 5 ;
    }

Best Answer

you could use the setInterval Method:

right_arrow.addEventListener(MouseEvent.mouseOver, handleMouseOver) ;
right_arrow.addEventListener(MouseEvent.mouseOut, handleMouseOut) ;

function handleMouseOver( event:MouseEvent):void {
  setTimeout( moveBoxR, 500 ); //every 500ms
}

function handleMouseOut( event:MouseEvent):void {
  clearTimeout( moveBoxR );
}

function moveBoxR() {
  box_image.x -= 5 ;
}

or the ENTER_FRAME Event

right_arrow.addEventListener(MouseEvent.mouseOver, handleMouseOver) ;
right_arrow.addEventListener(MouseEvent.mouseOut, handleMouseOut) ;

function handleMouseOver( event:MouseEvent):void {
  addEventListener( Event.ENTER_FRAME, moveBoxR )
}

function handleMouseOut( event:MouseEvent):void {
  removeEventListener( Event.ENTER_FRAME, moveBoxR )
}

function moveBoxR(event:Event) {
  box_image.x -= 5 ;
}
Related Topic