AS3 : How to identify which button was pressed? - actionscript-3

I have a MovieClip with a button inside it named t_bt. I have exported that MovieClip to action script and gave it a class name of e_panel . I created 50 instances of e_panel to stage with this code:
var e_p_y:Number=0;
for ( var i:Number=1;i<=50;i++)
{
var e_p:MovieClip = new e_panel();
e_p.x = 50;
e_p.y = e_p_y;
e_p.t_bt.addEventListener(MouseEvent.MOUSE_UP, f1);
addChild(e_p);
e_p_y = e_p_y+105;
}
now I want identify which button was pressed by user in function f1.
function f1(event:MouseEvent):void
{
//...what should I write here?
}

Every event has a property called currentTarget, which will be a reference to the object you added the event listener to.
So in your case:
function f1(event:MouseEvent):void
{
var t_bt:DisplayObject = event.currentTarget as DisplayObject; //would be the t_bt instance that was clicked (typed as a basic object)
var e_p:MovieClip = DisplayObject(event.currentTarget).parent as MovieClip //would be the e_p of the item clicked
//so if you wanted to do something like make the whole panel half transparent once clicked
e_p.alpha = .5;
//if you wanted to get the index of the button clicked
trace("Button Clicked: ", e_p.parent.getChildIndex(e_p));
}
In contrast, the target property of an event is the actual displayObject that was clicked (which could be the same as currentTarget or a child of the currentTarget)

e_panel clip must have an associated E_Panel class that extends MovieClip and (for example) adds an index property to hold the index of the item.
Then, on f1 you could do:
function f1(event:MouseEvent):void
{
var e_panel:E_Panel = event.currentTarget.parent as E_Panel;
trace(e_panel.index);
}
Remember to set index property upon movie clip creation
Hope it helps
Extra info
Create this a class for E_Panel and link it to movieClip with library properties
public class E_Panel extends MovieClip
{
private var _index:int;
public function E_Panel(index:int)
{
super();
_index = index;
}
public function get index():int {
return _index;
}
}
Then in your code:
var e_p:E_Panel = new e_panel(i); //"i" is the iteration counter
e_p.x = 50;
... //Etc

Try this Code::
var e_p_y:Number=0;
for ( var i:Number=1;i<=50;i++)
{
var e_p:MovieClip= new e_panel();
e_p.x=50;
e_p.y=e_p_y;
e_p.t_bt.addEventListener(MouseEvent.MOUSE_UP, f1);
addChild(e_p);
e_p_y=e_p_y+105;
e_p.name= i+'';
}
Now write this code in f1 function::
function f1(event:MouseEvent):void
{
trace(event.currentTarget.name);
}

Related

How to make sure mouse event can only detect movie clip according to their orders in action script 3?

I'm trying to figure out how to make action on movie clip which can only be detected according to movie clip order.
Follows my code:
test1.addEventListener (MouseEvent.MOUSE_DOWN, test1OnClick);
function test1OnClick (e:MouseEvent) : void
{
var currentMC:MovieClip = MovieClip (e.target);
.....
}
test2.addEventListener (MouseEvent.MOUSE_DOWN, test2OnClick);
function test2OnClick (e:MouseEvent) : void
{
var currentMC:MovieClip = MovieClip (e.target);
.....
}
I want to make sure the user can only continue clicking movie clip "test2" after done with "test1".
Use
test1.removeEventListener(MouseEvent.MOUSE_DOWN, test1OnClick);
when the user is done with test 1.
If you want to only be able to click on one at a time, don't add the test2 listeners until you remove the test1 listener. It could look like this
test1.addEventListener (MouseEvent.MOUSE_DOWN, test1OnClick);
function test1OnClick (e:MouseEvent) : void
{
var currentMC:MovieClip = MovieClip (e.target);
test1.removeEventListener(MouseEvent.MOUSE_DOWN, test1OnClick);
test2.addEventListener (MouseEvent.MOUSE_DOWN, test2OnClick);
.....
}
function test2OnClick (e:MouseEvent) : void
{
var currentMC:MovieClip = MovieClip (e.target);
.....
}
Or you could do something a little more sophisticated like this
var currentMC:MovieClip;
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseClick);
private function mouseClick(e:MouseEvent):void{
currentMC = e.target as MovieClip;
//...
}
}
And now you don't need a separate function for each test. How to exactly implement this depends on what you put in the ... though.

Replacing a movieclip with another movieclip, keeping the same instance name

