how to get the id from the clicked component - actionscript-3

On runtime, I create multiple instants of the Group class, like this:
var groupArtist:Group = new Group();
groupArtist.id = artistXML.id;
groupArtist.width = 150;
groupArtist.height = 170;
groupArtist.clipAndEnableScrolling = true;
groupArtist.layout = new VerticalLayout();
I add an eventlistener:
groupArtist.addEventListener(MouseEvent.CLICK, viewDetails);
This is the eventlistener:
private function viewDetails(event:MouseEvent):void
{
Alert.show(event.target.id);
}
But it's not working. How can I get the id of the clicked Group?
I've checked, and the Id is added correctly to the groupinstances.

Try this:
Alert.show(event.currentTarget.id);
What you're alerting is the "target" that was clicked, and associated in the "MouseEvent.CLICK" event, and you probably want the "currentTarget". As Flex Documentation explains on this, "Every Event object has a target and a currentTarget property that help you to keep track of where it is in the process of propagation. The target property refers to the dispatcher of the event. The currentTarget property refers to the current node that is being examined for event listeners.".
The Most Interesting Man in the World doesn't normally code in Actionscript... but when he does, he uses event.currentTarget.

Related

Loop through array collection using same eventlistener for each item

var lstservices:Array = ["Attachment","Dev","Domain","service"]
for each(var servicename:String in lstservices)
{
var webs:WebService = new WebService();
var url:String= "http://XXXX/XXXX/"+ servicename +".asmx?wsdl";
webs.wsdl = url;
webs.loadWSDL();
webs.addEventListener(FaultEvent.FAULT,fault);
webs.addEventListener(ResultEvent.RESULT,GetDataFromService);
webs.makeObjectsBindable=false;
webs.GetAll();
srvs = servicename.substr(0,servicename.length-7);
}
as you see in the code event listener is added each time and the WSDL is changing each time. the problem is when the loop finishes the control goes to the result event Listener with the result from the first WSDL but the last variable in the loop is holding the last element of the Array not the first one as the WSDL result. I hope the questions is clear for you guys thanks. I used action script in flex
Where is this code placed?
It is going to the last item because the last is the only one. You keep overwriting var webs:WebService so the first one doesn't exist anymore, there is no existing reference to it.

how to simulate a click event for android using ActionScript 3

i have an object called sniper scope and a fire button.
when i point the crosshair of my sniper scope on the target and press the fire button i want it to simulate a mouseevent.click or a touchevent.TAP to play the movieclip of the target object.
how can i do this?
you can use this example, I'm using this in my accessibility implementation for the button in accDoDefaultAction method, it will work for you, you may wish to just use the click event (in my case I had to use all to properly update button with states), and feed with some details like mouseX. The master is the button in my case.
//this is to update buttons state (BaseButton children)
//we need to simulate user interaction in order to have button working
var e:MouseEvent = new MouseEvent(MouseEvent.MOUSE_OVER);
master.dispatchEvent(e);
e = new MouseEvent(MouseEvent.MOUSE_DOWN);
master.dispatchEvent(e);
e = new MouseEvent(MouseEvent.MOUSE_UP);
master.dispatchEvent(e);
e = new MouseEvent(MouseEvent.MOUSE_OUT);
master.dispatchEvent(e);
//this is to trigger actions associated with button (BaseButton children)
e = new MouseEvent(MouseEvent.CLICK);
master.dispatchEvent(e);
However after reading your query second time, I think you r problem may be that the sniper scope is hijacking the event if that is the case try:
myCrosshairInstance.mouseEnabled = false;
myCrosshairInstance.mouseChildren = false;
best regards

How can I recover an object who fires an eventListener event in AS3?

How can I access to an object who fires an eventListener event?
Let's say I have a mc:
var element = new MovieClip();
which has an eventlistener:
element.addEventListener(MouseEvent.CLICK, elementEventHandler);
And then, in the event handler, I want to add something to my mc:
function elementEventHandler(event:MouseEvent):void
{
var b1:balloon = new balloon("ballon1"); //this is another class.
event.target.addChild(b1);//this doesn't work.
}
So that is what I want to achieve... Recover the object who fired the event and then do crazy things with it (in this example, add another object in it).
If anybody has any idea, thanks in advance!
pd: yes, I know I can directly use the var element in this snippet, but in the real code I'm generating the mcs in a loop, according to a xml file.
function elementEventHandler(event:MouseEvent):void
{
// use the as-operator to cast the target into the class you need
var element:DisplayObjectContainer = e.target as DisplayObjectContainer;
// if the cast fails, element will be null, then we bail
if(!element) return;
// then, create your child and add it
var b1:balloon = new balloon("ballon1");
element.addChild(b1);
}
The reason you're getting an error is probably that the event is not coming directly from element but instead from one of its descendant objects.
"click" is a bubbling event.
Check out event flow in the DOM Level 3 Events spec to understand how the capture, target, and bubbling phases work:
http://www.w3.org/TR/DOM-Level-3-Events/#dom-event-architecture
So here's what I would do:
function elementEventHandler(event:MouseEvent):void
{
if (event.target != event.currentTarget)
// If event is not from "element", ignore it.
return;
...
}

