开发者

as3: MOUSE_OVER is slow/not working correctly

Okay so I'm working in pure as3 (no creative suite or xml). I have a sprite. It draws a large rectangle and then a slightly smaller rectangle. I want to change the color of the slightly smaller rectangle when I hover the mouse over it. Right now though it will either not respond (I can plainly have the mouse over the rectangle and nothing happens) or it is slow to respond. In addition the collision area of the rectangle seems a bit off, ie it responds more frequently when I have the mouse on the upper left corner of the rectangle than when I have the mouse elsewhere on it.

Anyways here's the code I'm using:

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();
    ad开发者_如何学JAVAdChild(BUTTON_1);
   }
}

I'm pretty new to as3 so if there's a better way to do this tell me.


this code doesn't seem correct. listener for mouse events takes MouseEvent object as parameter. So your MOUSE_OVER_1 should be like function MOUSE_OVER_1(e:MouseEvent):void.


I ended up using hitTestPoint, like this:

        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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