Flex Event Dispatcher Questions - apache-flex

I am trying to dispatch an custom YouTubeEvent from my Player.as and wish my Main.as would listen and create the video player...Apparently my eventHandler can't catch the event to create the videoplayer.......My flex debug mode is so screw up I can't even use it...My code is as follow..I really appreciate any reply or help.....
My custom event..
package com.youtube.events {
import flash.events.Event;
public class YouTubeEvent extends Event{
public static const PLAYER_READY:String="PLAYER_READY";
public function YouTubeEvent(type:String){
super(type);
}
}
}
My Main.as
public class SearchYoutube extends Sprite
{
private var videoPlayer:Player;
public function SearchYoutube()
{
/*********************Load Video Player****************************/
loadPlayer();
}
private function loadPlayer():void{
videoPlayer= new Player();
videoPlayer.addEventListener(YouTubeEvent.PLAYER_READY, playerReady);
//playReady would never be excuted....
}
private function playerReady(event:YouTubeEvent):void{
videoPlayer.createPlayer(); //This handler would never be executed...
addChild(videoPlayer); //This handler would never be executed...
}
}
Player.as
//only show part of codes here
public function Player(){
}
public function createPlayer():void{
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
}
private function onLoaderInit(event:Event):void {
_loader.content.addEventListener("onReady", onPlayerReady);
}
private function onPlayerReady(event:Event):void {
dispatchEvent(new YouTubeEvent(YouTubeEvent.PLAYER_READY));
}

YouTubeEvent.PLAYER_READY is dispatched some time after calling createPlayer(). You should call createPlayer() after videoPlayer.addEventListener(YouTubeEvent.PLAYER_READY, playerReady):
private function loadPlayer():void
{
videoPlayer= new Player();
videoPlayer.addEventListener(YouTubeEvent.PLAYER_READY, playerReady);
videoPlayer.createPlayer();
}

This short tutorial will get you on the right path on using custom events with Flex.

Related

Actionscript 3.0 -> How to override a protected method from UIComponent

I want to create a custom button in ActionScript.
This is my code:
import flash.events.MouseEvent;
import mx.controls.Alert;
import spark.components.Button;
public class BookViewButton extends Button
{
public function BookViewButton()
{
super();
}
override protected function mouseOver(event:MouseEvent,increase:int,newImageSource:String):void{
Alert.show("mouseOver for new class works");
}
}
But Adobe Flash Builder signals the method overriding line as an error.
I tried to change the new method signature so that it would match exactly, like this: (I thought the error might be because I was adding extra parameters to the mouseOver function so Flex might have not recognized it as an overriding)
override protected function mouseOver(event:MouseEvent):void{
Alert.show("mouseOver for new class works");
}
But still it doesn't work. Same little red exclamation mark comes up to signal a mistake.
I'd greatly appreciate any help. Thanks.
What SDK version do you use if you have mouseOver() method in Spark Button hierarchy?
Spark button has mouseEventHandler() method to override:
override protected function mouseEventHandler(event:Event):void
{
super.mouseEventHandler(event);
var mouseEvent:MouseEvent = event as MouseEvent;
if (event.type == MouseEvent.ROLL_OVER)
{
// My roll over logic here
}
}
And it is better to post the error text which IDE gives you. To copy it just open Problems view in Flash Builder, found your error message and copy it via context menu.
You can't use override here because the base class Button does not have a method mouseOver to override.
If you remove the override keyword it should work fine. That said, you'll need to actually add a listener for your function for it to be called when you mouse over. Like this:
import flash.events.MouseEvent;
import mx.controls.Alert;
import spark.components.Button;
public class BookViewButton extends Button
{
public function BookViewButton()
{
addEventListener(MouseEvent.MOUSE_OVER, _mouseOver);
}
private function _mouseOver(event:MouseEvent):void
{
Alert.show("mouseOver for new class works");
}
}
Info on super();
There's no need to call super in the constructor of an extending class, unless there are arguments that you want to parse. Example.
package
{
public class A extends Object
{
public function A()
{
trace("A");
}
}
}
And the extending class:
package
{
public class B extends A
{
public function B()
{
trace("B");
}
}
}
If we do:
var thing:B = new B();
You will have the output:
A
B
However, if you wanted arguments for your constructor, you would need to parse them up to the constructor of the class you're extending using super(). Like so:
public class A extends Object
{
public function A(arg:String)
{
trace(arg);
}
}
public class B extends A
{
public function B(arg:String)
{
super(arg);
}
}
Now this will output "asdf":
var thing:B = new B("asdf");

