adding new buttons with AS3
I've had some help here with this before and maybe it belongs in the AS3 forum but seeing as I'm a n00b I thought I post here.
I have some buttons that I got from this tut http://bit.ly/4XSrr0 I have got two working exactly the way I want but when I try and add new ones to the script the buttons start flashing on and off and they don't link anywhere, I don't have any error messages, just blinking buttons with no rollover state either?
Does any one have any ideas on how they would add new buttons to this script?
Game.as code:
Code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
public class Game extends MovieClip {
private var startButton:JitteryButton;
private var menuButton:JitteryButton;
// CLASS CONSTRUCTOR
public function Game() {
// create the jittery buttons from the simple buttons on the stage
// Button 1
startButton = new JitteryButton(button1);
addChild(startButton);
buttonF(startButton,"link that works ");
// Button 2
menuButton = new JitteryButton(button2);
addChild(menuButton);
buttonF(menuButton,"link that works ");
}
private function buttonF(btn:JitteryButton,urlS:String):void{
btn.urlS=urlS;
btn.addEventListener(MouseEvent.CLICK,buttonClickF);
}
private function buttonClickF(e:MouseEvent){
var btn:MovieClip = MovieClip(e.currentTarget);
trace("button url: "+btn.urlS);
navigateToURL(new URLRequest(btn.urlS), "_self");
}
}
}
Jitter button.as code:
Code:
package {
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.geom.Point;
import flash.media.Sound;
import caurina.transitions.Tweener;
public class JitteryButton extends MovieClip{
public var urlS:String;
private var myButton:MovieClip;
private var staticTimes:int;
private var fuzzMin:int;
private var fuzzMax:int;
private var dmFilter:DisplacementMapFilter = createDMFilter();
private var staticSound:Sound = new StaticSound();
// CLASS CONSTRUCTOR
public function JitteryButton(button:MovieClip) {
myButton = button;
addChild(myButton); //removes from previous parent. This may screw up relative positioning.
// add the rollover event listeners to the button
myButton.addEventListener(MouseEvent.ROLL_OVER, onButtonRollOver);
myButton.addEventListener(MouseEvent.ROLL_OUT, onButtonRollOut);
// start displaying the static effect
addEventListener(Event.ENTER_FRAME, displayStatic);
}
// increase the intensity of the static to HIGH
private function setStaticHigh(e:MouseEvent = null):void {
fuzzMin = 12;
fuzzMax = 25;
staticTimes = 12;
}
// called on button ROLL_OVER
private function onButtonRollOver(e:MouseEvent):void {
Tweener.addTween(myButton, {scaleX: 1.1, time: .5, transition: "easeOutElastic"});
setStaticHigh();
staticSound.play();
}
// called on button ROLL_OOUT
private function onButtonRollOut(e:MouseEvent):void {
Tweener.addTween(myButton, {scaleX: 1, time: .5, transition: "easeOutElastic"});
setStaticMedium();
}
// increase the intensity of the static to MEDIUM
private function setStaticMedium(e:MouseEvent = null):void {
fuzzMin = 2;
fuzzMax = 6;
staticTimes = randRange(8, 12);
}
// called on ENTER_FRAME
private function displayStatic(e:Event):void {
staticTimes--;
dmFilter.scaleX = randRange(fuzzMin, fuzzMax);
dmFilter.mapPoint = new Point(0, randRange(0, -160));
myButton.filters = new Array(dmFilter);
if(staticTimes <= 0){
fuzzMin = 0;
fuzzMax = 2;
}
}
// create the displacement map filter
private function createDMFilter():DisplacementMapFilter {
var mapBitmap:BitmapData = new StaticMap(0,0); // use the bitmapdata from our StaticMap image
var mapPoint:Point = new Point(0, 0); // this is the position of the StaticMap image in relation to our button
var channels:uint = BitmapDataChannel.RED; // which color to use for displacement
var componentX:uint = channels;
var componentY:uint = channels;
var scaleX:Number = 5; // the amount of horizontal shift
var scaleY:Number = 1; // the amount of vertical shift
var mode:String = DisplacementMapFilterMode.COLOR;
var color:uint = 0;
var alpha:Number = 0;
return new DisplacementMapFilter(mapBitmap, mapPoint, componentX, componentY, scaleX, scaleY, mode, color, alpha );
}
// returns a random number between the specified range (inclusive)
private function randRange(min:int, max:int):int {
var randomNum:int = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
}
}
Thanks