I'm new to actionscript so this question might be a stupid one.
I'm trying to replace a movieclip with another movieclip, while keeping the instance name of the previous.
I have a menu with a selection of buttons, each leading to the same screen with a movieclip and a scrubber bar. I tried defining the movieclip through a variable, then tried redefining it through an event listener function, but I'm guessing I can't do like this:
var MC: movieclipsymbol1 = new movieclipsymbol1;
private function selectionscreen(): void {
selectionscreenbutton1.addEventListener(MouseEvent.CLICK, screenbutton1);
selectionscreenbutton2.addEventListener(MouseEvent.CLICK, screenbutton2);
private function screenbutton1(event: MouseEvent): void {
var MC: movieclipsymbol1 = new movieclipsymbol1;
movieclipscreen();
}
private function screenbutton2(event: MouseEvent): void {
var MC: movieclipsymbol2 = new movieclipsymbol2;
movieclipscreen();
}
}
public function movieclipscreen(): void {
stage.addChild(MC);
}
Because of the scrubber bar code I did, I need to keep the instance for the movieclips the same. Is the approach I'm using completely off?
You have to remove var MC from both handlers, as you want your new MC to be accessible from outside of the handlers. But also you need to change the type of class variable MC so that it could hold either movieclipsymbol1 or movieclipsymbol2. The most common choice for the type in there is MovieClip. So, you have to change your functions like this:
var MC:MovieClip = new movieclipsymbol1();
private function screenbutton1(event: MouseEvent): void {
clearOldMC();
MC = new movieclipsymbol1();
movieclipscreen();
}
private function screenbutton2(event: MouseEvent): void {
clearOldMC();
MC = new movieclipsymbol2();
movieclipscreen();
}
private function clearOldMC():void {
if (MC.parent) MC.parent.removeChild(MC);
}
The new function removes the previously displayed movie clip, regardless of its type.
Use "name" property of display object to give the instance name to movieclip.

want to change the code actionscript2 to actionscript3?

i am newbie to flash.i need to change the below actionscript code to actionscript 3.0 code.
i am currently working on drag and drop. so i want to duplicate the movieclip while dragging i found the code on internet but it is actionscript 2.0 so please convert it to as3. the box is a instance name of a movieclip.
the code blocks are:
var num:Number = 0
box.onPress = function(){
num++
duplicateMovieClip(box ,"box"+num, _root.getNextHighestDepth())
_root["box"+num].startDrag();
}
box.onReleaseOutside = function(){
trace(_root["box"+num])
stopDrag();
}
If you dont want to use seperate .as file, follow this steps:
1- assign AS linkage to box movieClip (in library panel):
2- Select frame 1 on the timeline, and paste this code in the Actions panel:
var boxes:Array=[];
//var box:Box=new Box();
//addChild(box);
box.addEventListener(MouseEvent.MOUSE_DOWN,generateBox);
function generateBox(e:MouseEvent):void{
var newBox:Box=new Box();
newBox.x = e.target.x;
newBox.y = e.target.y;
newBox.startDrag();
newBox.addEventListener(MouseEvent.MOUSE_UP,stopD);
newBox.addEventListener(MouseEvent.MOUSE_DOWN,startD);
boxes.push(newBox);
addChild(newBox);
}
function startD(e:MouseEvent):void{
e.target.startDrag();
}
function stopD(e:MouseEvent):void{
e.target.stopDrag();
}
Unfortunately, there's no duplicateMovieClip analog in AS3, so you'll have to create a Class for your box movieClip template. Let's say it will be called BoxTemplate. (You can google how to create Classes for your library object). Add a Class with this name and add this code (event subscription in the constructor and a private event listener). You'll get something like this:
package
{
public class BoxTemplate
{
public function BoxTemplate()
{
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
}
private function onMouseUp(e:MouseEvent):void
{
stopDrag();
}
}
Leave your present instance of this symbol on the stage. This is your code in the frame:
import flash.event.MouseEvent
box.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
var newBox:BoxTemplate = new BoxTemplate();
newBox.x = e.target.x;
newBox.y = e.target.y;
addChild(newBox);
newBox.startDrag();
}
It will allow you to infinitely clone your boxes. Of course, you can add all of them in the array to keep the references.

Action Script 3.0 Mouse Event in a class package

am having problem with using mouse click event inside a class, i am an absolute beginner to Action Script.
what i want is that if i click the btn_MClick button it should run the script, but everytime i click it i get error message that btn_MClick is undefined.
btn_MClick is on stage and with the instance name if btn_MClick
public class gunShip1 extends MovieClip
{
var moveCount = 0;
public function gunShip1()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveGunShip1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, ShootGunShip1)
btn_MClick.addEventListener(MouseEvent.MOUSE_DOWN.KEY_DOWN, ShootGunShip1);;
}
function ShootGunShip1(evt: MouseEvent)
{
var s_Bullet:survBullet = new survBullet();
var stagePos:Point = this.localToGlobal (new Point(this.width / 2-10, this.height));;
s_Bullet.x = stagePos.x;
s_Bullet.y = stagePos.y;
parent.addChild(s_Bullet);
//play sound
var gun_sound:ricochetshot = new ricochetshot();
gun_sound.play();
}
}
Please, i have absolutely no idea what to do, and somehow it feels like the whole process is wrong.
Your class gunShip1 does not have the property btn_MClick, the root, or document class does.
Basically what's happening is that you've placed your button on the stage, which makes it an instance that belongs to the root container. At the moment, you're trying to refer to the button as a property of gunShip1.
What you should really do here is have the button click managed separately to gunShip1, and have that separate code invoke methods of gunShip1. For example, you could have this in your document class:
public class Game extends MovieClip
{
private var _ship:gunShip1;
public function Game()
{
_ship = new gunShip1();
// The Document Class will have reference to objects on the stage.
btn_MClick.addEventListener(MouseEvent.CLICK, _click);
}
private function _click(e:MouseEvent):void
{
_ship.shoot();
}
}
And then your updated shoot method in gunShip1:
public function shoot():void
{
var s_Bullet:survBullet = new survBullet();
var stagePos:Point = this.localToGlobal (new Point(this.width / 2 - 10, this.height));
s_Bullet.x = stagePos.x;
s_Bullet.y = stagePos.y;
parent.addChild(s_Bullet);
var gun_sound:ricochetshot = new ricochetshot();
gun_sound.play();
}
The idea is that the gunShip1 should not be responsible for dealing with user input (mouse, keyboard, etc). Instead, that should be a separate class which informs gunShip1 that it should do something.

my dynamically added movieclips have a name of "instance XX"

There are a couple things going on here that I dont fully understand. I have created a custom class that extends MovieClip to give some custom properties and create a geometric shape inside of the created MovieClip
package com.hyatt
{
import flash.display.*;
import flash.geom.*;
public class mapPin extends MovieClip
{
public var spirit:String;
public var callName:String;
public var hotelName:String;
public var city:String;
public var s:String;
public var zip:String;
public var country:String;
public var brand:String;
public var featured:Boolean;
public var horizon:Boolean;
private var _mc1:MovieClip = new MovieClip();
public function mapPin(_brand:String)
{
brand = _brand;
switch (_brand)
{
case "Andaz":
pinCircle(0xff0000);
break;
case "Grand Hyatt":
pinCircle(0x0000ff);
break;
case "Hyatt":
pinCircle(0x4600f0);
break;
}
}
private function pinCircle(color:uint):void
{
_mc1.graphics.beginFill(color);
_mc1.graphics.drawCircle(0,0,20);
this.addChild(_mc1);
_mc1.graphics.endFill();
}
}
}
Then I'm adding an couple instances of the mapPin class to a container movieclip on my stage and adding an event listener to that container clip.
var myTest1:mapPin = new mapPin("Andaz");
myTest1.brand = "Andaz";
container_mc.addChild(myTest1);
myTest1.name = "myTest1" //this is added purely for testing the "instance xx", same result
myTest.x = 100;
myTest.y = 100;
var myTest2:mapPin = new mapPin("Hyatt");
container_mc.addChild(myTest2);
myTest2.brand = "Hyatt";
myTest2.x = 400;
myTest2.y = 400;
container_mc.addEventListener(MouseEvent.CLICK, pinClicked);
finally I'm trying to be able to access the properties (the only one set thusfar is "brand") of the mapPin that is clicked.
function pinClicked(e:MouseEvent):void
{
trace(e.target.name); // traces "instance xx" instead of "myTest1"
trace(e.target.brand); // traces "undefined"
}
I can add the mapPin instances, and adjust their x and y though i cannot reference the custom class properties like "brand" and their name becomes a generic instance name. What am I missing? There are going to be upwards of 500 of these items added and I want to be able to pull information from them based upon a users click.
I'd have to see your mapPin class to be sure, but I think that the DisplayObject that is dispatching the event, is a child of mapPin.
To fix this, inside your mapPin class constructor add this line :
mouseChildren = false;
That will specify that children shouldn't receive clicks/dispatch mouse events.
currentTarget is the most recent object to dispatch an event and target is the object that originally dispatched it
It's not. AS3 doc says :
currentTarget :
The object that is actively processing the Event object with an event listener. For example, if a user clicks an OK button, the current target could be the node containing that button or one of its ancestors that has registered an event listener for that event.
target : The event target. This property contains the target node. For example, if a user clicks an OK button, the target node is the display list node containing that button.