Archive for August, 2012

Switch-case for object’s class type

Friday, August 31st, 2012

Quick tip; instead of using a bunch of “if (x is y) else if (x is z) else” conditionals, you can simply get the class of any object using the “constructor” property. It’s not exposed trough strict typing though, so you have to typecast your instance to Object to access it.

This allows you to have a switch-case for an object’s class:

function addShape(shape:Shape):void {
	switch (Object(shape).constructor) {
		case Circle:
			trace("It's a circle");
			break;
		case Square:
			trace("It's a square");
			break;
		default:
			trace("Unknown shape");
	}
}

Mouse scroll wheel in flash not working in Chrome

Tuesday, August 14th, 2012

Chrome’s latest update to version 21 introduced a new type of flash player using the PPAPI (pepper) plugin architecture.
I noticed that this version of flash started breaking mouse scroll wheel support for some flash content.

Which flash files break and which don’t depend on one thing; if you are using JavaScript to prevent the default behavior of the mouse scroll wheel or not.

The reason for preventing default mouse scroll wheel behavior is for when you don’t want the page to scroll when you scroll over the flash content.
To prevent that, developers write JavaScript code (or use libraries like SwfWheel.js) that add an event listener for the “mousewheel” or “DOMMouseScroll” event, and in the handler call “evt.preventDefault()” and/or “evt.returnValue = false” to cancel the scroll.

Therein lies the problem, the old NPAPI Flash Player would receive the mouse scroll event even if we prevented the default behavior. Maybe even incorrectly so, since the event was cancelled.
The new PPAPI Flash Player will not receive the event if the default is prevented.

If one were to remove the preventDefault() function, the Flash file would start working correctly again, but the whole page would again start scrolling, which is out of the question.

You will have to not only prevent the default scroll behavior, but also call an external interface function in your flash file to let it know when the user scrolls the mouse wheel.
In my case I was using SwfWheel and only needed a small modification to the library file: in the SWFWheel.getState() function add this code before the “if (br.mac)” line:

// The new pepper plugin requires chrome to always be hacked.
if (br.chrome)
{
	return STATE_HACKED;
}