0
votes

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
The game game i'm making you have to shoot the zombie and the zombie moves. I want to make it not duplicate - MatthewTheMan2
It's best to use the MouseEvent.MOUSE_MOVE event instead of ENTER_FRAME, unless you have the enterframe event doing other things anyway. What exactly is duplicating? You see two mouse cursors? - BadFeelingAboutThis
If you tell me a program that I can make a gif I can show you what I mean. Every 5 seconds the cursor duplicates, I have no idea why - MatthewTheMan2
Actually, most likely the problem is whatever frame you have the cursor on is getting looped/repeated. And since you're adding the cursor to the stage, it won't get removed automatically so more and more get added. - BadFeelingAboutThis
Nope, I have no other code but the one I have in the main post. - MatthewTheMan2

1 Answers

1
votes

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)