as3:MOUSE_OVER缓慢/无法正常工作

时间:2010-08-31 00:17:25

标签: actionscript-3

好的,所以我的工作是纯粹的as3(没有创意套件或xml)。我有一个精灵。它绘制一个大矩形,然后是一个略小的矩形。当我将鼠标悬停在它上面时,我想改变略小的矩形的颜色。现在虽然它不会响应(我可以明显地将鼠标放在矩形上并且没有任何反应)或者响应缓慢。此外,矩形的碰撞区域似乎有点偏离,即当我将鼠标放在矩形的左上角时,它比我在其他地方放置鼠标时响应更频繁。

无论如何这里是我正在使用的代码:

public function MAIN()
        {
   BUTTON_1.graphics.clear();
   BUTTON_1.graphics.beginFill(0x000000);
   BUTTON_1.graphics.drawRect(188,96,104,24);
   BUTTON_1.graphics.endFill();
   BUTTON_1.graphics.beginFill(0x0000DC);
   BUTTON_1.graphics.drawRect(190,98,100,20);
   BUTTON_1.graphics.endFill();
   addChild(BUTTON_1);
   BUTTON_1.addEventListener(MouseEvent.MOUSE_OVER,MOUSE_OVER_1);
   function MOUSE_OVER_1():void
   {
    removeChild(BUTTON_1);
    BUTTON_1.graphics.clear();
    BUTTON_1.graphics.beginFill(0x000000);
    BUTTON_1.graphics.drawRect(188,96,104,24);
    BUTTON_1.graphics.endFill();
    BUTTON_1.graphics.beginFill(0x0000A0);
    BUTTON_1.graphics.drawRect(190,98,100,20);
    BUTTON_1.graphics.endFill();
    addChild(BUTTON_1);
   }
}

我对as3很新,所以如果有更好的方法,请告诉我。

2 个答案:

答案 0 :(得分:0)

此代码似乎不正确。鼠标事件的监听器将MouseEvent对象作为参数。所以你的MOUSE_OVER_1应该像function MOUSE_OVER_1(e:MouseEvent):void

答案 1 :(得分:0)

我最终使用了hitTestPoint,如下所示:

        if (BUTTON_1.hitTestPoint(mouseX,mouseY,true)) 
        {   

            removeChild(BUTTON_1);
            removeChild(TEXT_MENU_1);
            BUTTON_1.graphics.clear();
            BUTTON_1.graphics.beginFill(0x000000);
            BUTTON_1.graphics.drawRect(188,96,104,24);
            BUTTON_1.graphics.endFill();
            BUTTON_1.graphics.beginFill(0x0000A0);
            BUTTON_1.graphics.drawRect(190,98,100,20);
            BUTTON_1.graphics.endFill();
            addChild(BUTTON_1);
            addChild(TEXT_MENU_1);
        }
        else
        {
            removeChild(BUTTON_1);
            removeChild(TEXT_MENU_1);
            BUTTON_1.graphics.clear();
            BUTTON_1.graphics.beginFill(0x000000);
            BUTTON_1.graphics.drawRect(188,96,104,24);
            BUTTON_1.graphics.endFill();
            BUTTON_1.graphics.beginFill(0x0000DC);
            BUTTON_1.graphics.drawRect(190,98,100,20);
            BUTTON_1.graphics.endFill();
            addChild(BUTTON_1);
            addChild(TEXT_MENU_1);
        }