views:

34

answers:

2

Is there a way to do it in Flex to say:

if mouseClick x<300&y<200 currentState='';

Thanks,

+1  A: 

Many objects dispatch a click event; and in that click event properties you can access the x and y position using stageX and stageY properties.

http://livedocs.adobe.com/flex/3/langref/flash/events/MouseEvent.html

However, I do not think it is possible to listen for a click event at a specific location without their being a UI Element at that spot.

I also question whether hard coding the x and y position for such a state change is a good idea; as different machines and different screen sizes and resolutions may size your content differently.

www.Flextras.com
A: 

You can add a listener to the stage to capture all clicks:

package
{

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

[SWF(width='500', height='300', backgroundColor='#ffffff', frameRate='30')]
public class ClickTest extends Sprite
{
    public function ClickTest()
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    }

    private function addedToStage(event:Event):void
    {
        stage.addEventListener(MouseEvent.CLICK, handleClick);
    }

    private function handleClick(event:MouseEvent):void
    {
        if((stage.mouseX < 300) && (stage.mouseY < 200)
        {
            trace("CLICKED WHERE I WANT");
        }
    }
}

}

This seems to work even when Sprites are placed on top of the interface.

James Fassett