Actionscript 3 使对象跟随鼠标问题
谢谢你抽出时间回答我的问题。所以问题是让一个简单的矩形跟随我的鼠标。我真的不明白为什么这个代码不起作用。 包类Actionscript 3 使对象跟随鼠标问题,actionscript-3,Actionscript 3,谢谢你抽出时间回答我的问题。所以问题是让一个简单的矩形跟随我的鼠标。我真的不明白为什么这个代码不起作用。 包类 { import flash.display.MovieClip; import flash.display.Stage; import flash.events.Event; public class watever extends MovieClip { var stageRef:Stage; publ
{
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);
}
}
}
对象只是在“奇怪”的地方运行,所以我试图跟踪mouseX,结果在输出中得到了一些愚蠢的数字
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
但是,如果我从父类声明它,它就可以正常工作。请问代码有什么问题?
(下面是它如何从父类开始工作)
鼠标位置相对于显示对象(
mouseX
与this.mouseX
相同,如果这能让它更清晰),因此如果您的watever
MovieClip在舞台上位于(0,50),鼠标位于(0,60),watever.mouseY
将为10
当您从document类中读取它时,您会得到相对于stage的mouseX
和mouseY
,这就是它正常工作的原因。您只需更改moveMe()即可执行以下操作:
public function moveMe(e:Event):void
{
this.x = stageRef.mouseX;
this.y = stageRef.mouseY;
}
public function moveMe(e:Event):void
{
this.x = stageRef.mouseX;
this.y = stageRef.mouseY;
}