Dynamic Text In Flash Cs3

August 6th, 2009 Rslegion No comments

There are 3 different types of TexArea you can use in Flash CS3.

  • Static Text
  • Dynamic Text
  • Input Text

The Input Text and Dynamic Text can be given instance names so that they can be referenced in code.

You can change the type of text area in the properties menu just above the instance name.

When referencing you’re text are in As3 you can change the text of a dynamic textarea like this:

myDynamicText.text = “Hello World”
Categories: Actionscript 3, Flash CS3 Tags:

Complex / Nested Arrays In AS3

August 5th, 2009 Rslegion No comments

This tutorial may be complex for a programming novice

In a previous tutorial we looked at the use of Arrays in As3 and how we can incorporate them into and games and applications.

In this tutorial we will look at how the more complex arrays, aka nested arrays work and why they are useful

Here is an example of a simple Array:

var myArray:Array = new Array ([100],[50])

And here is an example of a Nested Array:

var myNestedArray:Array = new Array ([ [100], [500] ], [ [200], [100] ])

I know that it looks very complex but once you understand the syntax of the array it becomes a lot easier.

If you look closely at the array, you will notice that it is actually split into 2 separate parts.

The first being:

[ [100], [500] ]

The second being:

[ [200], [100] ]

If you were to imagine that the 2 end square brackets on each part were actually parenthesis the entire array would look like this:

( ( [100], [500] ), ( [200], [100] ) )

Hopefully now this is starting to make a bit more sense, a nested array is basically many arrays placed one after the other as a single array. All the square brackets that we imagined as parenthesis just separate the arrays from each other.

If you’re still confused let me show you how you access the values in the code:

var myVariable:Number = myNestedArray[0] // [1]


In order to retrieve just one variable from this array you must include the second number, in this case [1], however for this example i am commenting it out.

I was talking earlier about the nested array being split into 2 parts. The first number we use selects this part. In this example myNestedArray[0] would retrieve the first part of the array,
[ [100], [500] ]

Now. If we again imagine that the end square brackets are parenthesis we are now left with a regular array like this:

( [100], [500] )

This is what the second number is for. As we used [1] it selects the 2nd value which in this case is 500

this means that in this case the uncommented value of myVariable would be 500.

I know that this was an extremely complicated tutorial, if you have any questions on this please feel free to leave them in the comments and i will gladly answer them.

Categories: Actionscript 3 Tags:

Creating You’re First Game In As3 Part 1

August 4th, 2009 Rslegion No comments

Sorry about the wait for this one guys, been on holiday for a while.

So now you know some basic AS3 code, I bet you’re itching to create you’re first game and receive some comments about it.

In this 3 part series i will show you how to create a very simple game from scratch and were to showcase you’re game for in my opinion, the most helpful comments

Lets get down to it!

Our game will consist of 2 text areas, 1 dynamic and 1 static. Go ahead and create these using the toolbar on the left. The type of text area, Eg. Dynamic, Static; can be changed easily in the properties menu.

Text Type

Place the dynamic text in the bottom center of the screen and the static text in the top center of the screen.

Once you’ve done that, create a button from the components menu and place it in the center of the screen.

You’re screen should now look something like this:

Canvas

Change the parameters of you’re button so that the text says something along on the lines of “Click!” or “Hit!” and name the button to ButtonClick

Change the name of the dynamic text box to ClickCount

After completing this steps, you’re ready to move onto the next part of the tutorial that i will release shortly.

Categories: Actionscript 3 Tags:

As3 Keyboard Controls

July 19th, 2009 Rslegion 1 comment

In many apps and games created in AS3 there is the need to use the keyboard. Unfortunately in AS3 this has been made more complex than it needs to be. In this tutorial i will show you how to make use of the keyboard in AS3.

The use of the keyboard in actionscript 3 is made available through the use of the EventListener.

Here is an example of the keyboard event listener.

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown)

This event listener will run when any key on the keyboard is pressed down. To make an operation run only when a certain key is run we have to use an If statement inside the keyDown function.

function keyDown (e:KeyboardEvent)
{

if (e.keyCode == 38)
{
key_Up = true;
}

}

When referencing keys on the keyboard they have to be referenced via there keycode. In this example we are using keycode 38 which is the up key.

The If statement here sets the variable Key_Up to true. This variable could be used to move something in a Event.ENTER_FRAME event listener.

function moveShip (e:Event)
{

if (Key_Up == true)
{
myShip.y += 10
}

}

This will move my rocket ship variable, myShip up 10 pixel per frame if the Key_Up variable’s value is true.

Here are some more Keycodes to help you out:

37 – left
38 – up
39 – right

32 – space

Categories: Actionscript 3 Tags:

As3 FOR loops

July 18th, 2009 Rslegion No comments

In any coding language there is always a need for loops; small bits of code that are rerun with only 1 or 2 small changes. In As3 this has been made extremely simple.

Here is of an example of a FOR loop:

for (var i = 0; i < 10; i++)
{
// Some Code
}

Loops work in a similar way to functions, any code inbetween the to curly brackets will be run each time the loop commences.

