Sunday, August 19, 2007

Events

Events are WAY different than they used to be. Here's a quick rundown:


MOUSE EVENTS:
You now have to create a listener whenever you have a mouse event. Here is an example:

box_mc.addEventListener(MouseEvent.CLICK, onClick); //add click event and tell it to call onClick function
box_mc.addEventListener(MouseEvent.ROLL_OVER, boxOver); //rollover event, call boxOver function
box_mc.addEventListener(MouseEvent.ROLL_OUT, boxOut); //rollout even, call function called boxOver

function boxOver(event:MouseEvent):void{
event.target.alpha = .5;
}

function boxOut(event:MouseEvent):void{
event.target.alpha = 1;
}

function onClick(event:MouseEvent):void{
//trace("you've been clicked");
event.target.y -= 15;
event.target.rotation += 45;
}

box_mc.buttonMode = true; //this tells box_mc to be a button and show the hand cursor


KEYBOARD EVENTS:
stage.addEventListener(KeyboardEvent.KEY_DOWN, jump); //add listener just like the mouse events (key down)
stage.addEventListener(KeyboardEvent.KEY_UP, land); //key up listener

function jump(event:KeyboardEvent):void{
//trace(event.keyCode);
box_mc.y -= 50;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, jump); //this will delete the key down listener when you push a key
stage.addEventListener(Event.ENTER_FRAME, flip); //this is the new enterframe event (this will call flip function)
}

function land(event:KeyboardEvent):void{
box_mc.y += 50;
stage.addEventListener(KeyboardEvent.KEY_DOWN, jump); //this creates a new key down listener when you release a key
stage.removeEventListener(Event.ENTER_FRAME, flip); //this removes the enterframe event when you release a key
}

function flip(event:Event):void{
box_mc.rotation += 45; //constantly rotates the box 45 degrees
box_mc.x += 10; //constantly moves the box 10 pixels to the right
}


LINK TO WEBSITE:
var link:URLRequest = new URLRequest("http://www.google.com"); //make the request

box_mc.addEventListener(MouseEvent.CLICK, onClick); //create the click listener, call function called onClick

function onClick(event:MouseEvent):void{
navigateToURL(link); //this will send the browser to the url specified above
}
box_mc.buttonMode = true; //make the movieclip a button


TIMER EVENT:
this is like the new setInterval

var jumpTimer:Timer = new Timer(5000, 10); //first peramiter is how often, second is how many times

jumpTimer.addEventListener(TimerEvent.TIMER, jump); //sets a listener to the jumpTimer timer, call function "jump"

function jump(event:TimerEvent):void{
animation_mc.play();
}

jumpTimer.start(); //this starts the timer DUH

1 comment:

Dessa said...

People should read this.