PDA

View Full Version : AS3 and button functions


damonlee
05-07-2007, 08:08 PM
I'm very new to AS3, of course, like a total noob, I've tried to format my button functions in the same manner as AS2.

I've figured out some how to implement mouse events in AS3, but it seems very rudundant; I have to use "addEventListener" each time i want the same button to execute a function???

Here is my code:

this.hsAboutBook.addEventListener(MouseEvent.MOUSE _OVER, btnAboutBookHighlight);
this.hsAboutBook.addEventListener(MouseEvent.MOUSE _OUT, btnAboutBookNormal);
this.hsAboutBook.addEventListener(MouseEvent.MOUSE _DOWN, openAboutBook);



this.hsRead.addEventListener(MouseEvent.MOUSE_OVER , btnReadHighlight)
this.hsRead.addEventListener(MouseEvent.MOUSE_OUT, btnReadNormal);



//// FUNCTIONS////

function btnAboutBookHighlight(event:MouseEvent):void {
this.btnAboutBook.gotoAndPlay(2);

}

function btnAboutBookNormal(event:MouseEvent):void {
this.btnAboutBook.gotoAndPlay(11);

}

function openAboutBook(event:MouseEvent):void{
getURL("aboutBook.html")

}


function btnReadHighlight(event:MouseEvent):void {
this.btnRead.gotoAndPlay(2);

}



This code actually works UNTIL I add the "getURL" statement; which causes my movie clips to loop...

Any help with condensing this code would be greatly appreciated...thank you
-D

Krilnon
05-07-2007, 08:27 PM
Use navigateToURL (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/package.html#navigateToURL())?

Dazzer
05-08-2007, 01:08 AM
you could create a class.


package
{
public class customButton extends Sprite
{
myURL:String;
public customButton(url:String):void
{
myURL = url;
//I should be associated with a button, or draw your own here using graphics class
this.addEventListener(blah blah) //Insert your listeners
}
private function myListeners(e:MouseEvent):void
{
//Do whatever here. Navigate to myURL or something.
}

}
}

pete66
05-08-2007, 08:24 AM
I'm just trying to follow along here, and I don't understand. Where in the class would (for example) the line this.btnAboutBook.gotoAndPlay(11); go?

Flashchump
05-08-2007, 10:29 AM
It will go in the handler function
this.addEventListener(event, handler)
function handler()

Also, if you plan on using gotoAndPlay(), your class should extends a MovieClip class, not a Sprite class.

I'm just trying to follow along here, and I don't understand. Where in the class would (for example) the line this.btnAboutBook.gotoAndPlay(11); go?

Dazzer
05-08-2007, 09:16 PM
where it says

//Do Whatever Here. Navigate to url or GOTO AND PLAY or something

damonlee
05-09-2007, 09:24 PM
you could create a class.


package
{
public class customButton extends Sprite
{
myURL:String;
public customButton(url:String):void
{
myURL = url;
//I should be associated with a button, or draw your own here using graphics class
this.addEventListener(blah blah) //Insert your listeners
}
private function myListeners(e:MouseEvent):void
{
//Do whatever here. Navigate to myURL or something.
}

}
}


This is what I tried:
package
{
public class customButton extends Sprite
{
myURL:String;
public customButton(url:String):void
{
myURL = "buyBook.html";
//I should be associated with a button, or draw your own here using graphics class
this.hsBook.addEventListener("click",myListeners) //Insert your listeners
}
private function myListeners(MouseEvent):void
{
navigateToURL(new URLRequest(myURL))
this.buyBookSub.gotoAndPlay(2);

}

}
}



I'm getting compile errors
Ok, so I'm totally new to classes, but I appreciate your response, any ideas what i'm doing wrong?

Derlei
05-10-2007, 02:53 AM
like Flashchump said, if you plan on using gotoAndPlay(), your class should extends a MovieClip class, not a Sprite class.(-: