views:

254

answers:

3

Observed behavior:

  1. I have a page of UI sliding onto the screen.
  2. On slide complete, activate buttons.
  3. If mouse already over a button, the rollover does not happen (since MOUSE_OVER hasn't technically occurred)

Desired behavior: 1, 2 the same, but on 3, I see my rollover.

Is there any way to easily do this, aside from something brute-force, like tracking the mouse and comparing its position against all buttons dims?

Thanks!

A: 

You can dispatch a mouseMove event to your top most container with x and y coordinates of the current mouse position. This will emulate the effect of the user having moved his mouse.

private function moveComplete():void
{
    topLevelContainer.dispatchEvent(new MouseEvent(MouseEvent.MOUSEMOUSE, true, false, topLevelContainer.mouseX, topLevelContainer.mouseY);
}
CookieOfFortune
I don't see how that helps me, though. If it is a mouseMove, but they've never left the interactive object, what will that get me? I'll still have to compare the x + y to the constraints of all the buttons, right?
Why? The topLevelContainer recieves a MOUSE_MOVE event, then it will propogate that down to it's children, which are the UI buttons you moved around. This will trigger a MOUSE_OVER event on the respective button and it'll do it's rollover effect.
CookieOfFortune
hmmm... sorry, cookie, I tried it but no dice. Unsure if I have implemented it correctly. I still don't see how this will trigger a MOUSE_OVER, considering the mouse is already over the desired button.Or do you mean add an additional listener on the sliding buttons to listen for the topLevel MOUSE_MOVE?
I guess a workaround would be to listen to the MOUSE_MOVE and then trigger the MOUSE_OVER, let me think about a more elegant solution than that though.
CookieOfFortune
The event would note get down to the children. It bubbles up, not down.
Lillemanden
I've been thinking about this and came up w/ this solution:* on button activate, if mouse on button, do roll overI mean, this is kinda the brute force option, but since I'm targeting that speficic group of buttons, it won't be too much of a load.
A: 

on button init i set the private var _bounds:

_bounds = getBounds(this);

on activate, I call:

if (isMouseOver()) doOver(true);

and then the function:

private function isMouseOver():Boolean {
    //trace ("isMouseOver:");
    var xBool:Boolean = _bounds.left < mouseX && mouseX < _bounds.right;    
    var yBool:Boolean = _bounds.top < mouseY && mouseY < _bounds.bottom;
    //trace (" - xBool: " + xBool);
    //trace (" - yBool: " + yBool);
    return xBool && yBool;
}
A: 

You could set a default parameter for your mouse over handler, so you wouldn't need to create and dispatch a new event to run the same code.

e.g.

myBtn.addEventListener(MouseEvent.MOUSE_OVER, btnOverHandler);

function btnOverHandler(e:MouseEvent = null):void{
trace('do stuff on roll over!');
}

//then you can do this wherever you need
btnOverHandler();

it should work in both situation( MouseEvent, or not ).

and for the mouse over thing, getObjectsUnderPoint can be handy. It's more than what you need for this particular example(, and bit more lengthy) , but it's something good to be aware it exists.

e.g.

function isMouseOver(target:DisplayObject,container:DisplayObjectContainer):Boolean{
   var isOver:Boolean = false;
   var pt:Point = new Point(mouseX, mouseY);
   var objects:Array = container.getObjectsUnderPoint(pt);
   for(var i:int = 0 ; i < objects.length; i++){
      if(objects[i] == target) {
         isOver = true;
         break;
      }
   }
   return isOver;
}

Hope this helps.

George Profenza