This loop will run 10 times in all. The first part of the loop, var i = 0; , creates the temporary variable in which the code inside the loop will most likely use.

The second part of the loop is the criteria in which the loop will run, in this case, i < 10;
This loop will run while the variable i is less than 10.

The third part of the variable, i++ tells the loop what to do each time the loop completes, if this part is coded wrongly, the loop could become infinite and could crash you’re editor.

This is one of the reason’s you are encouraged to save you’re work before each compile.

Categories: Actionscript 3 Tags:

As3 If Statements

July 17th, 2009 Rslegion No comments

One way of only initiating certain parts of code is through the use of ‘If Statements.’ These statements only allow there contents to run if a certain variable meets the requirements of the statement.

Here is an example of a simple If statement:

var myVar = 7
var mySecondVar

if(myVar < 10)
{
mySecondVar = true
}

This statement will run if myVar is any number below and including 9.

Some conditions that can be used inside if statements include:

== – Is equal to
<= - Less than or equal to
>= – Greater than or equal to
< - Greater than
> – Less than

An example of a more complex If Statement would be:

var myFirstVar = 5
var mySecondVar = 2
var myThirdVar

if (myFirstVar <= 4 || mySecondVar == 2)
{
myThirdVar = true
}

This code will run if myFirstVar is less than or equal to for OR mySecondVar is equal to 2.

These operators can be used to make you’re if statements more complex:

|| – OR
&% – AND

Categories: Actionscript 3 Tags:

Referencing Movieclips In AS3

July 17th, 2009 Rslegion No comments

If you’re reading this tutorial, you should already be able to create movieclips and export them for use with actionscript. If you can’t, follow this tutorial first:

Flash CS3 Creating MovieClips

In this tutorial, we will use the simple example of adding a movieclip to the screen. In order to do this you must first create a variable and set its type as a movie clip.

In our previous examples our code would have looked like this:

var myVar:TextField = new TextField()

When setting our variable to become a movieclip all we have to do is replace textfield with the name we gave our variable when we exported. If you read our previous tutorial instead of TextField we would type MyMovieClip

var myVar:MyMovieClip = new MyMovieClip()
Categories: Actionscript 3 Tags: ,

Flash CS3 Creating MovieClips

July 16th, 2009 Rslegion No comments

In As2/As3 the code is mostly centered around Movieclips. In flash CS3, creating and exporting movieclips for use in actionscript has been made extremly easy.

To start, open up a new As3 file.

Toolbox

Create you’re image using the toolbox on the left.

Export For Actionscript

Highlight the whole of you’re movieclip, right click and select convert to symbol

Convert to Symbol

Name you’re new movieclip and make sure the Movie Clip radio button is selected.

Click the export for actionscript button checkbox and click okay. You have now created you’re first movieclip.  Read the next tutorial located here:

For more information of how to call these movieclips in you’re As3 code.

Categories: Flash CS3 Tags: ,

PHP Loops

July 15th, 2009 Matthew Lodge No comments

Loops are pieces of code that repeat as many times as required. I will show a snippet on how each loop is used.

The FOR loop

for($i; $i<25; $i++){
//code
}

The loop is called with the word for(). The $i tells the for() loop that a variable called $i is being declared for this loop. The next argument tells the loop that if the variable is smaller than 25 repeat the code. This last argument increases the $i variable by 1.

The WHILE loop

The WHILE() loop is much simpler than the for loop.

$i = 0;
while ( $i<5 ){
//code
$i++
}

The while() loop repeats itself until the statement in is arguemnt is false. The variable is declared outside the loop so multiple loops can be run the same time from one variable. The code to increase the variable must be included inside the loop or it will run indefinately and crash the browser and possibly the server running the script.

The FOREACH loop

The foreach() loop is used with array variables. It is used to echo them out.

$array = array(1, 2, 3,4);
foreach($array as $a){
//code
print $a //prints every value in the array
}

These loops become very important for automating code when you reach more complicated levels of coding. It’s always good to learn for the future.

Categories: PHP Tags: ,

As3 Arrays

July 14th, 2009 Rslegion No comments

Actionscript 3 arrays are a way of storing data easily and efficiently in one variable rather than using multiple. Arrays are often used for holding certain attributes about an item.

If we were coding a game for example, an array could be used to hold the stats of a weapon / many weapons

var weapons:Array = new Array(["Sword"],[100])

This is a extremely simple version of an array. In this example we have created an array to hold data about some weapons we have in our game, this array holds two bits of data.

The first bit of data is the name of weapon. it is placed in speech marks to show that it a string.
The second bit of data is the damage that our weapon does.

In arrays the each individual bit of data is placed inside square brackets and is separated by a comma.

To get data from an array you have to use this code:

var weaponName = weapons[0]
var weaponDamage = weapons[1]

The data is extracted from the array by writing the order in which the data is assigned inside the variable. As Actionscript ordering starts at 0. The number used to extract the weapon name would be 0 and the damage, 1.

In our next Actionscript 3 tutorial, we will look at creating more advanced arrays called ‘nested arrays.’

Categories: Actionscript 3 Tags: