Actionscript 3 F.A.Q.
From GatchaWiki
Contents |
I'm dispatching GameEvents but the wrapper doesn't do anything!?
Because the GameEvents are actual Flash events, they need to bubble up to the wrapper for them to be heard. Check the following:
- Are you waiting for the Event.ADDED_TO_STAGE on the root displayobject of your game before dispatching GameEvents? The game needs to be added to the wrapper's displaylist before it can listen for those.
- Make sure you dispatch your event on a displayobject that is added to the game's stage, otherwise events won't bubble up.
- Make sure the bubbles property of your GameEvent is set to true.
Example
import flash.display.MovieClip; import flash.events.Event; public class Game extends MovieClip { public function Game() { addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler); } private function addedToStageHandler(event:Event):void { // cleanup listeners removeEventListener(Event.ADDED_TO_STAGE,addedToStageHandler); // the game has been added to the displaylist, so GameEvents can bubble up! dispatchEvent(new GameEvent(GameEvent.SOMETHING)); } }
Can I develop & test my game without the wrapper?
Sure you can. You can write code that does one thing when it's run locally and another when it's loaded inside the wrapper. For example, you don't want to dispatch and listen to GameEvents when your game is not loaded inside the wrapper. You just have to check if the root of the current swf has been added to another Stage. Here is a function that does that:
Example
/** * Checks if a specified displayobject has been loaded inside another swf and returns true or false. */ public static function isLoadedInSWF(displayObject:DisplayObject):Boolean { if(displayObject.root) { if(getQualifiedClassName(displayObject.root.parent) == "flash.display::Stage") { return false; } else { return true; } } else { throw new Error("displayObject :: " + displayObject + " has not been added to the stage yet."); } } // usage isLoadedInSWF(this.root)
Why is external data not loaded (Error #2044: Unhandled SecurityErrorEvent)?
The reason is because the domain from which your game is loaded does not allow external access through the crossdomain.xml. Explanation can be found on the Policy file whitepaper. Even if you are loading the game from a localhost, you still have to have a crossdomain.xml to specify access.
You must create a crossdomain.xml file in the root of the loading domain ( or read the whitepaper for other setups on this matter ). A crossdomain.xml containing the following will do just fine.
<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy> <site-control permitted-cross-domain-policies="master-only" /> <allow-access-from domain="*.netlog.com" /> <allow-access-from domain="*.netlogstatic.com" /> </cross-domain-policy>
I'm loading an external asset (xml, swf, mp3, ...) into my game at runtime, but i get an IO error.
Never use relative url's to load external assets. This is because these url's are relative to the html container that the swf is loaded in, and not the game swf itself. You can determine the absolute url of your game location by using the loaderUrl property of the LoaderInfo object. Concatenate this absolute url to the relative paths to load your files.
// Get the swf url from the loaderinfo object. Looks something like this http://www.gatcha.com/static/f/games/partner/yourgame/game.swf[[DYNAMIC]]/1 var fullUrl:String = this.root.loaderInfo.loaderURL; // remove garbage for clean game url if contains [[DYNAMIC]] (added to url by wrapper load process) var gameUrl:String; (fullUrl.indexOf("/[[DYNAMIC]]") >= 0) ? gameUrl = fullUrl.substring(0, fullUrl.indexOf("/[[DYNAMIC]]")) : gameUrl = fullUrl; var pathUrl:String = gameUrl.substring(0, gameUrl.lastIndexOf("/") + 1); // +1 for trailing slash // concatenate pathUrl to the relative url's of your game. For example pathUrl + "xml/settings.xml";
