Reputation: 125
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;
}
Upvotes: 0
Views: 3058
Reputation: 2254
The mouse position is relative to the DisplayObject (mouseX
is the same as this.mouseX
if that makes it clearer), so if your watever
MovieClip is at (0, 50) on the stage, and the mouse is at (0, 60), watever.mouseY
will be 10.
When you read it from the document class, you're getting mouseX
and mouseY
relative to the stage, which is why it works correctly. You can just change moveMe() to do this:
public function moveMe(e:Event):void
{
this.x = stageRef.mouseX;
this.y = stageRef.mouseY;
}
Upvotes: 1