• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Multiple buttons with AS3

Explorer ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

Hi,

Please help... I'm working on a slide show which I would like work  automatically (fade in and out) as well as being able to be prompted to  switch via buttons... I know this is a super simple thing, but I cannot  get all the buttons to work. The buttons are copies of the same instance  with different instance names and are located at the 3 respective  frames in timeline where the pieces of slideshow should fade in/out ( I  hope this makes sense) I get the first set of buttons to work:

import flash.events.MouseEvent;

controls.bttn1.addEventListener (MouseEvent.CLICK, clickSection1);
function clickSection1 (evtObj:MouseEvent) {
     // trace shows what's happening in the output window
     trace ("The Button 1 was clicked!")
     gotoAndPlay("bttn1");
}

controls.bttn2.addEventListener (MouseEvent.CLICK, clickSection2);
function clickSection2 (evtObj:MouseEvent) {
     // trace shows what's happening in the output window
     trace ("The Button 2 was clicked!")
     gotoAndPlay("bttn2");
}

controls.bttn3.addEventListener (MouseEvent.CLICK, clickSection3);
function clickSection3 (evtObj:MouseEvent) {
     // trace shows what's happening in the output window
     trace ("The Button 3 was clicked!")
     gotoAndPlay("bttn3");
}

but when I add more buttons that are located later on in a timeline, I  get the following error:

TypeError: Error #1009: Cannot access a property or method of a null  object reference.
     at INDEX_TEST_r3_fla::MainTimeline/frame1()

I hope this makes sense - I'd REALLY appreciate any help

Thank you!

TOPICS
ActionScript

Views

4.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

because the objects have to be in the frame where the code is executed.

Put them all in one frame and use the visible property.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

I'm sorry - i'm a designer and am really horrible with AS.. I tried having the code for the 2nd section of the button on the same frame as the 2nd section and It didn't work... I'm not sure what you mean by: "visible property." Could you possibly show me how it should work with my code?

Thank you ahead of time!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

That first response sh/could have said to either have the code where the buttons are (which you tried), or have the buttons all in frame 1 and make the ones you want visible/invisible when you reach particular frames (such as using....controls.bttn4.visible = false;... etc)

Placing the code where the buttons are should work, so you might need to explain a little more about what you tried.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

Thank you very much for your response...


I have the buttons on frame 1, 300 and 900  - where the pieces of slideshow connect..


this is the code I have on the first frame:

controls.bttn1.addEventListener (MouseEvent.CLICK, clickSection1);
function clickSection1 (evtObj:MouseEvent) {
    // trace shows what's happening in the output window
    trace ("The Button 1 was clicked!")
    gotoAndPlay("bttn1");
}

controls.bttn2.addEventListener (MouseEvent.CLICK, clickSection2);
function clickSection2 (evtObj:MouseEvent) {
    // trace shows what's happening in the output window
    trace ("The Button 2 was clicked!")
    gotoAndPlay("bttn2");
}

controls.bttn3.addEventListener (MouseEvent.CLICK, clickSection3);
function clickSection3 (evtObj:MouseEvent) {
    // trace shows what's happening in the output window
    trace ("The Button 3 was clicked!")
    gotoAndPlay("bttn3");
}

and it works fine for the first set of buttons. When I click on button 2 - which takes me to frame 300, I get this error and the slide show gets stuck:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at INDEX_TEST_r3_fla::MainTimeline/frame300()
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at flash.display::MovieClip/gotoAndPlay()
    at INDEX_TEST_r3_fla::MainTimeline/clickSection2()

I hope this makes sense... Thank you!


Lana

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 12, 2010 Jul 12, 2010

Copy link to clipboard

Copied

Chances are you are missing an instance name somewhere in frame 300.  Go into your Publish Settings and select the option to Permit Debugging.  The error message should then include a specific line number just after where it says " at INDEX_TEST_r3_fla::MainTimeline/frame300 "... shown as "frame300:2" for example (frame 300 line 2).

Whichever object is being targeted on the line indicated is not being seen by the code... either the instance name isn't assigned or it is somehow otherwise compromised.  (Someday it would be nice if they could include the offending object/name in the message... it's able to see the name is an issue, but doesn't bother offering what it is in the error)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 13, 2010 Jul 13, 2010

Copy link to clipboard

Copied

Thank you... but it still doesn't work.. I tried changing both instance names... and making sure I reference it correctly... but it still doesn't work... here's the error (I have debugging turned on):

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at INDEX_TEST_r3_fla::MainTimeline/frame300()[INDEX_TEST_r3_fla.MainTimeline::frame300:1]
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()
    at flash.display::MovieClip/gotoAndPlay()
    at INDEX_TEST_r3_fla::MainTimeline/clickSection2()[INDEX_TEST_r3_fla.MainTimeline::frame1:12]

could you may be possibly tell me how to do the whole thing from the first frame?? This is such a simple thing and could be done in 2 min with AS2...  but I need to use AS3 since I'm using the 3D rotation... Thank you!


Lana

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 13, 2010 Jul 13, 2010

Copy link to clipboard

Copied

To help you fix it as is, you'll have to describe how you have the objects designed into the timeline.  Based on the error message whatever is in the first line of frame 300 is the problem.  You can try using trace function calls to find out which object is coming up null... like if line 1 is ...

controls.bttn1.addEventListener (MouseEvent.CLICK, clickSection1);

then you can use trace(controls); and see if that produces a null result, and then trace(controls.bttn1); and see how that turns out.

You might try placing the buttons in frame 300 on a separate layer from the frame 1 buttons, just to insure they are inheriting properties from the predecessor objects.

For the all in frame 1 approach you need to have all the buttons in frame 1 of a layer and all coded in frame 1 as well, and extend the layers thru the full length of the timeline.  In frame 1 you turn any you don't intend to see invisible by setting their visible property to false (as in controls.bttn1.visible = false).   Then, when you get to frame 300 you turn them visible or make the others invisible.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 13, 2010 Jul 13, 2010

Copy link to clipboard

Copied

Once again thank you very much...


I will try that...I do want the buttons to change appearance when the frames come up (on 1, 300 and 900, sort of like this: http://www.wireless.att.com/cell-phone-service/welcome/index.jsp) that's why I had it on thouse frames but I figured may I'll try it using a movie clip. The problem I ran into now is that apparently there's no more _root???   Could yu tell me what is the equivalent of _root in AS3? This is the code I'm using:

stop();

import flash.display.MovieClip;
import flash.events.MouseEvent;

this.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler);
this.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler);
this.addEventListener(MouseEvent.CLICK, onClickHandler);
this.addEventListener(MouseEvent.MOUSE_DOWN, onPressHandler);
this.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);
// if you want a hand cursor
this.buttonMode = true;
this.useHandCursor = true;
function onRollOverHandler(myEvent:MouseEvent) {
    trace("On");
    this.gotoAndPlay("one");
}
function onRollOutHandler(myEvent:MouseEvent) {
    trace("Out");
    this.gotoAndPlay("two");

   
    // myClip.stop();
}
function onClickHandler(myEvent:MouseEvent) {
    trace("I waited for Press AND Release!!!");
    _root.gotoAndPlay("bttn2");
}
function onPressHandler(myEvent:MouseEvent) {
    trace("Press");
}
function onReleaseHandler(myEvent:MouseEvent) {
    trace("Release");
    this.gotoAndPlay("four");
}

Thanks!!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 13, 2010 Jul 13, 2010

Copy link to clipboard

Copied

LATEST

Thank you very much for your help - I figured it out!!!  I used this instead of _root:

MovieClip(parent.parent.parent.parent).gotoAndStop(3);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines