FORUMS: list search recent posts

Problems scripting button links in Flash CS4/AS3.0

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Megan Kenal
Problems scripting button links in Flash CS4/AS3.0
on Jul 3, 2012 at 9:36:34 pm

Hi all. First let me just get this out there: I'm new to Flash, and to scripting in general (my programming language knowledge extends as far as HTML and some CSS, and that's about it...if you can even call that programming). I'm redoing my website in Flash in an effort to teach myself Flash/ActionScript, since I learn best by doing. Four days and countless tutorial videos later, I think I have the basic hang of it...except now I've hit a major roadblock that I can't find.

A little background about my .fla: my movie comprises a single scene, with a total of six "pages" that folks can visit by clicking buttons; each button is a single line of text that I converted to a symbol of the "button" type. Each "page" has a frame label: "retHome" (for the main page), "retProse" (for my short stories), "retPoetry" (for my poems), "retEssays" (self-explanatory), "retArtwork" (photography), and "retContact" (self-explanatory). Each button instance has been appropriately labeled; for example, on the main "page" the buttons are labeled proseBtn, poetryBtn, essaysBtn, artworkBtn, and contactBtn (there actually is a 6th called designBtn that will link to an external website of mine, but I'm focusing just on internal links right now).

What I want to have happen is this: visitor opens website. Movie starts playing, then stops upon reaching final frame for main "page." Visitor clicks a button, movie moves to appropriate frame, and begins playing again from that frame on until final frame of ~that~ "page," etc. Back buttons on each "page" allow user to return to main "page" to visit another section.

The final frame of the main section is 168, so I've inserted the following code there:


stop();

//---home page buttons---

proseBtn.addEventListener(MouseEvent.CLICK, goProse);

function goProse(event:MouseEvent):void{
gotoAndPlay("retProse");
}

poetryBtn.addEventListener(MouseEvent.CLICK, goPoetry);

function goPoetry(event:MouseEvent):void{
gotoAndPlay("retPoetry");
}

essaysBtn.addEventListener(MouseEvent.CLICK, goEssays);

function goEssays(event:MouseEvent):void{
gotoAndPlay("retEssays");
}

artworkBtn.addEventListener(MouseEvent.CLICK, goArtwork);

function goArtwork(event:MouseEvent):void{
gotoAndPlay("retArtwork");
}

contactBtn.addEventListener(MouseEvent.CLICK, goContact);

function goContact(event:MouseEvent):void{
gotoAndPlay("retContact");
}



The stop(); works perfectly. The goProse function works beautifully. However, absolutely nothing after that works at all. I've tried rearranging the code bits (proseBtn/goProse is the only one that works, and only if it's first), I've tried grouping all the event listeners together and then doing the functions (same problem - the prose link is the only working one)... I don't know what's going on. I've verified I can't even count how many times now that the button instance names are spelled correctly both in the script and in their properties, that the frame labels are spelled correctly both on the timeline and in the script, etc. I've tried rewriting the code chunks by hand instead of copy/paste/change names like I did originally after writing the script for the prose link.

Debug mode shows the error tied to line 11, where the poetryBtn event listener is; that's where it hangs up, it won't progress past that even when I click "continue," and it doesn't throw any other errors. Again it doesn't matter which button is there, that's where it throws the error, unless I move proseBtn so it isn't first, in which case the error then throws at line 5. Here is the error when I test the movie:

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


And here it is when I run it in Debug mode:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main_fla::MainTimeline/frame168()[main_fla.MainTimeline::frame168:11]


I'm tearing my hair out here folks!! Where have I gone wrong?!! Is there some limitation with links/buttons that I'm just missing here??


Return to posts index

Megan Kenal
Re: Problems scripting button links in Flash CS4/AS3.0
on Jul 3, 2012 at 9:39:21 pm

I should probably also mention that if I take out the code for all the buttons except proseBtn/goProse, the movie works and throws no errors whatsoever.


Return to posts index

demetri tashie
Re: Problems scripting button links in Flash CS4/AS3.0
on Jul 11, 2012 at 2:32:34 pm

the short answer is: you can't name something in code that is not yet available on that frame. actionscript can not see into future frames like that.

the long answer is this:

this is an inherent problem in coding timeline based naviagation, and one of the reasons why it has fallen out of use. that being said, it can of course be done, but with some extra work.

probably the easiest way is to break up the code, and place the relevant pieces on whichever and however many different frames as needed. if your 'proseBtn' appears on frame 100,then the code for that button would also be on frame 100. if your 'poetryBtn' is on frame 150, then the code for that would be on frame 150. doing this carefully can and will make it work. HOWEVER, having code scattered all over the place is seen as being bad form and coding practice ( imagine coming back to it in a few months, or handing it off to someone else to try to work on - it could quickly become overwhelming).

so, a step up from that is to keep all the code in one place( the first frame) and work off of it there. this is where an ENTER_FRAME Event comes into play. attached to the stage, it checks every frame while the timeline is moving ( or 15 times a sceond for a framerate of 15 fps while the timeline is not moving). judiciously used ( add the event for a timeline in motion, and remove it when it is not in motion) it can effectively be used to turn this kind of statement into code:

" if the timeline reached or is passing through frame 100 ( or frame label 'retProse'), then add an event listener to the 'proseBtn' " **

other effective techniques are to use and manipulate the visibility and/or alpha properties. this way everything is actually on the stage at the beginning, and will not throw errors in the coding. the visibility/alpha is truned on and off as needed.

better yet is to abandon the timeline, and make each 'page/section' its own movieclip, or even external swf, which can be added or made visible as needed.

i know that's a lot, and is obviously just a skeletal overview of how to approach this. whichever way you choose, including more advanced methods, you do yourself a favor by thinking through the logic and building a coding strategy around that. in other words, at every stage, you should be able to state what you want happening, and what you don't want happenning. then you can be clear with your coding, and troubleshooting will also be MUCH easier.

if you have specific questions along the way, please ask.

** here is some sample code for illustration:
proseBtn.visible=false;
stage.addEventListener(Event.ENTER_FRAME, checkFrames);
function checkFrames(e:Event):void {

if(currentFrame==100) {
stop();
proseBtn.visible=true;
proseBtn.addEventListener(MouseEvent.CLICK, goProse);
}
}
function goProse(e:MouseEvent):void {

gotoAndStop("retProse");
}


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]