How can I know which button was clicked in Flash - actionscript-3

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 :)

Related

Loop animation until button is pressed, but let animation finish

Well I know what I must do, just don't know how.
I know I have to (after make all my animations and whatnot) declare all variables on frame 1(in a seperate time-line), like a=0;b=0...
Then do a gotoandplay at the end of the loop to the begging, with a condition (in frame 120, if a=0 gotoandplay (100) otherwise gotoandplay(15)
And then have a button to change the value of a, so that when the loop meets the conditional it goes to whatever frame I want.
Any pointers?
Thanks
Lets assume your button has an instance name of btn and you looping animation has an instance name of mc_loop.
You can do something like this:
//listen for the mouse click on your button
btn.addEventListener(MouseEvent.CLICK, buttonClicked, false, 0, true);
//run this function on the click
function buttonClicked(e:Event):void {
//add a script to the last frame that calls this inline function
//the addFrameScript method wants a 0 based frame number, so the first frame would be 0, the last frame would the total frame count less 1
mc_loop.addFrameScript(mc_loop.totalFrames-1, function(){
mc_loop.stop(); //stop the clip so it doesn't loop anymore
mc_loop.addFrameScript(mc_loop.totalFrames-1, null); //remove the frame script so it doesn't run again should mc_loop reach the last frame again
});
}
With this code, assuming mc_loop is already playing/looping, clicking the btn will stop it once it reaches its last frame.
So i've taken both of yours suggestions, and did this
1 animation, with a circle going left and right and a button going from frame 1-20 and text saying "Done" one layer 21
3 layers :
Actions
Button
Layer 1 (animation layer)
After that i set on Action frame 1, the code
var loop:Boolean =true
And on frame 20
if (loop==true){
gotoAndPlay(2);
}else {
gotoAndStop(21)
}
Then on the button, did
btn.addEventListener(MouseEvent.CLICK, buttonClicked, false, 0, true);
function buttonClicked(e:Event):void {
loop=false
}
And it worked. the loop finishes and only then it goes to the 21st frame, and stops there.
Am i missing something? or is this a good solution? (also, might use this for a menu, with case instead of if's and integer values to jump to which chapter i want)

ActionScript 3.0 - How to make a button finish looping the scene, then go to the next scene

So, lets say scene 1 has 40 frames. Someone clicks on the button to go to the next scene at frame 10. I want it to continue to frame 40, then change to the next scene.
Keep in mind that the 40 frame scene is on loop, so it could loop 4 or 5 times, then someone clicks next scene. So, I want it to finish the loop, then go to the next scene.
I've been looking everywhere on how to do this, and can't find anything!
So, I have this code right here -
At the beginning of the movie (Frame 1-39), I have -
var clicked:Boolean = false;
button1.addEventListener(MouseEvent.CLICK, goScene2);
function goScene2(event:MouseEvent):void {
clicked = true;
}
Then, on the last frame (frame 40), I have -
if(MouseEvent.CLICK){
gotoAndPlay(1,"Scene 2");
}
else{
gotoAndPlay(1,"Scene1");
}
It goes to scene 2, even if you don't click the button.
Thanks in advance!
That's because the MouseEvent.CLICK variable is always truthy. It's the constant you use to bind a click event handler. I think you mean to use the clicked variable that you set to true in the click handler.
if(clicked){
gotoAndPlay(1,"Scene 2");
}
else{
gotoAndPlay(1,"Scene1");
}

Simple click event

I don't know why I can't figure out this problem that is really basics !! (sometimes the brain is tired I guess).
I've got a movieclip with a guitar string.
I want the string to move everytime I click on it.
I've created a movie clip with 2 lables. the first = non movement, the second = movement.
I've placed in the second one a stop(); action. (in order to stop the loop)
I've put this code :
stringOne.addEventListener(MouseEvent.CLICK, accord1, false, 0, true);
public function accord1(e:MouseEvent):void{
var stringOne;
trace("DING");
stringOne.gotoAndStop("first");
}
It works but, of course, it only play the string movement at the first click.
Do you know how I could play the string movement EVERYTIME that I click on the string ?
Thank you very much and sorry for this easy question (little ashamed)..,
EDIT
Ah ! It seems to work with goToAndPlay !
if (stringOne.currentLabel=="premier") {
stringOne.gotoAndStop("default");
} else {
stringOne.gotoAndStop("first");
}
Just a thing, I have to click twice..
(one click = the string vibrate (label2))
(one click again = the string does nothing (going to label 1))
(one click again = the string vibrate (label 2))
Is there anyway to automatically skip the 2nd click (the one that tells the string to go back at label 1), and let do the code like : - When animation of label 2 is finished, automatically go back to label 1 ? –
Presumably, the "movement" label of the string is some sort of animation?
It seems to me what you want in your "guitar string" movieClip is, on the last frame of the timeline animation for "movement", a script that says gotoAndStop('non movement'). The click handler should gotoAndPlay('movement').
Then, when the string 'movement' animation is finished, it will reset itself, so that the next time you click it will play again.
So, your original code is fine (before the edit; but remove the "var stringOne;" since that will break the reference to stringOne). The only thing you need to add is a script in the timeline on the last frame of the movement animation ("first" label?) that says gotoAndPlay("default") (assuming default is the 'non movement' label). You may need a stop() in the timeline frame for "default".
Something like that :
stage.addEventListener( MouseEvent.CLICK, onStageClick);
protected function onStageClick(event:MouseEvent):void
{
switch( event.target )
{
case stringOne:
trace("String one stuff");
break;
case stringTwo:
trace("String 2 stuff");
break;
}
}
Or add your movieClip into a Sprite and listen click event on the Sprite and not on the MovieClips.
Thank you for the answer. I've found an other way, instead of mouse click I've used mouse down for going to frame 2 and mouse up for going to frame 1

AS3 - how to make 4 buttons and go to another frame when all 4 buttons clicked in random order

Hello I hawe 4 buttons on one frame and I can't figure out how to go to another frame when I clicked all 4 buttons in random order. Condition is one - all 4 buttons must be clicked at least once.
my buttons:
Button1
Button2
Button3
Button4
in frame 1 - when al clicked - goto frame 2
Here is a pretty common way of getting this done.
private var _buttonsRemaining:int;
private function setup():void{
button1.addEventListener(MouseEvent.CLICK,onButtonClicked);
button2.addEventListener(MouseEvent.CLICK,onButtonClicked);
button3.addEventListener(MouseEvent.CLICK,onButtonClicked);
button4.addEventListener(MouseEvent.CLICK,onButtonClicked);
_buttonsRemaining = 4;
}
private function onButtonClicked(e:MouseEvent):void{
(e.target as EventDispatcher).removeEventListener(MouseEvent.CLICK,onButtonClicked);
_buttonsRemaining --;
if(_buttonsRemaining <= 0){
allClicked();
}
}
private function allClicked():void{
trace("all buttons clicked.");
}
Unlike above, I like to declare 'allClicked' as an Event-friendly method. This way you can dispatch an event -- Event.COMPLETE maybe -- to trigger the method.
private function allClicked(e:Event=null):void{
//...
}
I am not sure if I understand your problem right. I assume you mean be "frame" the view of a view controller, and you want to show another view of another view controller as soon as all of your 4 buttons in the 1st view have been pushed at least once.
I assume you have already your first view controller and its view, together with the 4 buttons, defined in the storyboard. You could then drag a new view controller from the library to your storyboard. You had to define a new subclass of an UIViewController in your app, and set the class of the new view controller in the storyboard to this new subclass.
Next you had to check if all of your buttons have been pressed. You could thus define one #property bool buttonXpressed for each button X, and set this property to YES when the IBAction method of this button is pressed. In each of these IBActions, you could check if all 4 properties are set to YES, and if so, you could present the view of the 2nd view controller by calling the presentViewController:animated:completion: method with the 2nd view controller as argument.

how to check which button is clicked in action script 3.0

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