Saturday, 27 March 2010

TextAnim class

I found a free open source class called TextAnim which dynamically creates text animations in Actionscript 3. You can see an example of this class at this website here.

TextAnim works by breaking up the text field into characters, words, or lines and then dispatching up to five different block sequences to create the text animations. You then use a tweening engine of your choice to create the animations. The TextAnim documentation can be found here.

To get the TextAnim class to work you need to first download the zip file from here. Then extract the folder called ‘flupie’ into the same directory as your fla file. Open a Flash AS3 file and select the Text tool with dynamic text and type your message. I have used Myriad Pro font with red colour.



Give your dynamic text field an appropriate instance name e.g. myText.



Select the ‘Character Embedding’ button and embed the glyphs you have used in your text message. If you are using more than one glyphs set then hold down the Ctrl key and choose all the glyphs you need.

On the timeline insert layer called action then open up the actions panel and enter your code. Below is an example of a text animation I have created.

Create a scratch card in Actionscript 3 part 2

This is part 2 of the scratch card tutorial where I will add a random background to the scratch card. So every time you scratch the card you get a different background. You could also add a different message if you wish.


Create a scratch card in Actionscript 3 part 2

Step 1

Open up part 1 of the tutorial. Then Import the images you want to use into the library by selecting File > Import > Import to library on the menu bar. I have used free stock images, but you can use any images you wish.


Step 2

On the stage, double click on the maskedbg_mc movie clip to enter its timeline. Now insert a new key frame (F6) and add a new image on to the stage. Repeat this if you have more images. On timeline insert a new layer and call it Actions then open up the actions panel and the following code: stop().


Step 3

Return to the main timeline and open up the Actions panel and add the following lines of code.

//This gets a random number between the number of frames in the 
//masked_bg movie clip.
var r:int = 1 + Math.random() * maskedbg_mc.totalFrames;

//This will stop at a random frame so you will get a random image.
maskedbg_mc.gotoAndStop(r);

Step 4

Test your movie Ctrl + Enter.



Try refreshing the page after you have scratch the card and you should get a different image.

Friday, 26 March 2010

Rain effect in AS3 part 2

You’ve probably heard of the phrase ‘Raining cats and dogs’ which basically means its raining very heavily. Inspired by this phrase I will be literally creating a ‘Raining cats and dogs’ effect by modifying the previous Rain effect in Actionscript 3 tutorial. I will be adding additional lines of code and modifying the rain drop movie clip. For this effect you will need an image of a cat and dog.


Rain effect in Actionscript part 2

Step 1

Open up the Rain effect tutorial.
Import the images you wish to use by selecting File > Import > Import to Library. Then in the library panel locate the rain drop movie clip and double click to enter the timeline.


Step 2

Using the selection tool (V) select the rain drop and delete from the stage. Drag one of your images from the library onto the stage. You may need to resize the image by using the Free Transform tool (Q). Also make sure the position x and y is at 0.


Step 3

Insert a key frame (F6) on the second frame then drag your second image onto the stage. You should now have two images both on separate frames. On the timeline insert a new layer called ‘Actions’ then open up the actions panel and enter the following code.

//This stops the play head from playing the second frame when you run the movie.
stop();




Step 4

Return to the main timeline by pressing the back button or double clicking on grey area on the stage. Now open up the actions panel and add the following line of code inside the first for loop.
//This will randomly choose either frame 1 or 2 using the Math.random() method.
myRain.gotoAndStop(Math.floor(Math.random()*2)+1);

If your cat and dog images are looking a little too large, you can use the scaleX and scaleY properties to reduce the size.
//This will reduce the images to half the size.
myRain.scaleX = .5;
myRain.scaleY = .5;

You can also randomise the scale property if you wish, but you might end up with strangely shaped cats and dogs.


Step 5

Test your movie Ctrl + Enter.



You should have a raining cats and dogs effect.

Saturday, 20 March 2010

Drag and drop puzzle in Actionscript 3

This is an update to drag and drop puzzle tutorial where I will provide the Actionscript 3 code. The steps are exactly the same in the previous Actionscript 2 version, so I will only be providing the code in this tutorial. For more information on dragging and dropping, checkout this tutorial.

//Array to hold all the puzzle movie clip instances.
var burgerArr:Array = new Array (burger_mc, burger2_mc, burger3_mc,
burger4_mc, burger5_mc, burger6_mc, burger7_mc);

//For loop to control the drag and drop of each burger by adding a
//mouse up and down to each movie clip.
for (var i:uint =0; i < burgerArr.length; i++) {
burgerArr[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
burgerArr[i].addEventListener(MouseEvent.MOUSE_UP, drop);
}

//This function drag the movie clips when the mouse is pressed.
function drag(event:MouseEvent):void {
event.currentTarget.startDrag();
}

//This function drop the movie clips when the mouse is released.
function drop(event:MouseEvent):void {
event.currentTarget.stopDrag();
}


After you have completed this tutorial you should end up with something like below.

Friday, 19 March 2010

Scary maze game in Actionscript 3

This post is the update for the Scary maze game. I will be rewriting the game tutorial using Actionscript 3 code. The steps in this tutorial are very similar to the previous one. You will again need a scary image and a screaming sound effect for this game. Both of these are free to download and can be found at everystockphoto.com and sounddogs.com. All the code used for this game can be found from various tutorials on this blog or by using the search box on the right.


Scary maze game in Actionscript 3

Step 1

Open a new AS3 file with the stage size 400 x 300 and the background colour #32FF99.

Select the Text tool (T) with static text and type your message on the stage. This message will be the initial instructional text for the game. I have typed the exact same message from the AS2 version of the game, but you can type whatever you wish.



I have again used a double knocked out text effect for the heading.


Step 2

Create the play button for the game and place the button below the instrutional message. Give your button the instance name: start_mc. If you don’t know how to create buttons, take a look at the basic buttons tutorial.




Step 3

On the timeline create a new layer called Actions then open up the actions panel and enter the following code.

//Stops the playhead moving to the next scene.
stop();

//Adds an event listener to the button with the mouse click event.
start_btn.addEventListener(MouseEvent.CLICK, goNextScene);

//This function stops on the first frame of the second scene using the
//gotoAndStop() method.
function goNextScene (event:MouseEvent) {
gotoAndStop("1","Scene 2");
}


Step 4

Insert a new scene by selecting Windows > Other Panels > Scenes on the menu bar. Add a new scene by selecting the ‘Add scene button’ as shown below.



Step 5

Select ‘Scene 2’ on the Scenes panel if you are not on the second scene.

Take the Rectangle tool (R) and create your maze pattern. For full instructions on how to make the pattern, checkout step 5 from the previous AS2 tutorial here.


Step 6

Using the Selection tool (V) select your maze pattern and convert into a movie clip. Then give the movie clips the instance name: wall_mc




Step 7

On the timeline create a new layer called ‘End maze’. Select the Brush tool (B) and make a small brush point at the end of the maze. The brush point is the area the player should aim to reach. It should looking something like below.



Convert the brush point into a movie clip and give it the instance name: hit_mc


Step 8

On the timeline create a new layer called ‘Actions’. Then open up the actions panel and enter the following code:
//Stops the play head from moving to the third scene.
stop();

//Adds an event listener onto the stage with the mouse move event.
stage.addEventListener(MouseEvent.MOUSE_MOVE, detectHits);

function detectHits(event:MouseEvent) {

//If the mouse touches the edges of the maze, return back to the
//first scene, and remove the mouse move event listener from the
//stage.
if (wall_mc.hitTestPoint(mouseX,mouseY,true)) {
gotoAndStop("1","Scene 1");
stage.removeEventListener(MouseEvent.MOUSE_MOVE, detectHits);
}

//If the mouse touches the brush point, go to the third scene and
//remove the mouse move event listener from the stage.
else if (hit_mc.hitTestPoint(mouseX,mouseY,true)) {
gotoAndPlay("1","Scene 3");
stage.removeEventListener(MouseEvent.MOUSE_MOVE, detectHits);
}
}

**A tutorial on the hitTestPoint() method can be found here.

Optional

You can create a custom cursor for your maze game at this point if you wish by using the following tutorial.


Step 9

Insert another scene by Windows > Other Panels > Scenes on the menu bar. Make sure the new scene is placed below ‘Scene 2’.


Step 10

Import your sound effect and scary image into the library by choosing File > Import > Import to Library.

On the timeline rename the first layer to “Image” and drag your image onto the stage ensuring it matches with the stage boundaries. You can use the Transform tool (Q) to resize your image if it doesn’t. Increase the image layer on the timeline by inserting a frame (F5) at frame 40.

Insert another layer called ‘’Sounds’ above the “Image” layer then drag your sound files from the library onto the stage. Your timeline should look like below.




Step 11

Create new layer called “Button” above the “Sounds” layer. On frame 40 insert a key frame (F6). And create a button with the message “Play again?” Give the button the instance name playAgain_btn.


Step 12

Again insert another layer on the timeline called “Actions” above “Button” layer.
Insert a new key frame (F6) on the 40th frame and add the following code:

//Stops the play head from moving back to the first scene.
stop();

//Add an event listener to the button with the mouse click event.
playAgain_btn.addEventListener(MouseEvent.CLICK, goBackScene);

//This function returns back to the first scene.
function goBackScene (event:MouseEvent) {
gotoAndStop("1","Scene 1");
}

Your timeline should look like below.




Step 13

Test your game Ctrl + Enter.

Wednesday, 17 March 2010

Star wars text effect in Flash CS4

In this tutorial you will learn how to create a Star Wars text effect in Flash CS4. I have used the default motion presets to create this effect, but the 3D rotation tool can also be used. The Star Wars text effect can be seen at the beginning of each Star Wars film where text crawls away from the screen toward a vanishing point. You can download the Star War font for this tutorial if you wish. According to Wikipedia the text used the Star War films is called New Gothic. I have used a free font called Star Jedi which can be downloaded here.


Star wars text effect in Flash CS4

Step 1

Open a new Flash AS3 file in CS4 and change the stage to a black colour.


Step 2

Select the Text tool with static text and #FFFE65 colour and type your message on the stage. I have used a font called Star Jedi which can be downloaded here.



Make sure your text fills the whole of the stage area and the paragraph format is set to justify. It does not matter if you text overlaps the stage boundaries.


Step 3

Using the selection tool select your text on the stage and then open up the motion preset panel by selecting Window > Motion Preset. Locate the ‘text scroll 3D’ preset and select the apply button. A window dialog about converting a symbol for tween will appear, click ok.




Step 4

If you test your movie now it will look a little odd. Firstly, the text will be moving too quickly and secondly the text will be starting in the middle of the stage. To fix the latter problem select the first frame on the timeline then select 3D translation tool and drag the green y axis handle downwards so that the text is moved off the stage. Repeat this for the last key frame so that the text is moved off the top of the stage. Still with the last key frame selected set the alpha property to 0% on the colour effects panel.

To fix the fast text, simply increase the number of frame in between the tween.


Step 5

Test your text effect Ctrl + Enter. You should now have a Star Wars text effect.

Monday, 8 March 2010

Combo Box component in AS3 part 2

This is part 2 of the Combo Box component tutorial where you will learn how to use XML to load text and images by selecting the different labels in the Combo box. For this tutorial you will need four images of flags. I will not be explaining the structure and attributes etc of XML in this tutorial as there are so many tutorials on the internet on this subject. If you want more information on XML then I suggest you do a Google search.


Combo Box component in AS3 part 2

Step 1

Open a text editor and type the following.



Once you have finished typing save the file as: languages.xml


Step 2

Open a new Flash AS3 file and save it in the same directory as your XML file. Then select the Text tool with dynamic text and drag a text field onto the stage like below.




Step 3

On the menu bar select Window > Components and drag a ComboBox component onto the stage below the dynamic text field.




Step 4

Give the ComboBox component the instance name ‘my_comboBox’ and give dynamic text field the instance name: output_txt.


Step 5

Temporary import one of your flag images onto the stage and place it above the dynamic text field. Take note of the x and y position of the image as this will be needed later on then delete it from stage. The image will be loaded dynamically so will not be needed on the stage.




Step 6

On the timeline create a new layer called Actions then open up the Actions panel and enter following code.

//Creates a holder for the images and add it onto the stage with the x position
//at 83 and the y at 25.
var imageHolder:MovieClip = new MovieClip();
addChild(imageHolder);
imageHolder.x=83;
imageHolder.y=25;

//This creates a new instance of the URLrequest object with
//the path to the XML file.
var my_req:URLRequest=new URLRequest("languages.xml");

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

//The XML is saved in the variable my_xml.
var my_xml:XML;

//Event listener to listen to when the XML file has finised loading.
my_loader.addEventListener(Event.COMPLETE, xmlLoader);
my_loader.load(my_req);


function xmlLoader(event:Event):void {
//This parses the XML data.
my_xml=new XML(my_loader.data);

setupComboBox();
}

function setupComboBox() {
//This add the labels to the combo box.
for (var i:int = 0; i < 4; i++) {
my_comboBox.addItem( { label: my_xml.languages.language.@name[i],
data: my_xml.languages.language.@name[i]} );
}

//Adds an event listener to the comboBox with the change event.
my_comboBox.addEventListener(Event.CHANGE, sayHello);
}

function sayHello(event:Event):void {
//This loops through the languages found in the XML file.
for each (var language:XML in my_xml.languages.language) {
//If the attribute in the language tag is the same the label in the combo box
//then the appropriate message get displayed in the text field.
if (language.@name==my_comboBox.value) {
output_txt.text=language.result;

//This loads the from the image path in the xml
var image_req:URLRequest=new URLRequest(language.image);
var image_Loader = new Loader();

//This listener to when the image has finished loading.
image_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoader);

//This loads the image into the image loader.
image_Loader.load(image_req);
}

}

}

