Posts Tagged ‘swf’

External Interface error: Object expected

Thursday, February 23rd, 2012

It’s taken me a while to figure this one out. I have a flash game on facebook that uses Stage3D, so window mode has to be set to “direct” which means that any time facebook wants to overlay some elements on the page they appear under the game on many browsers.

Facebook has a solution for you, on FB.init you can set your own callback function via the hideFlashCallback parameter. In this callback you can move the flash object out of view by, for example, setting the top to be -10000px whenever a Facebook dialog is opened and then putting it back when the dialog is closed.

I had functions registered with external interface to pause and resume the game that would get called via javascript in the hideFlashCallback callback.

This was working well enough but for some reason on Internet Explorer I couldn’t call the resume() function just after showing the flash object, instead I got an “Object Expected” error.

Internet Explorer does not support calling external interface methods for flash objects if they are not visible. Since it could be a race condition (DOM might not render the flash object before the function was called) I added a 100ms delay to the call to resume(), only for IE. This seemed to work fine.

But once we got close to production this error re-appeared, but only when Facebook’s purchase dialog had been shown. Weird! No amount of delay made the external interface calls work again. If I showed the purchase dialog once and closed it, it would be no longer possible to call any external interface functions for the flash object until a page refresh. This was not acceptable.

After a lot of debugging, I noticed that even though we have registered for the hideFlashCallback function, facebook STILL sets the visibility of the flash object to “hidden” after 200ms.

As can be seen in Facebook’s documentation: “The custom action must complete within 200ms or the Flash object will be hidden automatically regardless.”

Apparently the fact that we’re changing the visibility of the flash object along with whatever facebook does differently during the purchase dialog (overlay a white div over the whole page) breaks all external interface functionality.

This has a simple fix though, create a higher priority CSS rule that makes the flash object always visible:

It doesn’t need to be hidden since we are moving it out of view ourselves anyways.

Preloader without a loader-swf in AS3

Wednesday, November 17th, 2010

Here’s one method of loading your AS3 movie, using a preloader class that loads before all other classes.

The flex compiler can actually split up the code for you so one class loads before all others. Even though we’re using the flex compiler to do this we do not have to have a Flex project, it can be a Flash project or a pure actionscript project. If we’re using a Flash project we do have to set the flex compiler path though.

Anyways, the way it works is that in your main class you add the Frame metadata, like this:

package com.flassari {
// Preloader meta tag
[Frame(factoryClass="com.flassari.Preloader")]
// Start of our own application code
[SWF(backgroundColor="#FFFFFF", frameRate="24", width="800", height="600", pageTitle="My preloaded project")]
public class Main extends Sprite
{

The preloader has to extend movieClip, because behind the scenes it is using frames for its magic.
In your preloader class, when it is done loading (by checking if it is at the last frame), your have to instantiate the main class with no strict typing. If you were to use strict typing, the whole application would have to load before the preloader can be shown so that would defeat the purpose of a preloader:

private function onEnterFrame(e:Event):void {
	if (currentFrame == totalFrames) { // If we're at the frame where Main is ready
		// Stop and clean up
		stop();
		removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		// Create the main application and add it to the display list
		var mainClass:Class = getDefinitionByName("com.flassari.Main") as Class;
		addChild(new mainClass() as DisplayObject);
	}
}

Just remember, everything that the preloader references will be loaded before the preloader can be shown, so keep it to a minimum.
Also, because the main class is created before it is put on the stage, be sure not to reference the stage in the constructor.

Flash focus border in Firefox

Monday, March 22nd, 2010

I’ve noticed recently that Firefox has started showing a 1px dotted border around flash objects when I click on them. It didn’t bother me enough to look for a solution, but today the solution found me.
In my rss reader a blog post from FlashComGuru pops up, showing how to get rid of this annoyance, pasted here for your convenience:

Simply add this to your page’s stylesheet:

a:focus, object:focus { outline: none; -moz-outline-style: none; }