how to simulate a click event for android using ActionScript 3 - 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

Related

Actionscript: BlitMask stopping mouse event listeners from working, how to fix?

I have a button class which contains event listeners to trigger an animation of the button on click.
I used many instances of this class to form a list which the user can scroll through. I have implemented BlitMask, this works, however the mouse event listeners in the button class no longer work. This code
_blit = new BlitMask(_mc, _obounds.x, _bounds.y, _bounds.width, _bounds.height, false);
Is what stops the button class.
How can I get the behaviour pre blitmark?
My code which creates the bottom is
var tf:TextField = new TextField(text);
tf.x = 70;
tf.y = 20;
_btn.addChild(tf);
_btn.addEventListener(MouseEvent.CLICK, click);
click is never called.
The blitmask causes the movieclip to be nothing more than an image of clip.
bitmapMode must be set to false to correct this.
_blitMask.bitmapMode = false;
EDIT: To expand on a further issue that implementing this solution causes, turning bitmapMode on only when needed causes the bitmap to not reflect changes in the classes that changed while bitmapMode was off. So you must set
_blitMask.update(null, true);
To force a full update when you use
_blitMask.bitmapMode = true;

AS3: Best way to register event listeners

My question today is about the way we can register an event listener.
Let's say we have a Group element and inside it a custom Handler element. We want the Handler element to do something when Group triggers a custom event. Now, what is the best way to do it?
var group:Group = new Group();
var handler:Handler = new Handler();
group.addElement(handler);
Now, what is the best way to register the event listener?
1. Go on and do it from the file where we initialized the objects
group.addEventListener("CustomEvent", handler.handlerFunction);
2. Register the event listener from the Handler's class:
parent.addEventListener("CustomEvent", handlerFunction);
3. Any other way?
You can let Group class instance dispatch custom event directly on Handler class instance. Handler class would have an internal listener registered for example in constructor.
public function Handler() {
addEventListener("CustomEvent", handlerFunction);
}
Group class would dispatch event following way:
handler.dispatchEvent(new CustomEvent());

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;
...
}

how to get the id from the clicked component

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.

How to get ContextMenuItem x and y coordinates

i build a contextmenu i want when user click any item then i get the x and y coordinate of the contextmenuitem............actully i want to dispaly a textbox infornt of contextmenuitem when user click on the item........ or any other solution that i will show inputtext control as submenuitem in contextmenuitem
The only way I can think of doing what you are asking for is:
disallow the right-click mouse in the
html container with javascript
capture right-click events and
forward them to flash via
ExternalInterface
In the method
triggered from ExternalInterface,
do/show what you want.
There are some open source solutions:
custom-context-menu
Article at ADM blog
Add an event listener to each menu item. In the listener function, the event's target object is the object you clicked on - all you need to do is cast it to DisplayObject, and you can access the x and y coordinates:
contextmenuItem.addEventListener (MouseEvent.CLICK, onItemClick);
function onItemClick (ev:MouseEvent) : void {
var item:DisplayObject = ev.target as DisplayObject;
// use item.x and item.y to get the object's position.
}