Skip to main content
Participant
October 26, 2020
Answered

ActionScript 3: MovieClip not following cursor

  • October 26, 2020
  • 1 reply
  • 493 views

I'm working on testing an interactive animation where a spider follows my cursor with a delay and a fly follows my cursor much more closely, to give the illusion that the spider is chasing the fly. However, when I test the animation the spider moves as it should, but the fly stays at a radius far from the cursor without approaching it. I don't know much about coding, but the code I have for the spider and the fly is the same, so I'm not sure why only the spider is acting correctly. Here's the code I'm using:

 

 

this.addEventListener(Event.ENTER_FRAME, MoveSpider);
function MoveSpider(evt:Event):void
{
  var xMouse = root.stage.mouseX;
  var yMouse = root.stage.mouseY;
  if(Math.abs(xMouse - Spider.x) < 1)

  {
    //if spider is within 1px move to mouse position
    Spider.x = xMouse;
    Spider.y = yMouse;

  }
  else
  {
    //move spider 1/6 of the way to mouse each frame
    Spider.x -= (Spider.x-xMouse) / 100;
    Spider.y -= (Spider.y-yMouse) / 100;

  }
}

this.addEventListener(Event.ENTER_FRAME, MoveFly);
function MoveFly(evt:Event):void
{
  var xMouse = root.stage.mouseX;
  var yMouse = root.stage.mouseY;
  if(Math.abs(xMouse - Fly1.x) < 1)

  {
    //if fly is within 1px move to mouse position
    Fly1.x = xMouse;
    Fly1.y = yMouse;

  }
  else
  {
    //move fly 1/6 of the way to mouse each frame
    Fly1.x -= (Fly1.x-xMouse) / 100;
    Fly1.y -= (Fly1.y-yMouse) / 100;

  }
}

 

If anyone knows why my fly wants to social distance I'd be grateful for any help!

This topic has been closed for replies.
Correct answer harlow

Idk how to delete this, but the fly MovieClip wasn't centered on the stage. That's all it was. I am dumb.

1 reply

harlowAuthorCorrect answer
Participant
October 26, 2020

Idk how to delete this, but the fly MovieClip wasn't centered on the stage. That's all it was. I am dumb.

JoãoCésar17023019
Community Expert
Community Expert
October 26, 2020

Hi.

 

You're not dumb. Programming is often tricky.

 

I will mark your answer as correct in case someone comes across the same situation as yours.

 

Regards,

JC