View Full Version : Listen on CustomEvent to SpotSprite object in AS3
ranone
11-20-2009, 12:55 PM
Hi all, I would execute a function on my custom event.
My goal is to run the FOR statement only once at the beginning of my application.
I followed this (http://evolve.reintroducing.com/2007/10/23/as3/as3-custom-events/) guide to create a custom event.
Custom Event package:
package
{
import flash.events.*;
public class CustomEvent extends Event
{
public static const DEFAULT_NAME:String = "DefaultName";
// event constants
public static const ON_TEST_CASE:String = "onTestCase";
public var params:Object;
public function CustomEvent($type:String, $params:Object, $bubbles:Boolean = false, $cancelable:Boolean = false)
{
super($type, $bubbles, $cancelable);
this.params = $params;
}
public override function clone():Event
{
return new CustomEvent(type, this.params, bubbles, cancelable);
}
public override function toString():String
{
return formatToString("CustomEvent", "params", "type", "bubbles", "cancelable");
}
}
}
Main file:
import flash.events.*;
import CustomEvent;
var evt:CustomEvent = new CustomEvent(CustomEvent.ON_TEST_CASE, {param1: "first param", param2: "second param"});
for (var spot_id:String in hotspots.getSpots()) {
var spot:Object = hotspots.getSpot(spot_id).movie;
// this works
//spot.addEventListener(MouseEvent.CLICK, doStuff);
// this not works
spot.addEventListener(CustomEvent.ON_TEST_CASE, doStuff);
// this print TRUE
trace(spot.hasEventListener(CustomEvent.ON_TEST_CA SE));
}
dispatchEvent(evt);
function doStuff(e:Event):void
{
trace(e.target);
}
As I've commented in the code, the EventListener doesn't work on my SpotSprite object.
Any suggestions?
Thanks.
AxeCrazy
11-22-2009, 11:04 PM
the spot does not know your custom event, so how should it trigger it?
MouseEvent.CLICK is a standard event, which is supported within flash and thus in flashpanorama.
Why should you want to use a customclass?
ranone
11-23-2009, 07:31 AM
AxeCrazy thanks for your reply.
I have created several custom events that are dispatched by custom functions/methods.
My goal is to assign to each Spot one or more personalized listener. This is to avoid the use of the FOR statement and scroll all the Spots every time.
It's possible?
LepLep
11-23-2009, 02:04 PM
can you say what events exactly you want spots listen to?
it would make it much easier to help
allSaints
11-23-2009, 04:26 PM
LepLep,
I think the point is that he is going to fire the event himself, then he wants the spot that he has added the event to, to answer by calling his function.
And I can't really see why it shouldn't work - maybe a "bubbling" issue...or does that only involve button events?
Tommy
LepLep
11-23-2009, 04:59 PM
ok, to avoid "for" statemnt and scanning all hotspots
i would do it this way
spotObject = panorama.externals.hotspots.getSpot("thisSpotID").movie;
spotObject.addEventListener ...
(not sure if it helps in this case)
AxeCrazy
11-23-2009, 06:17 PM
but then again
who will dispatch this custom event?
The spotobject object self will probably not dispatch this event, or, do you dispatch this event from a different point in your code.
The pano player and 'his' implementation of the spot object can never dispatch your custom event type.
Or (perhaps it's the other way around :) ) do you want the spot objects themself to react to a custom event (for example, toggle visiblility based on this custom event) That should be possible as long as you change known parameters of the spot object (again eg. visibility ;) )
Perhaps you could clarify what you want these custom events to do.
AxeCrazy
11-23-2009, 06:22 PM
i looked into your example code above more clearly
you add an eventlistener to the spot object like this
spot.addEventListener(CustomEvent.ON_TEST_CASE, doStuff);
then dispatch this evnet like this
dispatchEvent(evt);
And indeed this does not work, since spot is in that case not the one that is dispatching the event.
you should dispatch an event something like
spot.dispatchEvent(new Event(CustomEvent.ON_TEST_CASE));
that will work, but then you could probably just call the doSomething() function yourself :P
AxeCrazy
11-23-2009, 06:26 PM
maybe a "bubbling" issue...or does that only involve button events?
No, the bubbling issue is valid for every event dispatched within Actionscript.
It is not only a button event option, but it is commonly used with buttons thats true :)
ranone
11-23-2009, 06:34 PM
I tried with the bubbling issue, but it's does not work.
Suppose you have 10 hotspots and to manage the following cases:
- all 10 hotspots should be hidden when the Custom1 event is dispatched
- the first 5 hotspots should be hidden when the Custom2 event is dispatched
- the last 5 hotspots should be hidden when the Custom3 event is dispatched
- hotspots 4,5,6,7 should be hidden when the Custom4 is dispatched
And so on
allSaints
11-23-2009, 06:42 PM
And these events does not include loading any new pano...?
AxeCrazy
11-23-2009, 07:17 PM
I tried with the bubbling issue, but it's does not work.
Suppose you have 10 hotspots and to manage the following cases:
- all 10 hotspots should be hidden when the Custom1 event is dispatched
- the first 5 hotspots should be hidden when the Custom2 event is dispatched
- the last 5 hotspots should be hidden when the Custom3 event is dispatched
- hotspots 4,5,6,7 should be hidden when the Custom4 is dispatched
And so on
A, ok
But then again.
Who is dispatching these events?
since there is where you should add the eventlistener to ;)
And in the callback function you could check which hotspot should be hidden or visible..
ranone
11-25-2009, 08:21 AM
And these events does not include loading any new pano...?
No, the custom events doesn't include loading any new pano.
ranone
11-25-2009, 10:16 AM
A, ok
But then again.
Who is dispatching these events?
since there is where you should add the eventlistener to ;)
And in the callback function you could check which hotspot should be hidden or visible..
My application dispatches several custum event on TimerEvent, on MouseEvent, etc.
The callback function should have as event.target the spot. So I can change the visibility (for example) on the single object and I am not forced to scroll all the spots.
AxeCrazy
11-28-2009, 05:47 PM
but that will not work i am afraid.
For the way you attach the eventListeners it is neccesary that the spot himself dispatches the event.
Perhaps it is easier to create custum array for each "event"
So something like
var arrayCustom1:Array = new Array();
arrayCustom1.push (spot1);
arrayCustom1.push (spot2);
arrayCustom1.push (spot3);
this.addEventListener(CustomEvent.EVENT1, event1Callback);
private function event1Callback(e:Event){
for(var p in arrayCustom1){
arrayCustom1[p].visible.false
}
}
In this way, in the callback function you only adress the spot you want and leave the rest as they are.
Hope it is clear.
vBulletin® v3.7.1, Copyright ©2000-2012, Jelsoft Enterprises Ltd.