views:

248

answers:

2

HELP! How would I fix this Actionscript 3.0 so that it creates a Dynamic Page title for each page? I am new to Actionscript 3.0 and I am trying to generate a page title using Dynamic Text for each page. I have four pages in my site, and here is the actionscript that I have so far:

stop ()
function createTitle (whichpage:String) {
pageTitle.text = whichpage;
};
var page:String="theres no place like home";
createTitle (page);

function btn1Press (MouseEvent) {
gotoAndStop (1);
}

function btn2Press (MouseEvent) {
gotoAndStop (10);
}

function btn3Press (MouseEvent) {
gotoAndStop (20);
}

function btn4Press (MouseEvent) {
gotoAndStop (30);
}

btn1Press.addEventListener (MouseEvent.CLICK, Home);
btn2Press.addEventListener (MouseEvent.CLICK, Services);
btn3Press.addEventListener (MouseEvent.CLICK, About);
btn4Press.addEventListener (MouseEvent.CLICK, Contact);

stop();


1,10,20,30 represent the frames that each page is on

The four navigation buttons are setup as follows:

btn1Press = Home Page (Instance name "Home")
btn2Press = Services Page (Instance name "Services")
btn3Press= About Page (Instance name "About")
btn4Press=Contact Page (Instance name "Contact")

I already have the dynamic text box set up. Basically I need each of the buttons to go to their respective pages, and I need the text in the dynamic text box to change depending on which page it is. Here is the additional actionscript that I have at frames 10,20 and 30 (services,about,and contact)

page = "services"; createTitle (page); stop ();

page = "about"; createTitle (page); stop ();

page = "contact"; createTitle (page); stop ();

I am getting really frustrated with this, and any help would be SO awesome!

I keep getting error #1061 about undefined functions

So I think a better way to ask my question would be:

I have a four page site in Flash CS4, Page 1 starts on Frame 1, Page 2 starts on Frame 10, Page 3 starts on Frame 20 and Page 4 starts on Frame 30. There are Four navigation buttons that I have converted to button symbols. What should I call each button, and what should I call the instance of each button? What code do I need to put in the actions panel in order to get the navigation buttons to function properly, and make the dynamic text box display a title specific to the page that it is on ("Welcome to the Home Page", "Welcome to the About Page", etc)

Thanks for your help + patience, I am finding this all pretty confusing. :(

A: 

I think the biggest problem is that you're attaching event listeners to functions, instead of functions-as-event-handlers to buttons. Those lines near the end of your first block should probably be like this:

homeBtn.addEventListener (MouseEvent.CLICK, btn1Press);

Or instead, there's a property page within Flash on buttons where you can attach a "Click" handler for that button.

You should try Flex Builder. What you're building here really feels like something that should be built in Flex rather than Flash. Flex has a lot of the pretty transitions and such, and it's easy to build something in Flash Professional and drop that into a Flex project. This will fix much of your style and organization problems.

There are many other flaws in this code, not all of which are fatal, just ugly:

  • In AS, like JS, semicolons at the end of statements are optional, but it's still a good idea to add them. (Line 1, "stop()".)

  • You've reinvented 1980's style BASIC here, complete with "GOTO LINE". Switching to Flex is one way to improve your code style, since Flex Builder enforces many good style rules. If you have to stick with Flash Professional only, there are probably Flash design patterns to avoid this in that environment, too. Get a book on AS3-heavy Flash development. I'm sure you'll find lots of good style advice there.

  • Ditto with the numbered controls, only that brings you up to beginner-class VisualBasic. btn1Press(), for example, should be called something like homeButtonPressed(). A year from now, you'll have to go clicking all around the Flash development environment to figure out what "btn1" is. If you call the button and the functions it calls by their meaning, you won't have this problem.

Warren Young
A: 

Sounds like you want to dynamically change the HTML title on a page from within Flex when switching views in Flex. The only way to do this is via javascript called from within Flex. It would go something like this:

FLEX

public function setTitle(title:String):void {
if (ExternalInterface.available) {
 ExternalInterface.call("setTitle", title);
}
}

JAVASCRIPT

    function setTitle(t){
document.title.innerText = t;
}
Bernesto