Thursday, 19 August 2010

Endless scrolling background in AS3

In this tutorial you will learn how to create an endless scrolling background in Actionscript 3 where an image will continue looping. I have used an image of some building for this tutorial, but any image will work.

Endless scrolling background in AS3

Step 1

Open a new AS3 file and import your image onto the stage by selecting File > Import > Import to Stage.

Step 2

Convert your image into a movie clip (F8). Then give an appropriate name, select the top left registration point. And click the ‘Export for Actionscript’ checkbox and give the class name: ScrollBg. Once you have create the movie clip delete it from the stage.

Step 3

Add the following code in the Actions panel.

//The speed of the scroll movement.
var scrollSpeed:uint = 2;

//This adds two instances of the movie clip onto the stage.
var s1:ScrollBg = new ScrollBg();
var s2:ScrollBg = new ScrollBg();
addChild(s1); 
addChild(s2);

//This positions the second movieclip next to the first one.
s1.x = 0;
s2.x = s1.width;

//Adds an event listener to the stage.
stage.addEventListener(Event.ENTER_FRAME, moveScroll); 

//This function moves both the images to left. If the first and second 
//images goes pass the left stage boundary then it gets moved to 
//the other side of the stage. 
function moveScroll(e:Event):void{
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;
}
}
Step 4

Test your movie clip Ctrl + Enter.

Sunday, 15 August 2010

Number guessing game in AS3 part 2

This is part 2 of the Number guessing game in Actionscript 3 where I will be limiting the number of guesses in the game. A message will display the number guesses left. If the correct answer is guessed, or the number of guesses is used up then an appropriate message will be displayed. Be sure you have completed the previous tutorial before attempting this one as additional code will be added.


Number guessing game in AS3 part 2

Step 1

Open up the Number guessing game in AS3.
Select the Text tool with dynamic text and create another text field below message field and give it the instance name: guess_txt.













Step 2

On the timeline select the Actions layer and open up the Actions panel and enter the following code.

**Note that I have not included the code from the previous tutorial.
//Declare variables
var numOfGuesses:uint ;
var numOfGuessesMessage:String;

function init():void {
    //Displays a message showing the total number of guessing remaining 
    numOfGuesses = 10;
    numOfGuessesMessage = " Guesses remaining";
    guess_txt.text = numOfGuesses + numOfGuessesMessage;
}

function yourGuess(event:MouseEvent):void {
    guessMessage();
}

function guessMessage():void{
    //Only decrement the numOfGuesses if the text field contains characters. 
    if(input_txt.length > 1 && numOfGuesses != 0){
        numOfGuesses--;
        guess_txt.text = numOfGuesses + numOfGuessesMessage;
    }
    
//If there is no guesses left then a message appears, and you can restart the game  
    if(numOfGuesses == 0){
        message_txt.text = "The number was " + randomNumber + ".";  
        guess_txt.text = "Unlucky, you never found the number.";
        endGame();
    }
    
}

**I have change the name of the winGame() function to endGame(), so you will need to update all instances of this name.


Step 3

Test your movie Clip Ctrl + Enter.

Tuesday, 10 August 2010

Delay function in Actionscript 3

I previously wrote a post called Time delay in Actionscript 3 where I showed different ways of setting a time delay. In this post I will show two ways of delaying a function. You can use the native setTimeout function, or the delayedcall method from the tweenlite package.

Firstly, the setTimeout() function is shown in the example below. There are two parameters in the setTimeout() function. The first one is the function to be executed and second is the delay in milliseconds. So, in the example below a message will be traced out after three seconds. For more information, checkout the AS3 language reference.

//Traces out the “Hello ilike2flash”
function displayMessage(){
trace(“Hello ilike2flash”);
}

//Call the displayMessage function after three seconds.
setTimeout(displayMessage, 3000);

The tweenLite delayedcall method works pretty much exactly how the setTimeout() function works. However, the parameters are the opposite way around and the delay is in seconds. You will of course need to download the tweenlite package for this to work.

//Imports the tweenlite package
import com.greensock.* 

//Traces out the “Hello ilike2flash”
function displayMessage(){
trace(“Hello ilike2flash”);
}

//Call the displayMessage function after three seconds.
TweenLite.delayedcall(3, displayMessage);

You can also use the delayedcall method to call a function after a certain number of frames, or pass in parameters to the function. For more information, click here.

Another method is to use the Timer class. You set a specified a time delay and a repeat count of 1.This will call the showMessage() function after three seconds.
var myDelay:Timer = new Timer(3000,1);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();

function showMessage(event:TimerEvent):void{
trace("hello");
}

Monday, 9 August 2010

Call function from external SWF in AS3

This post will show you how to call a function from an external SWF. I have modified code from the load external SWF tutorial. The code below assume you have a method in your external SWF called testing().

//This creates a new instance of the loader object.
var my_Loader:Loader = new Loader();

var my_loadedSwf:MovieClip;

//These listeners detect when the file has finished loading, and if the correct file is 
//loaded.
my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
my_Loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);

//The load method then loads the SWF file into the loader object.
my_Loader.load(new URLRequest("mouseover2.swf"));

//This assigns the loaded SWF into a variable which allows the testing() function to be called.
function finishLoading(loadEvent:Event) {
my_loadedSwf = loadEvent.currentTarget.content;

addChild(my_loadedSwf);

my_loadedSwf.testing();
}

function errorHandler(errorEvent:Event):void {
trace("file not found");
}

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP