使对象跟随鼠标问题 [英] Making object follow the mouse issue

查看:88
本文介绍了使对象跟随鼠标问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您抽出宝贵时间来回答我的问题。所以,问题是做一个简单的矩形跟着我的鼠标。我真不明白为什么这code不工作。 包中的类

  {
    进口的flash.display.MovieClip;
    进口的flash.display.Stage;

    进口对象类型:flash.events.Event;


    公共类watever扩展影片剪辑
    {
        VAR stageRef:舞台;

        公共职能watever(X:数x,y:号码,stageRef:第一阶段)
        {
            this.x = X;
            this.y = Y;
            this.stageRef = stageRef;
            的addEventListener(Event.ENTER_FRAME,moveMe,假,0,真正的);

        }


        公共职能moveMe(五:事件):无效
        {
            this.x = mouseX;
            this.y = mouseY的;

            跟踪(mouseX);
        }
    }
}
 

对象只是那张奇怪的地方,所以我试图跟踪mouseX和我在输出中得到了傻号

  -1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
1790
-1373
 

不过,如果我从父类中声明它,它工作正常。什么是错的code,好吗? (下面是它如何工作的父类)

 公共职能的DocumentClass()
        {


            C =新watever(200,200,舞台)
            stage.addChild(C);
            的addEventListener(Event.ENTER_FRAME,环路,假,0,真正的);

        }
        私有函数环(五:事件):无效
        {
            c.x = mouseX;
            c.y = mouseY的;
        }
 

解决方案

鼠标的位置是相对的DisplayObject( mouseX 是一样的 this.mouseX 如果这使得它更清晰),所以如果你的 watever 影片剪辑是在(0,50)在舞台上,和鼠标在(0,60), watever.mouseY 将是10。

当您从文档类读它,你得到 mouseX mouseY的相对于舞台,这就是为什么它工作正常。你可以改变moveMe()来做到这一点:

 公共职能moveMe(五:事件):无效
{
    this.x = stageRef.mouseX;
    this.y = stageRef.mouseY;
}
 

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 (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;
}

这篇关于使对象跟随鼠标问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