//This function adds the loaded image to the image holder.
function imageLoader(event:Event):void {
imageHolder.addChild(event.target.content);
}

Step 7

Test your ComboBox Ctrl + Enter. Try different values in the combo box and you should see a different message and flag appear.

Monday, 1 March 2010

3D Rotation in Actionscript 3

The Flash Player 10 added support for 3D which adds an additional axis (z axis) instead of the x and y axis for 2D. The z axis gives you the ability to move objects further into the screen or further away from the screen. As well as the extra axis, you get 3D rotation. The new properties are: rotationZ, rotationX, and rotationY. An important thing to note for 3D rotation is that the angles are in degrees and not radians. Here are some examples.



var speed:uint = 3;
var angle1, angle2, angle3:uint = 0;

addEventListener(Event.ENTER_FRAME, enterHandler);

function enterHandler (e:Event):void{
 mc1.rotationY = (angle1 < 360) ? angle1+=speed: angle1 = 0;
 mc2.rotationX = (angle2 < 360) ? angle2+=speed: angle2 = 0;
 mc3.rotationZ = (angle3 < 360) ? angle3+=speed: angle3 = 0;
}  

This code rotates three movieclips using the rotationZ, rotationX, and rotationY properties. All the movie clips have there registration points in the centre. The next example creates a rotating card effect. This requires the tweenLite class from greensocks.com, an image of a back card and an image of a front card. The images have been converted into movie clips with instance names: front_mc and back_mc. The two movie clips are placed on top of each other and then converted into another movie clip with the registration points in the centre and the instance name: cardholder_mc.



import com.greensock.TweenLite;

container_mc.front_mc.visible = false;

TweenLite.to(container_mc,2, {rotationY:180, onUpdate:upHandler})

function upHandler():void
{ 
  if(container_mc.rotationY > 80){
  container_mc.front_mc.visible = true;
  container_mc.back_mc.visible = false;
 
}

You can also reverse the card rotating effect using the TimelineLite reverse() method like this.
import com.greensock.TweenLite;
import com.greensock.TimelineLite;

var tl:TimelineLite = new TimelineLite({onComplete:completeHandler});
tl.append( TweenLite.to(container_mc,2, {rotationY:180, onUpdate:upHandler}) );

function upHandler():void
{ 
 if(container_mc.rotationY > 80){
  container_mc.front_mc.visible = true;
  container_mc.back_mc.visible = false;
 }else{
  container_mc.front_mc.visible = false;
  container_mc.back_mc.visible = true;
 }
}

function completeHandler():void
{
 tl.reverse();
}

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP