AS3 movie clip button is not working - actionscript-3

I am creating a button out of a movie clip, and I cannot seem to figure out why it is not working.
I have tried this code:(With this code over nor down will work)
import flash.events.MouseEvent;
this.buttonMode = true;
this.ContinueOver.addEventListener(MouseEvent.MOUSE_OVER, onButtonOver);
function onButtonOver( event:MouseEvent ):void
{
gotoAndStop("over");
}
this.addEventListener(MouseEvent.MOUSE_OUT, onButtonOut);
function onButtonOut( event:MouseEvent ):void
{
gotoAndStop("up");
}
this.ContinueDown.addEventListener(MouseEvent.MOUSE_DOWN, onButtonDown);
function onButtonDown( event:MouseEvent ):void
{
gotoAndStop("down");
}
this.addEventListener(MouseEvent.MOUSE_UP, onButtonUp);
function onButtonUp( event:MouseEvent ):void
{
gotoAndStop("up");
}
And I have also tried this: (with this version, the over button stays active, and the down nor up will not work)
stop();
this.CommunityCampus.communityUp1.addEventListener(MouseEvent.MOUSE_OVER, this_over);
this.CommunityCampus.communityDown1.addEventListener(MouseEvent.MOUSE_DOWN, this_down);
this.CommunityCampus.communityOver1.addEventListener(MouseEvent.MOUSE_UP, this_over);
this.CommunityCampus.communityUp1.addEventListener(MouseEvent.MOUSE_OUT, this_out);
function this_over(e:MouseEvent):void{
this.gotoAndStop("over");
}
function this_down(e:MouseEvent):void{
this.gotoAndStop("down");
}
function this_out(e:MouseEvent):void{
this.gotoAndStop("up");
}

The code as is should work, so I can only guess the issue.
When you advance the timeline, the buttons you add actions to need to be present at all times. If the timeline advances to somewhere that the button does not exist, the action is removed, when you then go back, where the button exists, you will no longer have that action available.
You can solve this by having the buttons present at all times, and only hiding them as needed, instead of removing them.
see example image, where the button is missing for certain frames, this will cause it to not work

Related

How to access a frame inside a movieclip from within another movieclip

I have a problem regarding movieclips...
On my main timeline there are 2 MCs and inside MC1 there is a button, which - when clicked - should get me to Frame 10 of MC2 (on the main timeline)...
My button code (inside mc1):
btn_standard.addEventListener(MouseEvent.CLICK, standard_click);
function standard_click(myNextEvent:MouseEvent):void {
MovieClip(root).mc2.gotoAndPlay(10);
}
There is no error, but however the button won't work...
Can anyone please help me! :-(
EDIT: Here's a sample file - same problem!
Why don't you add as3 code on main timeline instead of inside of mc1, like this:
mc1.btn_standard.addEventListener(MouseEvent.CLICK, standard_click);
function standard_click(myNextEvent:MouseEvent):void {
mc2.gotoAndPlay(10);
}
This code wont work because in the 1-st frame there is no mc2:
function standard_click(myNextEvent:MouseEvent):void {
MovieClip(root).mc2.gotoAndStop(10);
}
Dispatch any event from this MovieClip after mouse click:
function standard_click(myNextEvent:MouseEvent):void {
dispatchEvent(new Event(Event.COMPLETE));
}
Add next code to the first frame of the main timeline:
import flash.events.Event;
stop();
mc1.addEventListener(Event.COMPLETE, onComplete);
function onComplete(event:Event):void
{
gotoAndStop(10);
mc2.gotoAndStop(10);
}

Stop Drag, Add Up Release for a separate function

I have a movie clip where I've placed start drag and stop drag actions, and I'd like to activate a button action (run a movie clip named person_mo) with the ReleaseToDrop function. However, I cannot code it correctly. Here is the AS I have for the drag:
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
SCOPE2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
SCOPE2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
SCOPE2.stopDrag();
}
How can I play the person_mo movie clip and stop the drag?
After I tested your code, your stopDrag function is working well. You just need to fire your event/function/ command when you fire the ReleaseToDrop function.
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
SCOPE2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
SCOPE2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
SCOPE2.stopDrag();
person_mo.play(); // if you want to run the movie clip
yourFunction(); //or else if you want a function to fire, simply add yourFunction which fires the button action.
}
I hope this helps.

Proceeding in Movieclip After Animation Finishes AS3

