views:

36

answers:

3

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();
    addChild(BUTTON_1);
   }
}

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

A: 

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.

bhups
Actually that's not a problem. Since `MOUSE_OVER_1` is a nested function, it won't have its parameters checked. Nested functions are treated identically to top-defined local vars of type `Function`.
Gunslinger47
A: 

for newbie

Eugene
A: 

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