Thank you for taking the time to answer my question. So the problem is making a simple rectangle follow my mouse. I really don’t understand why this code isn’t working.
package Classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class watever extends MovieClip
{
var stageRef:Stage;
public function watever(x:Number, y:Number, stageRef:Stage)
{
this.x = x;
this.y = y;
this.stageRef = stageRef;
addEventListener(Event.ENTER_FRAME, moveMe, false, 0, true);
}
public function moveMe(e:Event):void
{
this.x = mouseX;
this.y = mouseY;
trace(mouseX);
}
}
}
The object just goes on “strange” places, so I tried to trace mouseX and I got silly numbers in the output
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
However, if I declare it from the parent class it works fine. What’s wrong with the code, please?
(Below is how it works from the parent class)
public function DocumentClass()
{
c = new watever(200, 200, stage)
stage.addChild(c);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event):void
{
c.x = mouseX;
c.y = mouseY;
}
The mouse position is relative to the DisplayObject (
mouseXis the same asthis.mouseXif that makes it clearer), so if yourwateverMovieClip is at (0, 50) on the stage, and the mouse is at (0, 60),watever.mouseYwill be 10.When you read it from the document class, you’re getting
mouseXandmouseYrelative to the stage, which is why it works correctly. You can just change moveMe() to do this: