  | | Re:[FLASHmacromedia] Writing a function for navi
gating a site | Re:[FLASHmacromedia] Writing a function for navi
gating a site 2004-04-14 - By peter.ginneberge@(protected)
Back grrrr, darn 'CTRL+S'. Sorry, previous message got sent too early by accident...
I ended up saying that you can either assign a function to an onRelease event or have it point to a function. The latter is handy in case multiple Buttons/MovieClips have to do similar stuff.
Instead of:
button1.onRelease = function (){ trace("hello world"); } button2.onRelease = function(){ trace("hello world"); }
// you can write
var helloFunction:Function = function(){ trace("hello world"); } // button1.onRelease = button2.onRelease = helloFunction;
One important thing to note here is that the scope inside the helloFunction is not the timeline it is written in, but the timeline of the instance invoking the function. In the above case, when button1 is clicked, 'this' inside helloFunction will point to button1. And when button2 is clicked, 'this' will point to button2. So , assuming the code is written in the main timeline, to point to the main timeline you'll have to use this._parent inside helloFunction. In case you have multiple instances calling the same function and you want to do something slighlty different depending on which instance invoked that function, you can use 'if', 'if else' or 'switch' statements to determine which instance invoked the function and act accordingly. For instance with Button /MovieClip instances you can check their instance names, as in the following:
//NAVIGATION FUNCTION var navFunction:Function = function () { // NOTE: scope is instance that has been clicked // main timeline is this._parent var p:MovieClip = this._parent; var name:String = this._name; trace("you pressed: "+name); switch (name) { case "logo_btn" : case "text_home_btn" : p.gotoAndStop("home"); break; case "flights_btn" : case "text_flights_btn" : p.gotoAndStop("flights"); break; case "hotels_btn" : case "text_hotels_btn" : p.gotoAndStop("hotels"); break; case "packages_btn" : case "text_packages_btn" : p.gotoAndStop("packages"); break; case "business_btn" : case "text_business_btn" : p.gotoAndStop("business"); break; case "booking_btn" : case "text_booking_btn" : p.gotoAndStop("booking"); break; case "about_btn" : case "text_about_btn" : p.gotoAndStop("about"); break; } }; //-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- // assign event handler for each navigation instance //-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- logo_btn.onRelease = text_home_btn.onRelease=navFunction; flights_btn.onRelease = text_flights_btn.onRelease=navFunction; hotels_btn.onRelease = text_hotels_btn.onRelease=navFunction; packages_btn.onRelease = text_packages_btn.onRelease=navFunction; business_btn.onRelease = text_business_btn.onRelease=navFunction; booking_btn.onRelease = text_booking_btn.onRelease=navFunction; about_btn.onRelease = text_about_btn.onRelease=navFunction; // stop();
If you put the above code in the main timeline of the first scene, it will actually work and jump to that specific frame label, even if it's in anther scene. The problem is though, that this only works in the scene the code is in, since in each scene each existing navigation instance (the one in the scene you were in) is overwritten with a new one (the one in the scene you jump to). You'll have to assign the onRelease event handlers for each navigation item (the last 9 lines in the above sample code) in each scene. The navFunction however can be invoked from within any scene without a problem (that's because scenes no longer exist when a flash file is compiled).
In some cases, scenes can be useful, but most of the time you should avoid them . Especially when you have items that are being used across scenes, etc..
In FMX2004 Macromedia introduced 'Forms', which is probably more suitable for the kind of thing you're doing. However, I'm not a big fan of them in their current state. IDE becomes slow and compiling takes forever.
regards, Muzak
marisa_antonaya (14/04/2004 06:42): >Hello, > >I'm trying to write a function to activate the navigation (both icons and text ) of a >website (made 100% with Flash). Each page of the site is a scene with the same >navigation scheme (and the >same instance names for the buttons), and to save code I wanted to create the >function on the first frame of scene 1 ("home") and then call it from the other scenes. >I wrote the function, and the ActionScript checker said it was fine, but the buttons >don't work, even from the "home" scene where the function originates. I'd appreciate >any help with this. Thanks! I'm using MX2004 Pro, BTW. > >Marisa >
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- Server geeks, take heart! If you're sick of wading through animation & graphics instruction for relevant info., then Nate Weiss' Macromedia Flash MX Professional 2004 for Server Geeks is for you. Leave designing to designers and dig into core concepts to create rich user interfaces, online advertising, and more. See www.peachpit.com for more info. Yahoo! Groups Links
<*> To visit your group on the web, go to: http://groups.yahoo.com/group/FLASHmacromedia/
<*> To unsubscribe from this group, send an email to: FLASHmacromedia-unsubscribe@(protected)
<*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
|
|
 |