Flex 4 Custom Preloader not Working

I am following this help page to create a custom preloader extending Sprite to load an animation SWF, but it is not working (the animation SWF is not displaying):
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf 69084-7e3c.html#WS2db454920e96a9e51e63e3d11c0bf62d75-7feb
I know the Animation.swf file is okay, because if I load it into the main app it displays and runs.
The preloader works if an image is loaded by the preloader instead of the animation SWF.
--------------------- test.mxml (main app) -----------------
BTW, I typically have many more lines of ComboBox in the app to force the preloader to display, but limiting number of lines here.
--------- customPreloaders.SparkAnimationProgressBar.as ----------------
package customPreloaders
{
import flash.display.;
import flash.events.;
import flash.net.;
import flash.utils.;
import mx.events.*;
import mx.preloaders.*;
public class SparkAnimationProgressBar extends Sprite
implements IPreloaderDisplay{
// Loader instance to load the animation SWF file.
private var animationLdr:flash.display.Loader;
public function SparkAnimationProgressBar() {
super();
}
// Add event listeners.
public function set preloader(preloader:Sprite):void {
preloader.addEventListener(
ProgressEvent.PROGRESS, handleProgress);
preloader.addEventListener(
Event.COMPLETE, handleComplete);
preloader.addEventListener(
FlexEvent.INIT_PROGRESS, handleInitProgress);
preloader.addEventListener(
FlexEvent.INIT_COMPLETE, handleInitComplete);
}
// Initialize the Loader control in the override
// of IPreloaderDisplay.initialize().
public function initialize():void {
animationLdr = new flash.display.Loader();
animationLdr.contentLoaderInfo.addEventListener(
Event.COMPLETE, loader_completeHandler);
animationLdr.load(new URLRequest("assets/Animation.swf"));
}
// After the SWF file loads, set the size of the Loader control.
private function loader_completeHandler(event:Event):void
{
addChild(animationLdr);
animationLdr.width = 200;
animationLdr.height= 200;
animationLdr.x = 100;
animationLdr.y = 100;
}
// Define empty event listeners.
private function handleProgress(event:ProgressEvent):void {}
private function handleComplete(event:Event):void {}
private function handleInitProgress(event:Event):void {}
private function handleInitComplete(event:Event):void {
var timer:Timer = new Timer(2000,1);
timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
timer.start();
}
private function dispatchComplete(event:TimerEvent):void {
dispatchEvent(new Event(Event.COMPLETE));
}
// IPreloaderDisplay interface methods.
public function get backgroundColor():uint {
return 0;
}
public function set backgroundColor(value:uint):void {}
public function get backgroundAlpha():Number {
return 0;
}
public function set backgroundAlpha(value:Number):void {}
public function get backgroundImage():Object {
return undefined;
}
public function set backgroundImage(value:Object):void {}
public function get backgroundSize():String {
return "";
}
public function set backgroundSize(value:String):void {}
public function get stageWidth():Number {
return 200;
}
public function set stageWidth(value:Number):void {}
public function get stageHeight():Number {
return 200;
}
public function set stageHeight(value:Number):void {}
}
}
I remember there are hard restrictions on your custom preloader.
It (assets/Animation.swf) should be a Flash animation of exactly 100 slides.
Probably that could be the reason.

AS3: Event dispatching via dispatchEvent- pass params?

I would like to dispatch an event from my class along with a url.
I know that I can do the following:
import flash.events.EventDispatcher;
private function thumbClick(e:MouseEvent):void
{
dispatchEvent(new Event("clicked"));
}
But I don't know how I would send params along with the event...?
Also, in my main app runner, I try:
var my_ev:Event = new Event("clickedImage");
my_ev.hasOwnProperty(e.currentTarget.link);
dispatchEvent(my_ev);
...but I'm not sure that this would be the correct syntax.
Thanks for any help,
jml
Allan is correct, you will want to make a custom event. Couple of things to note:
import flash.events.Event;
public class ThumbnailEvent extends Event
{
public static var THUMB_CLICKED:String = "thumbClicked";
private var _url:String;
public function get url():String { return _url }
public function ThumbnailEvent (type:String, url:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type,bubbles,cancelable);
_url = url
}
override public function clone():Event
{
return new ThumbnailEvent(type, url, bubbles, cancelable);
}
}
Custom events need to always override clone. If the event is bubbled or relayed in anyway it needs this method. Custom properties should be private with a read-only getter. This is a standard convention to prevent the alteration of properties throughout the life of the event.
Using this approach would change your code to:
private function thumbClick(e:MouseEvent):void
{
dispatchEvent(new ThumbnailEvent(ThumbnailEvent.THUMB_CLICKED, myUrlString));
}
//elsewhere
addEventListener(ThumbnailEvent.THUMB_CLICKED, thumbClickedHandler);
private function thumbClickedHandler(event:ThumbnailEvent):void
{
var link:String = event.url;
}
Custom Event tutorial at adobe.com
I just make a custom event class.
import flash.events.Event;
public class ThumbnailEvent extends Event
{
public static var THUMB_CLICKED:String = "thumbClicked";
public var url:String;
public function ThumbnailEvent (type:String,url:String)
{
super(type);
this.url = url
}
}
and then use it like:
var thumbEvent:ThumbnailEvent = new ThumbnailEvent(ThumbnailEvent.THUMB_CLICKED,"myURL");
dispatchEvent(thumbEvent);

Flex 3 Customevent not getting dispatched

I have a function called DrawPlaybook which listens to two events, one mouseclick event and one custom event.
public function DrawPlaybook(...):void
{
//...... other stuff
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onClickHandler(e,this.panel)});
panel.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
onCustomPanelClicked);
}
I am planning to call the custom event from within "onClickHandler" like this:
public function onClickHandler(e:MouseEvent,panel):void
{
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = panel;
dispatchEvent(eventObj);
}
private function onCustomPanelClicked(e:CustomPageClickEvent):void {
Alert.show("custom click");
}
And here is the class definition for CustomPageClickEvent:
package
{
import flash.events.Event;
import mx.containers.Panel;
public class CustomPageClickEvent extends Event
{
public var panelClicked:Panel;
// Define static constant.
public static const PANEL_CLICKED:String = "panelClicked";
public function CustomPageClickEvent(type:String){
super(type);
//panelClicked = panel;
}
// Override the inherited clone() method.
override public function clone():Event {
return new CustomPageClickEvent(type);
}
public function getPanelSource():Panel{
return panelClicked;
}
}
}
The issue is that "onCustomPanelClicked" never gets invoked at all. Please let me know if you notice anything that I missed.
It's because you have registered the event listener for CustomPageClickEvent on the Panel, but you're dispatching it from DrawPlaybook
Just change this:
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = panel;
dispatchEvent(eventObj)
to this:
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = panel;
panel.dispatchEvent(eventObj)
... or change the event listener to this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
onCustomPanelClicked);.
Let me know if that works.

Custom Event Not Captured- why?

I am having a problem where I dispatch a custom event but the listener does not receive it (i.e. myHandler() in the code below). If I put everything in one mxml file, it works. When I separate the responsibilities in to separate classes, it fails. It is not clear to me what I am missing.
Any help you be appreciated.
Here is my code (update() in ViewModel.as is the entry point):
ChangeEvent.as
import flash.events.Event;
public class ChangeEvent extends Event
{
public function ChangeEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
// Set the new property.
this.isEnabled = isEnabled;
}
// Define static constant.
public static const ENABLE_CHANGED:String = "enableChanged";
// Define a public variable to hold the state of the enable property.
public var isEnabled:Boolean;
// Override the inherited clone() method.
override public function clone():Event {
return new ChangeEvent(type, isEnabled);
}
}
Model.as
public class Model extends EventDispatcher
{
private function TriggerEvent():void
{
var eventObj:ChangeEvent = new ChangeEvent(ChangeEvent.ENABLE_CHANGED);
dispatchEvent(eventObj);
}
}
ViewModel.as
public class ViewModel
{
import mx.controls.Alert;
import ChangeEvent;
private var model:Model;
public function ViewModel()
{
model = new Model();
addEventListener(ChangeEvent.ENABLE_CHANGED, myHandler);
}
public function update():void {
model.LoadData();
}
private function myHandler(event:Event):void {
Alert.show("An event occurred.");
}
}
Do I have to 'register' the event in ViewModel.as similar to the metadata tag in mxml?
e.g.
[Event(name="enableChange", type="ChangeEvent")]
You have to add the event listener on the model object (since it is the one dispatching the event).
model = new Model();
model.addEventListener(ChangeEvent.ENABLE_CHANGED, myHandler);
Hope that helps.

Resources