0

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]
Taslem
  • 65
  • 8
  • Is there an 'exit_btn' on the 'Main Menu' frame? You're telling Flash to listen for something that isn't there. There is a sidebar on the right full of people who have had the same problem as you that you might want to check out. – IAmMearl Aug 08 '12 at 01:46
  • I've looked through every single one of those posts and I didn't find an answer. Here is the project so you can see it: http://www.sendspace.com/file/f8y82w – Taslem Aug 08 '12 at 01:54
  • What I believe is happening is Flash is `NULL`ing out all the MovieClips each time you change a frame; you can see this if you debug and look at this -> exit_btn (its value is null). I will post my advice in the answer section. – IAmMearl Aug 08 '12 at 02:39

2 Answers2

0

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!

IAmMearl
  • 487
  • 2
  • 7
  • Thanks a ton! The funny part is I tried the swf in UDK and it worked fine which was to say the least weird. Great answer though. – Taslem Aug 08 '12 at 12:44
0

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.