AS3 UrlLoader does not fire Event.COMPLETE - actionscript-3

Everything work, except the Event.Complete... (that means that I retrieve the good content at the server side, and the progress event work as expected)
I have a simple code that process an upload. I use this class : https://github.com/Nek-/Multipart.as/blob/master/src/com/jonas/net/Multipart.as
And my code:
package com.foo.http
{
import com.jonas.net.Multipart;
import flash.net.*;
import flash.events.*;
import flash.utils.ByteArray;
import flash.external.ExternalInterface;
public class RequestManager
{
private var request:Multipart;
private var loader:URLLoader;
/**
* The url can be http://foobar:952/helloworld
* #param url
*/
public function RequestManager(url:String)
{
this.loader = new URLLoader();
this.request = new Multipart(url);
// This is needed, if we don't set it, the complete event will never be trigger
// We retrieve some text
this.loader.dataFormat = URLLoaderDataFormat.TEXT;
// Events
this.attachEvents();
}
public function getRequest():Multipart
{
return this.request;
}
public function send():void
{
this.loader.load(this.request.request);
}
private function attachEvents():void
{
this.loader.addEventListener(Event.COMPLETE, this.requestCompleted);
this.loader.addEventListener(ProgressEvent.PROGRESS, this.requestProgress);
this.loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityError);
this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.networkError);
}
// Of course there is also listener methods (with a trace inside and call to JS, nothing more)
}
}
Any idea what it comes from ?

It looks like my problem was not from actionscript or flash itself but from my javascript and the flash debugger on firefox.

Related

Custom events not working in AS3

I have an event called SelectEvent. Whenever I click the buyer, the select event should be launched. But that is not what is happening.
My code of base:
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
public class FashionFrenzy extends MovieClip
{
public var Buyer_mc:Buyer;
public var Buyers:Array;
private var BuyerNumber:Number;
public var xpositions:Array;
public var ypositions:Array;
public var SelectedBuyer:Number;
public function FashionFrenzy()
{
GameTimeController();
xpositions=new Array();
xpositions.push(523,563,603);
ypositions=new Array();
ypositions.push(377,377,377);
Buyers = new Array ;
BuyerNumber=0;
Time_mc.gameTimer.addEventListener(TimerEvent.TIMER,GenerateBuyers);
addEventListener(SelectEvent.BUYERSELECT,showselectbuyer);
}
public function GameTimeController()
{
Time_mc.StartGame();
}
public function showselectbuyer(event:SelectEvent):void
{
trace("Bamba");
}
public function GenerateBuyers(event:TimerEvent):void
{
if(BuyerNumber<6)
{
if (Math.random() < 0.01)
{
var position:Number = Math.floor(Math.random()*3);
var newBuyer_mc = new Buyer(xpositions[position],ypositions[position],BuyerNumber);
ypositions[position]-=40;
Buyers.push(newBuyer_mc);
addChild(newBuyer_mc);
BuyerNumber++;
}
}
for each (var buyer_mc:Buyer in Buyers)
{
if(buyer_mc.walking==true)
{
buyer_mc.Enter();
}
}
}
}
}
My code of the buyer:
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
public class Buyer extends MovieClip
{
public var buyerTimer:Timer;
public var walking:Boolean;
public var stopposition:Number;
public var buyerCode:Number;
public function Buyer(startx:Number, stopy:Number, code:Number)
{
x=startx;
stopposition=stopy;
walking=true;
buyerCode=code;
BuyerProperties();
}
public function Enter():void
{
if(y>stopposition)
{
walking=false;
StartFunction();
}
else
{
y= y+3;
}
}
public function BuyerProperties():void
{
buyerTimer = new Timer( 25 );
trace(" I am generated");
}
public function StartFunction():void
{
buyerTimer.start();
addEventListener(MouseEvent.CLICK,Select);
trace("My timer starts now within Buyer.as");
}
public function Select(event:MouseEvent):void
{
trace(buyerCode);
dispatchEvent( new SelectEvent(SelectEvent.BUYERSELECT));
}
}
}
Now that when I'm clicking the buyer, the MouseEvent.CLICK is activated and then in the buyer.as, buyercode is traced on the screen. But the event is not dispatched or else it is dispatched but the eventlistener is not executing the code. I'm not getting any runtime errors. the function "showselectbuyer" is never even launched.
How should I solve it?
The accepted answer is incorrect. The provided solution works but for the wrong reasons. It creates a direct dependency to the object supposed to listen to the custom event and then makes that object be the dispatcher. All together this makes the whole idea of using event/custom event unnecessary at best since in that case a simple callback would work just as well. Instead using the useCapture flag would make the whole system work as expected:
addEventListener(SelectEvent.BUYERSELECT,showselectbuyer, true);
The accepted solution is also the inverse way of dealing with non DisplayObject event propagation. The dispatcher should be the listener (no dependencies) and not the listener should be the dispatcher (dependency necessary).
The trouble with custom events is that they don't bubble up the display list, so in case your event to be registered you need to dispatch it to the class instance that has a listener to that event attached. In your case it's an instance of FashionFrenzy. Apparently buyers don't know about a fashion frenzy instance they are running in, so they dispatch the event to themselves and wonder why no one else listens. In order to resolve this, either attach the listener to the buyer, or in the buyer class dispatch the event to parent, which is apparently the instance you want to receive the event.

