Sunday, 5 December 2010

KeyObject class in Actionscript 3

In Actionscript 3 the only way you can react to keyboard presses is via the KeyDown and KeyUp events. However, Senocular has created the KeyObect class which has the key.isDown() and key.isUp() methods from Actionscript 2. Here is a simple example.

var speed:uint = 5;

var key:KeyObject = new KeyObject(stage);
addEventListener( Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(e:Event):void {
   if(key.isDown(Keyboard.LEFT)){
      lemon_mc.x -= speed; 
   }

   if(key.isDown(Keyboard.RIGHT)){
      lemon_mc.x += speed; 
   } 

   if(key.isDown(Keyboard.UP)){
      lemon_mc.y -= speed; 
   }

   if(key.isDown(Keyboard.DOWN)){
      lemon_mc.y += speed; 
   }
}

I have also used the code from the limit stage boundaries tutorial to stop the object at the edges of the stage.

Saturday, 4 December 2010

Border outline class in Actionscript 3

This is code for creating a solid black border outline around the edges of the swf. I have used the AS3 drawing API’s lineStyle() and drawRect() methods to create the border outline.

/**
 * CREATEBORDER CLASS
 * CREATED BY ILIKE2FLASH.COM
 *
**/
package com.ilike2flash
{
 import flash.display.Shape;
 
 public function createBorder(w:uint, h:uint, col:uint = 0x000000):Shape 
 {
  var border:Shape = new Shape();
  border.graphics.lineStyle(2, 0x000000);
  border.graphics.drawRect(0, 0, w, h);
  border.graphics.endFill();
  return border;
 } 
}

To create a border, you call the createBorder function like below.

var border:Shape = new createBorder(stage.stageWidth, stage.stageHeight);
addChild(border);

Friday, 3 December 2010

Actionscript 3 email validation

When you submit form data, you may need to check if the characters in an email address are correct. In this quick tip I will show you how to validate an email address in Actionscript 3. I will be using the isEmailValid() function below which uses a regular expression to determine if the address is valid. The function will return true is the email is valid and false if it’s invalid.

function isEmailValid(email:String):Boolean {
	var pattern:RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
	return email.match(pattern) != null;
}

Here is an example that displays a message if the correct/incorrect email address is entered.

//Adds a mouse click listener to the submit button.
submit.addEventListener(MouseEvent.CLICK, checkEmail);

//This function displays the appropriate message 
//when an email address is email.
function checkEmail(e:MouseEvent):void{
	if(isEmailValid(email_txt.text)){
		valid_txt.text = 'Email address is valid';
	}else{
		valid_txt.text = 'Email address is invalid';
	}
}

//This function will return true if the email address is valid. 
function isEmailValid(email:String):Boolean {
	var pattern:RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
	return email.match(pattern) != null;
}


Wednesday, 1 December 2010

Endless scrolling background in as3 part 2

This is part 2 of the Endless scrolling in as3 tutorial where I will show you how to control the scrolling images using the left and right keyboard keys. Take a look at the keyboard controls and the detect key press tutorials for more information on using keyboard keys.


Endless scrolling background in as3 part 2

Step 1

Open up part 1 of the tutorial then go in the Actions panel (F9) and make the following changes.

//Adds an event listener to the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);

//This function moves the image to left when the left keyboard
//key is pressed and moves to the right when the right key is pressed.
function keypress(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.RIGHT :
s1.x += scrollSpeed;  
s2.x += scrollSpeed;  

if(s1.x > s1.width){
s1.x = -s1.width;
}else if(s2.x > s2.width){
s2.x = -s2.width;
}

break;

case Keyboard.LEFT :
s1.x -= scrollSpeed;  
s2.x -= scrollSpeed; 

if(s1.x < -s1.width){
s1.x = s1.width;
}else if(s2.x < -s2.width){
s2.x = s2.width-10;
}
break;
}
}
Note that I have removed the enter frame event and the moveScroll function and replaced it with the above code.


Step 2

Test your movie Ctrl + Enter.

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP