Posts filed under 'Flash Tutorial'

Beginners Flash Tutorial – ActionScript Operators

For users who prefer to learn Flash visually we have a range of Adobe Flash CS3 video tutorials, this method of training greatly enhances learning and allows beginners to master even the most complex aspects of Adobe Flash with ease.
View the Beginners Flash CS3 Tutorial Video

.

Beginners Flash Tutorial – ActionScript Operators

Conditionals

In this Flash tutorial we’ll look at decision-making in Actionscript 3.0 using conditional statements. For example you might want a certain piece of code to execute under a certain circumstance and another piece of code to execute under different circumstances, ActionScript 3.0 like most programming languages uses conditionals to deal with these situations. To simplify ActionScript creates a test asking whether conditions are met, if the condition is met the test will evaluate to true and if the condition is not met alternative action is taken. In these examples we’ll be using some symbols known as comparison and logical operators. Below is a list of both types of operators you should familiarise yourself with before going any further.

Download the working files used in this tutorial »

Comparison Operators

== is equal to

< is less than

> is greater than

>= is greater than or equal to

<= is less than or equal to

!= is not equal to

Logical Operators

%% And

|| Or

! Not

If and Else Statements

For these examples we’re going to use some TextField and TextFormat properties to help explain the code a bit better, below these objects are briefly explained.

We create a new TextField object and call it theText. This creates an instance of the TextField class and gives us access to all the properties, methods and events of that class.

   var theText:TextField = new TextField();

Next we create a TextFormat object called theFormat, again giving us access to all the properties, methods and events available to that class. The TextFormat class will allow us to change the colour properties.

   var theFormat:TextFormat = new TextFormat();

Setting the colour format to red using the color property.

   theFormat.color = 0xFF0000;

Populating the TextField with the word “hello” using the text property.

   theText.text = "hello";

Combining the TextFormat properties with the TextField.

   theText.setTextFormat(theFormat);

Adding the TextField to the stage using the addChild() method, making it visible when the file is published or previewed.

   addChild(theText);

The if Statement

The first condition we’ll look at is the “if statement”, with if statements your essentially asking if something is true or not. To use the “if statement” we start out with the “if” keyword to declare that we are using an “if statement”. Inside the brackets is the condition and inside the curly braces is the code that’ll execute if the condition inside the curly braces is met. For our code below we’re asking if the text inside the theText TextField is equal to “hello”, it is so therefore the code inside the curly braces is executed and the text is traced out in the output window when the file is previewed.

Example;

   if(theText.text == "hello")
   {
   trace("the text inside the text field says hello");
   }

Adding an else statement

So what happens if you want some code to execute if the code returns a false value or doesn’t add up? For this we use the else statement, the else statement executes when all the other conditions aren’t met. In our code below we’re asking if the text inside the TextField is equal to “goodbye”, it isn’t therefore the else statement is executed and the trace statement is traced into the output window when the code is previewed.

Example;

   if(theText.text == "goodbye")
   {
   trace("The text inside the text field says goodbye");
   }
   else
   {
   trace("The text inside the text field doesn't say goodbye");
   }

else if Statement

What happens if you want to execute more than one if statement? Unfortunately we can only use one if statement and one additional else statement, but there is a way around this with the use of the else if statement. Think of the else if statement as just an alternate if statement. In our code below the “if statement” returns a false value so the code moves onto the else if statement, the else if statement condition is true so the trace statement inside the curly braces is executed.

Example;

   if(theText.text == "Goodbye")
   {
   trace("the text inside the text field says goodbye");
   }
   else if(theText.text == "hello");
   {
   trace("The text inside the text field says hello");
   }

Using switch Statements

As you may have noticed if statements can be rather bulky and can take a while to read through, this can be a particular pain when coming back to old code you haven’t seen in a while. An alternative method is to use the switch statement to shorten the code, let’s convert the code from the previous example into a switch statement. In the example below we start out with the switch keyword declaring that it’s a switch statement, inside the brackets is the object we want to evaluate, the case line represents the possible value and the trace statement executes if the test is successful. Finally the break line indicates the end of each case.

Example;

switch (theText.text){
   case "goodbye" :
   trace("The text inside the text field says hello");
   break
   case "hello" :
   trace("The text inside the text field says hello");
   break
   }

Add comment November 14, 2008

Flash CS3 Tutorial – Gradient Fills in Actionscript 3

For users who prefer to learn Flash visually we have a range of Adobe Flash CS3 video tutorials, this method of training greatly enhances learning and allows beginners to master even the most complex aspects of Adobe Flash with ease.
View the Adobe Flash Video Tutorial – Training.

.

Gradient Fills in Actionscript 3

Creating Gradients using Flash is not hard, but how about using ActionScript to dynamically create a Gradient? Following on from our earlier Flash Tutorials : Creating Lines and Shapes with ActionScript this Beginners ActionScript tutorial shows you how.

In this tutorial we’ll go through at creating some gradient fills using Actionscript 3.0. We’ll take a look at radial and linear gradients; we’ll add angle and use different spread methods to manipulate the gradients effect. In the swf example below we have 6 different gradients each with different effects, from left to right we have a radial gradient, linear gradient, linear gradient with matrix angle, linear gradient with reflect spread method, linear gradient with repeat spread method and finally linear gradient with pad spread method. We’ll break each of these gradients down and go through and explain how they work.

Download a larger html version and the SWF / FLA files used in this tutorial

Radial Gradient

First we create a new Sprite object called gradients. This Sprite object will hold all the gradients information. A Sprite is just a Movie-Clip without a timeline.

var gradients:Sprite = new Sprite();

At the moment the Sprite object isn’t part of the display list. To add the Sprite object to the display list we use the addChild() method, adding an object to the display list essentially adds it to the stage making it visible when you preview or publish the file.

addChild(gradients);

Now we’re ready to start creating the gradient. We have to let Actionscript know what type of gradient we are creating; with the use of the GradientType class Actionscript gives us the choice of either a radial or linear gradient. In this case we’ll choose the RADIAL property, we store this data inside a String object called radType so we can use it later on in the code.

var radType:String = GradientType.RADIAL;

The main ingredients of a gradient are the colours themselves; we’ll choose 2 different colours that will act as the colours for the gradient. We’ll create an Array called radColours to hold the colour information. Using Arrays allow us to hold multiple data in a single variable, since we need to store 2 separate values this comes in handy. Actionscript reads colours in hexadecimal format, if you’re not sure about colours in hexadecimal format have a look at the colour picker in the Flash interface. In the code below we’ve chosen 0x3174d0 and 0x15396f giving us 2 different shades of blue.

var radColours:Array = [0x3174d0, 0x15396f];

We have the colours for the gradient but we can change the alpha scale of either of them. Again we’ll use an Array to hold this information, but this time we’ll call it radAlphas. Inside this Array we’ll change the alpha values of the colours, alpha scale in Actionscript is measured in percent (1 = 100%, .25 = 25%). The first value inside the Array will alter the first colour inside the radColours Array. In our code we’ve set both colours to 1 keeping them at 100%.

var radAlphas:Array = [1, 1];

Next we’ll look at the scale of each colour, for example we might want to show a lot of dark blue and just a little bit of the lighter blue blue. This method works in the same way as if we were to change the gradient scale in graphic applications such as Photoshop. Take a look at the image below, it shows the gradient properties box from Photoshop. The value 0 represents the left-hand position in the gradient box, and 255 indicates the right-hand position in the gradient box. In this current position both colours are distributed equally, but if we wanted to show more of the light blue we’d simply increase the left hand value, and to show more of the dark blue we’d decrease the right hand value. In our code we’ve kept it at 0 and 255 giving each colour an equal share.

var radRatios:Array = [0, 255];

Example of Photoshop Gradient Box;

To gain more control over gradients we can use the methods from the Matrix Class. It’ll enable us to change the height, width and rotation of the gradient. We’ll start by creating a new instance of the Matrix class giving us access to all the properties methods and events of that class.

var radMatrix:Matrix = new Matrix();

The method from the Matrix class we’ll use to manipulate the gradient is called createGradientBox(). This method has 5 parameters available to it, width, height, rotation, tx, and ty. For this gradient we’ll just use the width and height parameters, for our code we’ve set the width and height to 120px by 120px.

radMatrix.createGradientBox(120,120);

We’ll move the Sprite object by changing the x and y properties to 50px.

gradients.x = 50;
   gradients.y = 50;

We have a lot of variables with different data types but they don’t actually make up the gradient, to bring everything together we use a method called beginFillGradient(). Before we get into that method you’ll notice it’s assigned to a couple of elements, it’s attached to the graphics property and that in turn is attached to the gradients Sprite Object. We want all the data to be part of the Sprite object we created in the first line of code, to do this we first assign the method to “graphics” which will draw out the gradient and then finally that’s assigned to the gradients Sprite object. Looking at the beginFillGradient() method you’ll see it has a number of parameters available to it, but we’ve actually already done most of the work for it. When your typing out your parameters a little yellow help box pops up, this help box shows what values are required for the method. The first value required is gradient type, but we’ve already stored the gradient inside the String object so all we have to do is type “radGrad”. That will insert all the data from the String into our beginFillGradient() method. The same goes for the rest of the parameters; the next values required are the colours, we chose the colours earlier and stored them inside an Array, so we simply insert that Array into the method. We continue this process for the ratios, alphas and Matrix data.

//start code block
gradients.graphics.beginGradientFill(radType, radColours, radAlphas, radRatios,radMatrix)
//end code block

Using the drawRect() method we’ll create a rectangle for the gradient to reside in. The drawRect() method has 4 parameters available to it, x and y values, and width and height. For our code we’ll keep the x and y values at 0 because we’ll move the gradient by changing the x and y position of the gradients Sprite object, remember all the gradients information will be added to that Sprite object. We’ll make the rectangle the same height and width as the gradient box we created in the previous line, this now ensures that the radial gradient will sit in the centre of the rectangle.

gradients.graphics.drawRect(0, 0, 120, 120);

Linear Gradient
We’ll make a few changes to the linear gradient; the most notable and obvious is gradient type, which is obviously changed to LINEAR. We’ve also added a gradient angle to the createGradientBox() method by changing the rotation value to 45, changing the angle of the gradient.

var linGrad:String = GradientType.LINEAR;
   var linMatrix:Matrix = new Matrix();
   linMatrix.createGradientBox(120,120,45);
   var linColors:Array = [0xb61322, 0x580408];
   var linAlphas:Array = [1, 1];
   var linRatios:Array = [0, 255];
   var lin:Sprite = new Sprite();
   lin.graphics.beginGradientFill(linGrad, linColors, linAlphas, linRatios,linMatrix)
   lin.graphics.drawRect(0, 0, 120, 120);
   lin.x = 350;
   lin.y = 50;
   addChild(lin);

Adding Spread Methods
To add some spread methods we first let Actionscript know what type of spreadMethod we are using. This works in the same way as choosing the gradientType, we place the spreadMethod inside a string and then add it to the beginGradientFill method.

//Reflect Spread Method
var reflectGrad:String = GradientType.LINEAR;
   var reflectMatrix:Matrix = new Matrix();
   var spreadReflect:String = SpreadMethod.REFLECT;
   reflectMatrix.createGradientBox(20,20,0,0,0);
   var reflectColors:Array = [0xb61322, 0x580408];
   var reflectAlphas:Array = [1, 1];
   var reflectRatios:Array = [0, 255];
   var reflect:Sprite = new Sprite();
   reflect.graphics.beginGradientFill(reflectGrad, reflectColors, reflectAlphas, reflectRatios,reflectMatrix,spreadReflect)
   reflect.graphics.drawRect(0, 0, 120, 120);
   reflect.x = 50;
   reflect.y = 200;
   addChild(reflect);
//Repeat Spread Method
var spreadGrad:String = GradientType.LINEAR;
   var spreadMatrix:Matrix = new Matrix();
   var spreadMeth:String = SpreadMethod.REPEAT;
   spreadMatrix.createGradientBox(20,20,0,0,0);
   var spreadColors:Array = [0xb61322, 0x580408];
   var spreadAlphas:Array = [1, 1];
   var spreadRatios:Array = [0, 255];
   var spread:Sprite = new Sprite();
   spread.graphics.beginGradientFill(spreadGrad, spreadColors, spreadAlphas, spreadRatios,spreadMatrix,spreadMeth);
   spread.graphics.drawRect(0, 0, 120, 120);
   spread.x = 200;
   spread.y = 200;
   addChild(spread);
//Pad Spread Method
var padGrad:String = GradientType.LINEAR;
   var padMatrix:Matrix = new Matrix();
   var spreadPad:String = SpreadMethod.PAD;
   padMatrix.createGradientBox(20,20,0,0,0);
   var padColors:Array = [0xb61322, 0x580408];
   var padAlphas:Array = [1, 1];
   var padRatios:Array = [0, 255];
   var pad:Sprite = new Sprite();
   pad.graphics.beginGradientFill(padGrad, padColors, padAlphas, padRatios,padMatrix,spreadPad)
   pad.graphics.drawRect(0, 0, 120, 120);
   pad.x = 350;
   pad.y = 200;
   addChild(pad)

Add comment November 11, 2008

Flash CS3 Tutorial – Library Symbols

For users who prefer to learn Flash visually we have a range of Adobe Flash CS3 video tutorials, this method of training greatly enhances learning and allows beginners to master even the most complex aspects of Adobe Flash with ease.
View the Flash CS3 Training Videos .

.

Flash CS3 Tutorial –  Library Symbols in

This Flash CS3 Tutorial gives the beginner a detailed insight in to using Library Symbols.
When you work with Flash CS3, your movies will naturally contain a variety of resources; graphic images, movieclips, sound effects etc. The library is the main tool for managing these resources.

Open Flash and create a new document; if the library panel is not visible, choose Window > Library or press CTRL+L. To add some items to the library, we’ll create a couple of symbols. Draw a shape on the stage, select it and press F8 or choose Modify > Convert to Symbol. Enter ‘myshape’ as the name for the symbol and choose Graphic as the type:

graphic symbol

You can select a point on the Registration grid for flash to refer to when rotating or aligning the symbol, leave it at the default for now.

You should see right away that your Graphic symbol has been added to the library:

library item

The preview area gives you a thumbnail impression of the symbol, for MovieClips this will be a preview of the first frame, and allows you to preview the clip in progress also. Let’s create a MovieClip to try it out, this time we’ll create the symbol in a different way: choose Insert > New Symbol or press CTRL+F8 and enter ‘myclip’ as the name, MovieClip as the type:

movieclip symbol

Press OK and Flash will take you inside the MovieClip. To create some simple animation and make use of the library, we’ll use the myshape Graphic symbol created earlier: click on it in the library and drag it onto the stage – what you have done here is place an ‘instance’ of the library symbol within your movie. Click on frame 10 and create a keyframe there (F6 or Insert > Timeline > Keyframe). On frame 10, move the myshape instance to a different position on the stage. Now create a Motion Tween by clicking on frame 1 and selecting Motion from the Tween drop-down list on the Properties panel, or selecting Insert > Timeline > Create Motion Tween. If you click on frame 1 and press enter you should see the animation.

timeline with tween

Now go back to your main timeline by pressing the Back button:

back

Now if you select your MovieClip item in the library you should see that you have the option to preview the animation also, by pressing the play button:

clip in library

Add an instance of the clip to your movie by dragging it onto the stage; it will appear much the same as your graphic symbol as that is all that appears on the first frame.

When you test the movie, you should see the clip playing in a loop. To make the clip play only once, you need to edit the symbol. To do this, right-click (or CTRL+click) on it in the library and select Edit, or double-click the instance of the clip on your stage. Once inside the clip, select frame 10 and open the Actions panel (F9 or Window > Actions). Enter the following:

stop();

Now when you test your movie, the clip should play only once.

As your Flash projects become bigger and more complex there will be a greater need to manage your library well. To delete an item, you can select it and press the delete key, select it and press the delete button at the bottom of the library panel (see below), or right-click (CTRL+click) on it and select Delete:

delete

To group items together, you can use folders in the library; press the folder button:

folder

Enter a name, then drag the two library items into the folder to try it out. Right-click (CTRL+click) and choose Expand Folder:

expand

As your Flash skills expand, so will the range of different types of item in your libraries.

Add comment November 7, 2008


Follow us on Twitter

Categories