how to check which button is clicked in action script 3.0 - actionscript-3

I am making game in action script i want that when user selest any region etc then the value of that region must be selected i have 7 regions. how to identify which is the clicked button or selected region

Your question not very clear, but you can add a mouse listener to check if a button is clicked. You can use event.target to detect which button you actually clicked on.
this.mcButton1.addEventListener(MouseEvent.CLICK, handleButtonClick);
this.mcButton2.addEventListener(MouseEvent.CLICK, handleButtonClick);
this.mcButton3.addEventListener(MouseEvent.CLICK, handleButtonClick);
function handleButtonClick(event:MouseEvent):void
{
var button:DisplayObject = DisplayObject(event.target);
trace('I am clicked: ' + button);
}

Related

Wrong code in tutorial for event listeners

I am following this tutorial to build a store locator page with a Mapbox map.
I don't want to add custom markers because I already have custom map labels (symbols?), which means I don't need the optional last section of the tutorial and stop right after Add Event Listeners.
Once this is completed, the page should react to clicks in the side panel list, as well as on the map (2 event listeners). However, in the demo provided in the tutorial for that particular step, you can tell the code for the second event listener, the one making the map clickable, is not functioning, which makes me believe there is a mistake in the provided code:
// Add an event listener for when a user clicks on the map
map.on('click', function(e) {
// Query all the rendered points in the view
var features = map.queryRenderedFeatures(e.point, { layers: ['locations'] });
if (features.length) {
var clickedPoint = features[0];
// 1. Fly to the point
flyToStore(clickedPoint);
// 2. Close all other popups and display popup for clicked store
createPopUp(clickedPoint);
// 3. Highlight listing in sidebar (and remove highlight for all other listings)
var activeItem = document.getElementsByClassName('active');
if (activeItem[0]) {
activeItem[0].classList.remove('active');
}
// Find the index of the store.features that corresponds to the clickedPoint that fired the event listener
var selectedFeature = clickedPoint.properties.address;
for (var i = 0; i < stores.features.length; i++) {
if (stores.features[i].properties.address === selectedFeature) {
selectedFeatureIndex = i;
}
}
// Select the correct list item using the found index and add the active class
var listing = document.getElementById('listing-' + selectedFeatureIndex);
listing.classList.add('active');
}
});
Would anyone be able to tell what is wrong with this code?
Turns out the code is incomplete in that the cursor doesn't change to a pointer as you hover over a map label/marker so it doesn't clue you into realising you can click on it, hence my assumption it wasn't working at all. I assume the general users who would then face the map would be equally deceived unless the pointer shows up. So in the tutorial, if you do go ahead and click the marker, it will have the expected behaviour and display the popup, although no pointer is shown.
Here is how to create the pointer, based on this fiddle: https://jsfiddle.net/Twalsh88/5j70wm8n/25/
map.on('mouseenter', 'locations', function(e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'locations', function() {
map.getCanvas().style.cursor = '';
});

How can I know which button was clicked in Flash

I am making a simple game in Flash CS6.
There are 3 layers:
Action
Layer 1
Layer 2
In layer 1 there are 3 buttons:
button 1, button 2 and button 3.
Each button sends you to frame 5 on layer 2. For example, there is another button, FORWARD button.
How can I know on frame 5 which button was clicked? And how can I make it go to different frames by clicking in the FORWARD button depending if came from button 1, 2, or 3.
For whichever function you use your click event for going to different frame, just add the condition in the function definition:
if (event.target == btn_1) //btn_1 is the instance name of button 1
{ //add whatever you want to do with it
}
else if (event.target == btn_2) // btn_2 is the instance name of button 2
{
}
add as many conditions as you like.
Problem SOLVED.
Add:
var bt:int=0;
In each button function ad bt=1, bt=2 and bt=3.
Then in the other button function add:
If bt=1 gotoAndStop(5), if bt=2 gotoAndStop(6), if bt=3 gotoAndStop(7)
Thanks anyway :)

verifying a IF condition always AS3

I'm a beginner in AS3 so please if possible to give a point for a noob as answer. I want to have a specific button that is clicked to create a line, for example I click the button then I need to click twice on the stage
I click it and it is selected
I click first time on the stage to select the starting point of the line
I click second time on the stage to select the finishing point.
I tried to do it but i can`t manage to, if it is possible please help me. This is the code i wrote
dr_line.addEventListener(MouseEvent.CLICK,drawln);
var test:Boolean;
function drawln(e:MouseEvent):void{
test=true;
stage.addEventListener(MouseEvent.CLICK,reportClick);
}
var sx,sy,fx,fy:int;
var j:int;
function reportClick(event:MouseEvent):void
{
j=0;
j++;
if (test==true && j==1) {
sx=event.localX;
sy=event.localY;
}
j++;
test=true;
trace(j);
trace(test);
if (test==true && j==2) {
fx=event.localX;
fy=event.localY;
j=0;
test=false;
var line:Shape = new Shape();
line.graphics.beginFill(0x00FF00);
line.graphics.moveTo(sx,sy);
line.graphics.lineTo(fx,fy);
this.addChild(line);
}
}
How should i make it so this will work .. please help me , Thanks !!!! ,and yes the function reportClick should always be checked .... but i can`t get it going or when i press the dr_line button it should be active so it will be checked ....
You are assigning j=0 in click handler... you should do that in drawLn instead. Also, you require only one j++ in click handler.
But a cleaner approach would be to have two click listener point1ClickListener and point2ClickListener. You should attach to point1ClickListener in drawLn. Then inside point1ClickListener remove listening clicks to point1ClickListener and attach a click listener for point2ClickListener. Again, when point2ClickListener gets called remove listening for clicks to point2ClickListner.

Making simple paint in html 5 canvas

I have simple code to "draw" in canvas element:
function doFirst(){
var x = document.getElementById('canvas');
canvas = x.getContext('2d');
window.addEventListener("mousemove", rys, false);
}
function rys(e){
var xPos = e.clientX;
var yPos = e.clientY;
canvas.fillStyle="#000";
canvas.beginPath();
canvas.arc(xPos-7,yPos-7,10,0,Math.PI*2,true);
canvas.closePath();
canvas.fill();
}
window.addEventListener("load", doFirst, false);
As you can see, the function is working only when the mouse is over the canvas element. Now i want to make it "draw" when the mouse is clicked (just like in paint). Can someone tell me how to do it (with code)?
Thx for help
You need to keep track of the current mouse button state (pressed or not pressed) independently of the mouse movements.
You can do this by attaching event handlers to the "mousedown" and "mouseup" events, similar to how you attached to the "mousemove" event.
In these event handlers, you could keep track of the current state of the first mouse button by updating a global variable indicating whether or not the button is currently pressed. Then, in your "mousemove" handler, you can check this global variable and determine whether or not to paint when the mouse is moved.
When using the "mouseup" and "mousedown" events, you may want to limit your handling to only when the first mouse button is pressed. You can do this by checking that the event property "button" is 0. Note, that you can check for other buttons and keep track of them, also.
You can see a working example of this here: http://jsfiddle.net/mQtKz/

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