Actionscript dispatch event fails and solution.

The Problem:

A event too close to the instantiation of an object is not being fired, I believe the speed of the code execution is too fast for the listener to be able to register for the event.

Lets take a look at the following class to understand the issue.

[as]package application.views
{

import flash.display.Sprite;
import flash.events.Event;

public class TestView extends Sprite
{

public static const NAME : String = “TestView”;
public static const THE_EVENT : String = NAME + “The_Event”;

private var holder : Sprite;

public function TestView ( )
{

init();

}

private function init () : void
{
holder = new Sprite;
holder.graphics.beginFill(0x000000)
holder.graphics.drawRoundRect(0, 0, 100, 100, 0);
holder.graphics.endFill();
addChild(holder);

dispatchEvent ( new Event ( THE_EVENT))
}

}

}[/as]

In the example above what we are doing is dispatching an event after the creation of a simple object. If you notice the call to the init function is right after the construction of this specific object, in this case the object that is listening for that specific event is never able to “hear” the event, although it has been fired.

Here is the construction of the TestView and the listening for the event:

[as]private var testView : TestView;
testView = new TestView;
testView.addEventListener ( TestView.THE_EVENT, theEventHandler ) ;

private function theEventHandler ( event : Event ) : void
{
trace(“Event Fired”);
}[/as]

The Solution

The solution is very simple, add a delay as low as 1/1000 before the code execution.

[as]package application.views
{

import flash.display.Sprite;
import flash.events.Event;
import flash.utils.setTimeout;

public class TestView extends Sprite
{

public static const NAME : String = “TestView”;
public static const THE_EVENT : String = NAME + “The_Event”;

private var holder : Sprite;

public function TestView ( )
{

//delay the execution of init by 1/1000
setTimeout(init, 1);
//init();

}

private function init () : void
{
holder = new Sprite;
holder.graphics.beginFill(0x000000)
holder.graphics.drawRoundRect(0, 0, 100, 100, 0);
holder.graphics.endFill();
addChild(holder);

dispatchEvent ( new Event ( THE_EVENT))
}

}

}[/as]

Conclusion

There are ways to go around this issue, that is for certain but being able to find where your problem is located at is important to provide the right solution at the right time. I am going to do some more research in regards to why this specific problem happens at a lower level but for now it allows me to trust that the listener is going to be able to “listen” for a specific event.

Am I happy with this solution? NO. but for this specific application I am working on I need to make sure all the elements are in place before continuing the chain of events rather than just trusting that items are drawn or attached on the display list.

SVN PROPFIND 405 Method Not Allowed

Problem

I was getting the following error after trying to update a SVN repository.

[php]PROPFIND of ‘/’: 405 Method Not Allowed[/php]

And had no idea what it meant. I read on the internet that could be from trying to checkout a file that is none existent. The interesting thing is that I was just doing an update (svn update).

Solution:

[php]svn cleanup[/php]

That solved my problem immediately. After running the cleanup command I was able to make my update.

Learning PureMVC from HydraMVC

Intro

This post is not specifically on how to learn everything about PureMVC with HydraMVC but mainly points out their notification workflow document that they have on their site and some other statements they make on their site.

Keep in mind that HydraMVC is a complete rewrite from PureMVC, made specifically for the Flex framework.

HydraMVC Notification Workflow

Beautiful (technically speaking) document that allows you to follow their notification system from beginning to end (tip: start from the view component and follow along).

Screenshot:

Direct link to original PDF file in HydraMVC website (full resolution).

Briefly about HydraMVC

If you are using PureMVC keep in mind the following.

PureMVC intends to be language-agnostic where HydraMVC is a compromise by design

Meaning that the team at HydraMVC took the time to make the framework beautiful to fit their needs within Flex framework while PureMVC has kept it open so that you can extend to other languages such as PHP, Javascript, Objective C, Ruby and more. This gives PureMVC the room to breath without having to be tied up to a specific language.

But in the end that doesn’t mean that we can’t learn from HydraMVC if you are working on PureMVC, specially since Hydra is based (and complete rewrite) from PureMVC.

Hydra Events and Implementation

In addition to using Events vs. an independent Observer pattern, HydraMVC also streamlines implementation, encapsulating much of the initialization code that needed to be written when implementing PureMVC.

I am familiar with Flex and have written couple test applications with the framework but I haven’t used HydraMVC with Flex but I am curios on their initialization code which for the sound of their statements you have to write less code while still keeping the same functionality.

Here are some useful links:
PureMVC
HydraMVC

Send multiple parameters to event handlers

At one point or another during the time you write applications with ActionScript 3 you will have to send parameters from to an event handler. By default you can’t send information unless you use the DataEvent, but that only allows you to send a string. That is OK if that is all you need to send but how about multiple parameters? How about an object or references?

In the example below we create a square on the stage that then will dispatch a custom event with 3 parameters (two strings and one number).

[as]package {

import flash.display.Sprite;
import event.CustomEvent;
import flash.events.Event;
import flash.events.MouseEvent;

public class App extends Sprite
{

var square : Sprite ;

public function App()
{
init ( ) ;
}

private function init ( ) : void
{

//add the event listener to the Document class.

this.addEventListener(
CustomEvent.MOUSE_CLICK,
customEventHandler ) ;

// Draw a square on stage and add a mouseEvent
// that then will dispatch a custom event

square = drawSquare();
square.buttonMode = true;
square.addEventListener (
MouseEvent.CLICK,dispatchCustomEvent ) ;

addChild(square);

}

private function dispatchCustomEvent ( e : Event ) : void
{
dispatchEvent ( new CustomEvent (
CustomEvent.MOUSE_CLICK ,
“myParam1”,
“myParam2”,
3 ) ) ;
}

private function customEventHandler ( e :CustomEvent )
{
trace( “Param 1: ” + e.param1 ) ;
trace( “Param 1: ” + e.param2 ) ;
trace( “Param 1: ” + e.param3 ) ;
}

private function drawSquare ( ) : Sprite
{

var square:Sprite = new Sprite();

square.graphics.beginFill(0x000000);
square.graphics.drawRect(100, 100, 100, 100);
square.graphics.endFill();
return square as Sprite;
}

}

}[/as]

Custom Class

[as]package event
{

import flash.events.Event;

public class CustomEvent extends Event
{

public static const MOUSE_CLICK : String = “mouseClick”;

public var param1 : String ;
public var param2 : String ;
public var param3 : int ;

public function CustomEvent(
type : String,
param1 : String,
param2 : String,
param3 : int,
bubbles:Boolean = false,
cancelable:Boolean = false )
{

super ( type, bubbles, cancelable ) ;

this.param1 = param1;
this.param2 = param2;
this.param3 = param3;

}

override public function clone( ): Event
{
return new CustomEvent( type, param1, param2, param3,
bubbles, cancelable );
}
}
}[/as]