AS3: Display data(integer) from an swf to another swf through class

I have a "score1.as" class in which it will access a data from an swf and display it into
my "finalscore.fla"...I was able to pass and trace the data successfully into my "finalscore.fla"..But my problem is this: Though I was able to access the data by tracing it, I can't display it to my dynamic text...I thought by simply typing "txtScore.text = ("Score: " + lol1.go() );" would solve the problem but it didn't...Please help..Here's my code..bY the way, I'm using actionscript 3.0..
score1.as:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class score1 extends Sprite
{
private var loader:Loader;
public function Parent()
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("savescore.swf"));//This is the swf where in the data came from
}
public function onLoaded(e:Event):void
{
trace(loader.content['currentScore']);
}
public function go():int{
return loader.content['currentScore'];//This is the data being accessed
}
}
}
finalscore.fla:
var lol1:score1 = new score1();
txtScore.text = ("Score: " + lol1.go() ); // This is where I can't display the data
lol1.Parent();//I successfully traced the data
Check your embedded fonts. You are likely only embedding the characters from the font that were present at compile time. For example, if you have a textfield with "Hello World!", and you wanted to do textfield.text = "Jiggly Jello Lord!" would display as "l ello ord", because only those characters were present in the textfield during compile.
Edit:
I took a closer look at your code. You need to call Parent() before you can get your content. In fact, rename it to be the constructor, and you should be good.
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class score1 extends Sprite {
public var loader:Loader;
public function score1() {
// You need to run this code as your constructor.
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
loader.load(new URLRequest("savescore.swf"));
}
public function onLoaded(e:Event):void {
trace(loader.content['currentScore']);
}
public function go():int {
return loader.content['currentScore'];
}
}
}
You'd also want to wait for your content to be loaded before accessing the data. For this, you'd need to wait for your onLoaded to fire before trying txtScore.text = ("Score: " + lol1.go() );
Edit 2
onLoaded is how you know if it runs. It gets called when Event.COMPLETE fires. By the same extension, just register for that event from outside your class, or fire an event, or any other solution that completes after the data is loaded.
finalscore.fla:
var lol1:score1 = new score1();
lol1.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, scoreReady);
function scoreReady(e:Event):void {
txtScore.text = "Score:" + lol1.go();
}

Loading flex Modules from an XML file