adding eventlistener to movieClips created by a class

I may be asking the wrong question but I am trying to add evenListeners to movieClips that are created by a method in one of my Classes.
Creating an instance of my class from the main timeline and then adding that instance to the stage.:
//MY CLASS
var createSlide:AddItems = new AddItems();
var scrollClip:Sprite = new Sprite();
addChild(scrollClip);
//ADDIMAGES CREATES 4 MOVIECLIPS
createSlide.addImages(BG,image1,image2,image3,image4);
//ADD TO STAGE
scrollClip.addChild(createSlide);
SO how do I add event listeners to the movieClips created by createSlide?
If you need more info or this is not clear just let me know.
I'd recommend doing it like this, because it has never failed me:
for each(var mc:MovieClip in scrollClip)
mc.addEventListener("myEvent", onMyEventHandler);
If you have other movie clips in the scrollClip and you don't want to add listeners to them only way is to add names to your added children and then iterate through them and add listeners like in my example.
It's hard to say without knowing what's going on inside AddItems.
Presumably AddItems extends Sprite and adds the newly created objects to itself. In that case, you should be able to access them using getChildAt().
var child1:DisplayObject = createSlide.getChildAt(0);
var child2:DisplayObject = createSlide.getChildAt(1);
var child3:DisplayObject = createSlide.getChildAt(2);
var child4:DisplayObject = createSlide.getChildAt(3);
child1.addEventListener(...);
...
You could also expose them as public properties of the AddItems class.
Finally, you could listen for the events within the AddItems class itself, and dispatch them again as AddItems own events.
not sure if it's what you asked, try this:
var totalChild = createSlide.numChildren-1;
for(var i:int=0;i<totalChild;i++){
var childd = createSlide.getChildAt(i);
childd.addEventListener("event type","func handler");
}
....
//ADD TO STAGE
scrollClip.addChild(createSlide);

ActionScript 3 name property is not returning the right name...?

I experienced a problem with the name property in as3, I created this "dot" movieclip and I exported to a class,
then I anonymously created a bunch of dots using a loop. I assigned numbers as name to each dots
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
dotContainer.addChild(d]);
}
}
so far so good, I checked that if I trace the name here, I will get the number I want.
However, it's not giving me the numbers if I trace it in other functions.
I added all of my dots to "dotContainer", and if I click on one of the dots, it will call this function
private function callFullSub(e:MouseEvent):void
{
var full_loader:Loader = new Loader();
var temp:XMLList = subImages[sub];
var full_url = temp[e.target.name].#IMG;
full_loader.load(new URLRequest(full_url));
full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
}
e.target.name is suppose to be numbers like 1 or 2, but it's giving me "instance66" "instance70" and I
have no idea why. Because I did the same thing with loaders before and it totally worked.
Any ideas? Thanks.
christine
The e.target returns the inner most object clicked on, this could be a TextField, another MovieClip or posibly a shape (I'm not 100% of the last one) inside the "Dot".
To prevent this you could try to set the mouseChildren property to false on the Dot's when you add them. This should insure that nothing inside the dots can dispatch the click event, and thus the Dot's should do it.
Perhaps you could also in the event handler verify the target type with code like this:
private function callFullSub(e:MouseEvent):void
{
if(!e.target is Dot)
throw new Error("target in callFullSub is not Dot but: " + e.target.toString());
//The rest of you code here
}
The answer is [e.currentTarget.name] I perform this all the time!
Should return "Dot1" "Dot2", etc.
If the value you wish to return is a number or other data type other than a string (name of object) use [e.currentTarget.name.substr(3,1).toString()]
Should return 1, 2, etc.
Navee
I tried to reproduce your problem first with Flex using runtime created movieClips and then with Flash using Dot movieClip symbols exported for ActionScript. Neither application exhibited the problem.
You may already know names like "instance66" "instance70" are default enumerated instance names. So, whatever is dispatching the MouseEvent is NOT the dot instance. Perhaps you are unintentionally assigning callFullSub to the wrong targets, maybe your containers? Try assigning it to dot instance right after you create them, like this:
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
d.addEventListener(MouseEvent.CLICK, callFullSub);
dotContainer.addChild(d]);
}
}
Be sure to temporarily comment out your original assignment.
Try this might work,..
d.name = i.toString();
You have not shown enough of your code for me to be able to give you a DEFINATE answer, I will however say this.
//After you create each loader you need to set its mouseEnabled
//property to false if you do not want it to be the target of
//Mouse Events, which may be superseding the actual intended target;
var full_loader:Loader = new Loader();
full_loader.mouseEnabled = false;
//Also you could name the loaders and see if what comes back when you click is the same.
ALSO! Add this to your Mouse Event handler for CLICK or MOUSE_DOWN:
trace(e.target is Loader); //If traces true you have an answer
I believe that the mouse events are being dispatched by the Loaders.
please provide more of your code, the code where the Loader.contentLoaderInfo's COMPLETE handler fires. I assume this is where you adding the loaders to the display list as I cannot see that now.