Archive for February, 2011

RollOver is different in AS3 if cursor is already over MC

February 6th, 2011

So, in the process of converting a banner from AS2 to AS3, I noticed some odd behavior with some rollovers. Here is the original As2 banner for reference:

[kml_flashembed publishmethod=”dynamic” fversion=”8.0.0″ replaceId=”as2_banner” movie=”http://www.jeremy-knight.com/wp-content/uploads/veritage_realty_300x250.swf” width=”300″ height=”250″ targetclass=”flashmovie”]Get Adobe Flash player

[/kml_flashembed]

After the initial animation, two buttons show up that allow you to roll over to switch to different images.

When I converted this to AS3, though, the rollover kept on retriggering itself. Basically, in AS2, the roll over event is only triggered when you roll from something else on to the target object. If your mouse is already over the object and moves, it won’t trigger the rollover.

In AS3, that is not the case. Once the button reappeared underneath the mouse, and the cursor moved just a tiny bit, the rollover event would fire. Irritating, but I found a work around.

Instead of re-enabling the button immediately after the animation completes, I instead check to see if the cursor is over the button first. If so, I add an event listener for the ROLL_OUT event, which then runs a function that adds the listener for the ROLL_OVER event. See code sample below:

function enableIt() {
// This function is run after animation completes.

btn.mouseEnabled = true;
if(btn.hitTestPoint(mouseX, mouseY)) {
// Is the cursor over the button?
// If so, listener for the ROLL_OUT first, before listening for the ROLL_OVER.
addEventListener(MouseEvent.ROLL_OUT, addRollOverListener);
} else {
// Mouse is not over the button, just enable the ROLL_OVER listener right away.
addRollOverListener();
}
}
function addRollOverListener(e:MouseEvent = null) {
btn.addEventListener(MouseEvent.ROLL_OVER, doRollOver);
}

function doRollOver(e:MouseEvent) {
trace(“button has been rolled over”);
}

Custom Events cannot be named “CLICK”

February 2nd, 2011

I encountered this error while trying to create a custom event for a check box UI control:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::MouseEvent@5658bdf1 to com.cannedbanners.ui.SizePanelListItemEvent.

I had created a custom event class and named one of my events “CLICK”, because that’s what the event was. For some reason, it throws the above error because it thinks your trying to force a mouse event into a custom event. I renamed it to “CLICKED” and everything was happy again.