How to access a frame inside a movieclip from within another movieclip - actionscript-3

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

Related

Flash CS6, AS3 button issue

I'm using the code below to have button "legalBtn" access the layer and instance name of "legalOverlay." Then after the legalese has been read, the enduser can close the "legalOverlay" via "closeBtn" and return to the last frame of the banner ad. The code below has no compiler errors, but the "legalBtn" is still not working. Any ideas?
import flash.events.MouseEvent;
function init(){
legalBtn.addEventListener(MouseEvent.CLICK, legalClick);
legalBtn.visible=true;
legalOverlay.clickthru.addEventListener(MouseEvent.CLICK);
legalOverlay.clickthru.buttonMode=true;
legalOverlay.closeBtn.addEventListener(MouseEvent.CLICK);
legalOverlay.closeBtn.buttonMode=true;
legalOverlay.visible=false;
}
function legalClick(e:MouseEvent){
if(legalOverlay.visible==true){
legalOverlay.visible=false;
} else {
legalOverlay.visible=true;
}
}
stop();
You have to call the init() function to add the click event listener on your legalBtn object and make it visible.

AS3 movie clip button is not working

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

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

Actionscript 3 how to delete instance from stage

So I have two movieclips, sRP_mc and dP_mc, on the first frame. Now, when either of the movieclips are clicked on, then I want the movieclip to be removed from the stage and then I want the frame to change (I want the movie to go to frame 5). On frame 5, there is a close button, which, if clicked, takes you back to frame 1 (but when it takes you back to frame 1, I want the movieclip which was clicked on to not be there anymore). Here is my code for my first frame (frame 1).
import flash.events.MouseEvent;
stop();
if (sRP_mc.visible == true) {
sRP_mc.addEventListener(MouseEvent.CLICK, sRPClicked);
function sRPClicked(event:MouseEvent):void {
sRP_mc.removeEventListener(MouseEvent.CLICK, sRPClicked);
removeChild(sRP_mc);
gotoAndPlay(5);
}
}
if (dP_mc.visible == true) {
dP_mc.addEventListener(MouseEvent.CLICK, dPClicked);
function dPClicked(event:MouseEvent):void {
dP_mc.removeEventListener(MouseEvent.CLICK, dPClicked);
removeChild(dP_mc);
gotoAndPlay(10);
}
}
and on frame 5, there is a close button and the code is this.
import flash.events.MouseEvent;
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeScreen);
function closeScreen(event:MouseEvent):void {
gotoAndStop(1);
}
and on frame 10 there is also a close button the code is this.
import flash.events.MouseEvent;
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeScreen2);
function closeScreen2(event:MouseEvent):void {
gotoAndStop(1);
}
As you can see, if sRP_mc or dP_mc are removed using the removeChild method, then sRP_mc and dP_mc should not be visible (.visible != true) but when I play this, it says that sRP_mc and dP_mc are always visible and the child does not get removed completely from the stage (or I think the isntance keeps coming back whenever I go back to frame 1). Why is it doing this and how would I fix it?
When the object is removed from the stage the visible property isn't changed to false unless you do so manually. Here's a better to way to check if an item isn't on the stage (I also cleaned up your inline function):
if (sRP_mc.stage != null) {
sRP_mc.addEventListener(MouseEvent.CLICK, sRPClicked);
}
function sRPClicked(event:MouseEvent):void {
sRP_mc.removeEventListener(MouseEvent.CLICK, sRPClicked);
removeChild(sRP_mc);
gotoAndPlay(5);
}
If an object is removed from the stage its stage property is set to null. Hopefully this helps!

Flash CS6 Error #1009 - Button disappears

I'm starting to use Flash CS6 for the first time to try and make Scaleform UI's for UDK. I'm following this simple tutorial: http://goo.gl/yedMU. I've followed it to the letter but can't seem to get it to work. I even have tried it again in a new project but it ends up with the same error. I've triple checked each name and instance but it just refuses to work. Here is the really simple code of the two frames in the file:
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.MovieClip;
subMenu_btn.addEventListener(MouseEvent.CLICK, subMenu);
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
var cursor:cursor_mc = new cursor_mc();
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
stop();
function subMenu(event:MouseEvent):void
{
gotoAndStop('Sub Menu');
}
function exitGame(event:MouseEvent):void
{
fscommand('ExitGame');
}
and
play_btn.addEventListener(MouseEvent.CLICK, playGame);
back_btn.addEventListener(MouseEvent.CLICK, backBtn);
function playGame(event:MouseEvent):void
{
fscommand('PlayMap');
}
function backBtn(event:MouseEvent):void
{
gotoAndStop('Main Menu');
}
I used the debugger and the code breaks at
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
Any ideas? The whole thing works until I used the 'Back' button to go back to the first frame, when the 'Exit' button is gone and I get that error. The 'Submenu' button remains however and the menu is still operable.
This is the error using the debugger:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Menu_fla::MainTimeline/frame1()[Menu_fla.MainTimeline::frame1:6]
at flash.display::MovieClip/gotoAndStop()
at Menu_fla::MainTimeline/backBtn()[Menu_fla.MainTimeline::frame2:10]
Okay, so I might have been wrong in what I said in the comments above, the solution is this:
For your first frame's AS3:
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.display.MovieClip;
var cursor:cursor_mc = new cursor_mc();
subMenu_btn.addEventListener(MouseEvent.CLICK, subMenu);
exit_btn.addEventListener(MouseEvent.CLICK, exitGame);
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
Mouse.hide();
stop();
function subMenu(event:MouseEvent):void
{
subMenu_btn.removeEventListener(MouseEvent.CLICK, subMenu);
exit_btn.removeEventListener(MouseEvent.CLICK, exitGame);
removeChild(cursor);
gotoAndStop('Sub Menu');
}
function exitGame(event:MouseEvent):void
{
fscommand('ExitGame');
}
For your second frame's AS3:
play_btn.addEventListener(MouseEvent.CLICK, playGame);
back_btn.addEventListener(MouseEvent.CLICK, backBtn);
cursor = new cursor_mc();
addChild(cursor);
cursor.x = mouseX;
cursor.y = mouseY;
cursor.startDrag();
Mouse.hide();
function playGame(event:MouseEvent):void
{
fscommand('PlayMap');
}
function backBtn(event:MouseEvent):void
{
removeChild(cursor);
gotoAndStop('Main Menu');
}
You were instantiating the cursor each time you hit frame 1, which I believe would create a namespace problem. The solution was to remove the cursor from the stage, and add it back in for each frame. There may be a more elegant solution, but as I never use multiple frames with AS (for this very reason), this is the best I could do. I also hid the mouse cursor to give your cursor_mc more focus.
Let me know if you have any other questions.
Happy coding!
Another solution would be to create a new layer, and put the cursor code on that layer only. That layer would have one keyframe, and extend to cover the entire timeline, so it is always alive.
Or, create a menu movie clip that exists on frame 1. And inside that movie clip, have your different frames for each part of the menu (options, etc.). And the cursor would exist at the same level as the menu movie clip. So it always exists and is only initialized once.