Flash Actionscript 3.0 Mouse Cursor Duplicates

862 views Asked by At

I wrote a simple game and I want to add custom mouse cursor. I created MovieClip called Pointer, exported it to AS3 and wrote this code:

/* Custom Mouse Cursor
Replaces the default mouse cursor with the specified symbol instance.
*/

stage.addChild(movieClip_2);
movieClip_2.mouseEnabled = false;
movieClip_2.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_3);

function fl_CustomMouseCursor_3(event:Event)
{
    movieClip_2.x = stage.mouseX;
    movieClip_2.y = stage.mouseY;
}
Mouse.hide();

//To restore the default mouse pointer, uncomment the following lines:
//movieClip_2.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_3);
//stage.removeChild(movieClip_2);
//Mouse.show();

Here is a screenshot: enter image description here

Whenever I play the game (ctrl enter) it stops the play and duplicates the custom cursor. Is there anyway I can make it not duplicate this is very annoying and I have no idea on how to fix it.

~ EDIT 2 ~

Okay I changed the code to but the problem is now it's showing me the regular cursor and the custom one at the same time.

movieClip_1.mouseEnabled = false; movieClip_1.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor); function fl_CustomMouseCursor(event:Event) { movieClip_1.x = stage.mouseX; movieClip_1.y = stage.mouseY; } stage.removeChild(movieClip_1) Mouse.hide()

enter image description here

~ EDIT 3 ~

Thank you @LDMS for helping me. I had to remove the first line stage.addChild(movieClip_1); and it worked. :)

1

There are 1 answers

6
BadFeelingAboutThis On BEST ANSWER

Most likely, your problem stems from this line:

stage.addChild(movieClip_2);

When you add a movie clip that was created on the timeline to another display object (like the stage), it will not get removed from that new display object except through code.

If your timeline loops, then every loop it will create a new movie clip and add it to the stage (but not remove the old one).

To fix it, do one of the following:

  1. don't loop your timeline (so the code only happens once), eg put a stop() on your timeline

  2. manually remove the movie clip from the stage before the timeline loops (eg stage.removeChild(movieclip_2) at the end of your timeline

  3. Don't add it to the stage to begin with. (just take out the stage.addChild(movieClip_2); line)