I have a Movieclip on my stage which contains two buttons: Back and Next. Also on the timeline, I have another Movieclip which contains an animation. After the next button is clicked, I'd like to have the animation transition into the next animation without jumping. So, is there any way I can listen for the animation to be finished so that I can time the transitions seamlessly?
Here is the code for my Next and Back buttons (which are movieclip buttons which is why there is all of the extra code) which just switch between frames as of now:
//NEXT BUTTON
nextBtn.buttonMode = true;
nextBtn.addEventListener(MouseEvent.ROLL_OVER, nextBtnOver);
nextBtn.addEventListener(MouseEvent.ROLL_OUT, nextBtnOut);
nextBtn.addEventListener(MouseEvent.MOUSE_DOWN, nextBtnDown);
nextBtn.addEventListener(MouseEvent.MOUSE_UP, nextBtnUp);
function nextBtnOver(e:MouseEvent):void
{
nextBtn.gotoAndPlay("over");
}
function nextBtnOut(e:MouseEvent):void
{
nextBtn.gotoAndPlay(9- (nextBtn.currentFrame-1));
}
function nextBtnDown (e:MouseEvent):void
{
nextBtn.gotoAndPlay("down");
}
function nextBtnUp (e:MouseEvent):void
{
nextBtn.gotoAndPlay(5);
MovieClip(root).nextFrame();
}
//BACK BUTTON
backBtn.buttonMode = true;
backBtn.addEventListener(MouseEvent.ROLL_OVER, backBtnOver);
backBtn.addEventListener(MouseEvent.ROLL_OUT, backBtnOut);
backBtn.addEventListener(MouseEvent.MOUSE_DOWN, backBtnDown);
backBtn.addEventListener(MouseEvent.MOUSE_UP, backBtnUp);
function backBtnOver(e:MouseEvent):void
{
backBtn.gotoAndPlay("over");
}
function backBtnOut(e:MouseEvent):void
{
backBtn.gotoAndPlay(9- (backBtn.currentFrame-1));
}
function backBtnDown (e:MouseEvent):void
{
backBtn.gotoAndPlay("down");
}
function backBtnUp (e:MouseEvent):void
{
backBtn.gotoAndPlay(5);
MovieClip(root).prevFrame();
}
Thanks, any help is appreciated.
If your animations are timelined, call an animationComplete function in the last frame of your animation movieClip. This function would live at the same scope as your posted code above, so you just have to be able to path to it properly from within the animation clip. For instance, if the code is one level outside the animation movieClip (the mc's parent) then you might call this.parent.animationComplete(), which would contain the code you want to execute when the animation finishes.
According to your comment,
I tried this, and it works - except in order for
it to advance to the next point you have
to click the next button at exactly the right frame.
I'd like to make it so you can click it at any point in the Movieclip, and it
will play the remainder of the Movieclip before proceeding
to the next frame. Here's my code for what I've added: function
nextBtnUp(e:MouseEvent):void {
nextBtn.gotoAndPlay(5); if (MovieClip(root).animationTest.currentFrame ==
MovieClip(root).animationTest.totalFrames) MovieClip(root).nextFrame();
}
Thank you for the help so far
You have it most of it figured out, so to finish can add stop() in the code at the last frame of your MovieClips, or you can do this (the more complex version):
yourClip.yourFirstClip.addEventListener(Event.ENTER_FRAME, stopAtLastFrame);
yourClip.yourSecondClip.addEventListener(Event.ENTER_FRAME, stopAtLastFrame);
function stopAtLastFrame(e:Event):void
{
if(e.currentTarget.currentFrame === e.currentTarget.totalFrames)
// ^Note that no conversion is being made, so you can have 3 equal signs
{
e.currentTarget.stop();
}
}

prevent next scene frame on movieclip mouseclick

I have a scene with a few objects as movieclips which can be clicked one at a time.
What happens is that I'm able to click every object and on click the scene switches to the next frame.
How do I change that?
Basically I have a key and a door, both movieclips.
You can collect the key, it disappears and after that you are able to click the door to open it.
What actually happens is you are both able to click the key and the door.
When you click the key, it's working as intended, but when you click the door, the key still disappears. This is much more annoying with more than 2 objects.
code for the key:
addEventListener(MouseEvent.CLICK, CollectKey);
function CollectKey(event: MouseEvent): void
{
this.visible = false;
// door
MovieClip(root).door.addEventListener(MouseEvent.CLICK, MovieClip(root).FinishGame);
}
code for the door:
stop();
function FinishGame(event: MouseEvent): void
{
if(MovieClip(root).currentFrame == 4)
{
nextFrame();
}
}
http://www.wuala.com/sollniss/stuff/Untitled-2.swf/
http://www.wuala.com/sollniss/stuff/Untitled-2.fla/
EDIT
After looking at your .fla, I can see your issue:
On your first frame, you have the following script:
stop();
addEventListener(MouseEvent.CLICK, StartGame);
function StartGame(event: MouseEvent): void
{
nextFrame();
}
You likely aren't aware that the mouse click listener you add there, doesn't go away until you tell it to (even if the frame changes). That's why every click calls next frame.
To remedy this, simply remove the listening before you move on to the next frame:
function StartGame(event: MouseEvent): void
{
removeEventListener(MouseEvent.CLICK, StartGame);
nextFrame();
}
And, may be that only visible false is not enough, you also need to set enabled = false and mouseEnabled = false for the key element, because without it, it will keep hearing the click event.

Is it possible to SIMULATE a mouse click using a mouse down and up events?

A previous question has gotten me thinking. Is it possible to simulate a MouseEvent.CLICK to get fired by just firing first a MOUSE_DOWN followed by a MOUSE_UP.
As per Adobe's doumentation.
"... For a click event to occur, it must always follow this series of events in the order of occurrence: mouseDown event, then mouseUp. The target object must be identical for both of these events; otherwise the click event does not occur. Any number of other mouse events can occur at any time between the mouseDown or mouseUp events; the click event still occurs." http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html#event:click
From my tests it shows that the CLICK event is NOT constructed from the ActionScript 3 event queue. Or is something wrong with the code?
See:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(backgroundColor="0xFFFFFF", frameRate="30", width="800", height="600")]
public class Test extends Sprite
{
private var spr:Sprite = new Sprite();
public function Test()
{
trace("Test()");
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
public function init(e:Event):void
{
trace("init()");
spr.graphics.beginFill(0xFF0000);
spr.graphics.drawRect(0,0,200,80);
spr.graphics.endFill();
addChild(spr);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
spr.addEventListener(MouseEvent.CLICK, onClick);
}
private var tick:int = 0;
private function onEnterFrame(e:Event):void
{
if (tick == 1) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN,true,false));
if (tick == 2) spr.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP,true,false));
if (tick == 3) spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true,false));
tick++;
}
private function onClick(e:MouseEvent):void
{
trace("onClick() MouseEvent.CLICK dispatched");
}
}
}
I should get TWO 'onClick()' events instead of one.
The reason you cannot is that all three types are created by the plugin, and are not dependent upon each other.
MouseEvent.MOUSE_DOWN is fired as soon as you press on an interactive object.
MouseEvent.MOUSE_UP is fired as soon as you release the mouse, it does not depend upon the mouse click having started a MOUSE_DOWN within the same InteractiveObject.
MouseEvent.CLICK will only be fired when both events take place in the same object without the cursor leaving the object between the itnerval of mouse down and mouse up.
So you can see that there is a simple case, or two even, in which both MOUSE_DOWN and MOUSE_UP are fired, but CLICK is not called because the event is not a CLICK.
Furthermore, the ability to simply dispatch a MouseEvent.CLICK event makes it unnecesasry to create fake user interaction by firing multiple mouse events, when only one is called for. It would be awkward to have to track each object clicked in the mouse down listener, in order to check that the moue up should also trigger a click. You would also need to remove references when a ROLL_OUT event happened.
In short, the CLICK event is really a lot more than the MOUSE_DOWN and MOUSE_UP events. It is also managing that the events happened in succession and on the same display object without leaving the object. When that happens, and ONLY when that happens flash dispatches a CLICK event.
it's impossible, this code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
/**
* ...
* #author www0z0k
*/
public class Main extends Sprite {
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
}
private function onStageKeyDown(e:KeyboardEvent):void {
trace('pressed');
stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
stage.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
}
private function onClick(e:MouseEvent):void{
trace('clicked');
}
}
}
outputs just:
pressed
clicked
after a key is pressed on the keyboard
not sure about this for flash. But when i was learning windows MFC and was clueless. What i did was that I sent WM_MOUSEDOWN followed by WM_MOUSEUP after a short delay. It did work and the button's visual states also changed - without any mouse click!
If I understand what you're asking then the answer is YES.
I am curious if I have over simplified the solution here especially after reading your code attempt. It is very simple to "simulate" a mouse click. The code below outputs two "I have been clicked" statements when the object is clicked and none when you mouse down then move out of the objects boundaries.
This class extended a MovieClip I created on the stage so I didn't have to code a box.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class clickMe extends MovieClip {
public function clickMe() {
// constructor code
this.addEventListener(MouseEvent.CLICK, clicked);
this.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
}
private function clicked(e:MouseEvent):void{
trace("I have been clicked");
this.removeEventListener(MouseEvent.MOUSE_UP, clicked);
}
private function mDown(e:MouseEvent):void{
this.addEventListener(MouseEvent.MOUSE_UP, clicked);
}
}
}
In your code I believe the problem is that you are trying to run event listener dispatches through an "onEnterFrame" which will only ever fire one event per frame - never two. Also after testing your code I'm not sure what your were trying to accomplish with the "tick" variable because you never reset it. So the the MOUSE_UP event fires once (when tick = 2, unless your holding the mouse down) then none of the other dispatches will ever fire again.
Not quite sure why everybody's making these complex spikes. Or maybe I'm just overseeing something.
Anyway, here's a very easy way to check it:
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.CLICK, function(){
trace( 'clicked' );
} );
stage.dispatchEvent( new MouseEvent( MouseEvent.MOUSE_DOWN ) );
stage.dispatchEvent( new MouseEvent( MouseEvent.MOUSE_UP ) );
And the answer would be, "no, it's not possible"...
...and probably should be followed by a counter-question: WHY would I want to simulate a click event using a sequence of mousedown and mouseup, when I can dispatch a click event just as well?
Am I a button, having an existential crisis?
Or is it a question out of mere curiosity? But you know what happened to the cat... Right, but maybe it's schrödinger's cat, which means that at the end of this completely irrelevant train of thought, curiosity REALLY did and did NOT kill the cat.
Except if the cat's out of a box.
Then it probably just got wasted.
See, thinking out of the box can get you killed.