I am trying to parse an xml file and load Flex modules dynamically in my application. But it loads only the last module everytime. I have a singleton class which does the parsing and loading of modules. Here is the class
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleLoader;
import mx.modules.ModuleManager;
public class VappModuleManager
{
private static var _instance:VappModuleManager;
private static const MODULE_PATH:String="./com/emc/vapp/";
private static const MANIFEST_PATH:String="module-manifest.xml";
private var _module:IModuleInfo;
private var _handler:Function;
private var loader:URLLoader;
public function VappModuleManager(object:SingletonEnforcer)
{
}
public function set handler(handler:Function):void
{
_handler=handler;
}
public static function get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new VappModuleManager(new SingletonEnforcer());
}
return _instance;
}
public function load():void
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
loader.load(new URLRequest(MANIFEST_PATH));
}
private function xmlLoaded(event:Event):void
{
Alert.show("Event Completed");
var manifest:XML=new XML(event.target.data);
Alert.show(manifest.module.length());
for (var index:int=0;index<manifest.module.length();index++)
{
Alert.show(MODULE_PATH+manifest.module[index].#name);
_module=ModuleManager.getModule(MODULE_PATH+manifest.module[index].#name);
_module.addEventListener(ModuleEvent.READY,_handler);
_module.load();
}
}
}
}
internal class SingletonEnforcer {}
I use the above class as follows.
moduleManager=VappModuleManager.instance;
moduleManager.handler=myhandler;
moduleManager.load();
I understand the problem is with eventlistener for variable "_module" but dont know how to solve it. Any help appreciated.
The call to IModuleInfo.load is asynchronous so your for loop has run completely before any of the modules have loaded. Also, your class level _module property is overwritten by a new Module instance each time the loop iterates.
I'd suggest loading each module sequentially by waiting for the READY event and initiating the load of the next module only when it has fired. I'd also fire an event when all the modules are loaded instead of executing a callback function as this will give you more flexibility (multiple objects can listen for an event for example).
The following isn't tested, but should give you the idea:
package
{
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleLoader;
import mx.modules.ModuleManager;
public class VappModuleManager
{
private static var _instance:VappModuleManager;
private static const MODULE_PATH:String="./com/emc/vapp/";
private static const MANIFEST_PATH:String="module-manifest.xml";
private var loader:URLLoader;
private var manifest:XML;
private var loadCount:int = 0; // track loaded modules
public function VappModuleManager(object:SingletonEnforcer)
{
}
public static function get instance():VappModuleManager
{
if(_instance==null)
{
_instance=new VappModuleManager(new SingletonEnforcer());
}
return _instance;
}
public function load():void
{
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
loader.load(new URLRequest(MANIFEST_PATH));
}
private function xmlLoaded(event:Event):void
{
manifest =new XML(event.target.data);
// load the first module
loadModule();
}
private function loadModule() {
// Use a locally scoped variable to avoid writing over the previous instance each time.
// You could push these onto an array if you need to be able to access them later
var module:IModuleInfo = ModuleManager.getModule(MODULE_PATH+manifest.module[loadCount].#name);
module.addEventListener(ModuleEvent.READY, moduleReadyHandler);
module.load();
}
private function moduleReadyHandler(event:ModuleEvent) {
// Remove the event listener on the loaded module
IModuleInfo(event.target).removeEventListener(ModuleEvent.READY, moduleReadyHandler);
loadCount ++;
// Are there still modules in the manifest to load?
if (loadCount < manifest.module.length()) {
// Yes... load the next module
loadModule();
} else {
// No... we're all finished so dispatch an event to let subscribers know
dispatchEvent(new Event("complete"));
}
}
}
}

Passing a variable between classes

I am trying to pass a variable "budget" from my DocumentClass of a flash file, to a class.
Currently I have :
(DocumentClassv5 , this is the code thats attached to the flash file in the properties panel, some code ommited)
package
{
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.text.TextFieldType;
import flash.media.Sound;
import flash.media.SoundChannel;
import miniGameOne;
import floorTileMC;
import flash.display.Loader;
import flash.net.URLRequest;
public class DocumentClassv5 extends MovieClip
{
/*#################################
## Defining Variables ##
#################################*/
public var budget:int = 0;
var gameOne:miniGameOne = new miniGameOne();
/*#################################
## Constructor ##
#################################*/
public function DocumentClassv5()
{
/*#################################
## Adding Event Listeners ##
#################################*/
trace("Document class loaded");
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*###################################################
## Begins the mini game ##
###################################################*/
public function begin(evt: MouseEvent)
{
beginGame.visible = false;
beginGame.removeEventListener(MouseEvent.CLICK, begin);
budget = 500;
cleanListeners();
gameOne.loadGame();
trace(gameOne.testVar);
trace(floorTile.testVar2);
/*#################################
## Adding Event Listeners ##
#################################*/
trace("Game started");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
Then I have the miniGameOne class file, which at the moment does nothing
I also have another class file, called tileFloorMC. This is attached to a symbol.
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import DocumentClassv5;
public class floorTileMC extends MovieClip
{
var propertyA:Number;
//var hackerClass:DocumentClassv5 = new DocumentClassv5;
public var testVar2:int = 50;
public function floorTileMC()
{
this.propertyA = randomRange(100, 500);
this.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver, false, 0, true);
this.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut, false, 0, true);
}
private function manageMouseOver(evt: MouseEvent)
{
this.gotoAndStop(2);
//trace(mainClass.budget);
}
private function manageMouseOut(evt: MouseEvent)
{
this.gotoAndStop(1);
//mainClass.budget += 1;
}
private function randomRange(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}
}
}
Now, essentially I will need to be able to pass budget from DocumentClassv5 TO floorTileMC, and then BACK to DocumentClassv5. At the moment, I can pass anything from floorTileMC and anything from miniGameOne into DocumentClassv5, but when i try and pass from floorTileMC to DocumentClassv5, I get error
Error #2136: The SWF file file:///yadayada/GameV5.swf contains invalid data.
More specifically, as soon as I uncomment //var hackerClass:DocumentClassv5 = new DocumentClassv5;
Any help would be greatly appreciated!
Thanks,
Tiffany
You're trying to instantiate your document class in a subclass:
var hackerClass:DocumentClassv5 = new DocumentClassv5();
You want access to access the existing instance, not create a new one.
One thing you can do, is create a static reference to your document class. (see code sample below)
Static references can get ugly though, and you may just want to pass a reference of your doc class to your other classes when you instantiate them.
Both method below:
In your document class:
//instead of the line below:
var gameOne:miniGameOne = new miniGameOne(); //It's a bad idea to instantiate non primitive objects before the constructor of your document class runs.
//just declare it, and instantiate it in the constructor
var gameOne:miniGameOne;
//if you want to use a static reference:
public static var me:DocumentClassv5;
public function DocumentClassv5()
{
/*#################################
## Adding Event Listeners ##
#################################*/
//if using the static var me, set it's value to this (the instance of the document class):
me = this;
trace("Document class loaded");
gameOne = new miniGameOne(this); //pass a reference to the document class if NOT using the static var me
}
If using the static var me, you access it by doing the following from any class:
DocumentClassV5.me.budget;
Another cleaner alternative (if the values you need access to aren't really directly tied to any class, eg. global preferences), is make a whole new class that is just static (doesn't get instantiated) to hold your preferences.
package {
public class Global {
public static var budget:Number = 50;
}
}
Then you'd access budget by importing your Global class and doing Global.budget = 5

Flash AS3 Help detecting a loaded file

I'm trying to load an external and local XML file to read its data, but I don't really know how to detect when the file has been loaded, I'm just able to open the broser window,
here is what I have done
package {
import flash.display.Sprite;
import flash.events.;
import flash.net.;
import flash.net.URLRequest;
public class cargadorXML extends Sprite {
public var cuadro:Sprite = new Sprite();
public function cargadorXML() {
cuadro.graphics.beginFill(0xFF0000);
cuadro.graphics.drawRoundRect(0,0,100,100,10);
cuadro.graphics.endFill();
cuadro.addEventListener(MouseEvent.CLICK,init);
addChild(cuadro);
}
public function init(e:Event) {
var file:FileReference;
file = new FileReference();
file.browse();
file.addEventListener(Event.COMPLETE,bien);
}
public function bien(e:Event) {
trace("cargado");
}
}
}
but no "cargado" message appears, also I don't really think the Event.COMPLETE is the correct event at all xD
could some one help me here??
thanks
done, it's Event.SELECT instead of Event.COMPLETE