TheASthread

10 posts

Flag Post

Ok i just made the PHP thread and now im starting this one its all about action script.Rules of kong are the rules here ok i will start with couple of begginer tuts:
Beginning Actionscript

Okay first thing is first open flash. I am using Flash 8 for this but Flash MX 2004 should be fine as well as Flash MX. I do

suggest you upgrade to Flash 8 though.
Okay once youve opened flash create a new document and saved it; call it “my_flash” for now). Next open the actions panel by

pressing F9. Okay here you will see 4 boxes above a window where you can input your code. From left to right they are:
1) Add new Item to script – we will not be using this because all it does is types the command for you
2) Find/Replace – This can be used to quickly find and/or replace keywords within your code
3) Target path – This is very useful as it allows us to find the target path between 2 movieClips relativly or absolutely
4) Check syntax – This is the most useful thing up there because it tells you if your code is wrong and whats wrong with it
5) Auto format – Very useful it neatens up the code
6) Show code hint – Very useful when you forget the input parameters for functions
7) Debug options – This is just for editing breakpoints etc and we wont be using it in this tutorial

Okay so now we are familiar with the actions panel (Fig 1) we can begin with some AS

Your first lines of code

Okay now click on the first frame and go onto the actions panel. Type in:
trace (“hello world”)
Press Ctrl+Enter to test your movie and how exciting you should get a window saying “hello world”
Excited? Lets go through the code

the trace function
Trace is a function used only when debugging your flash document and wont show up in the final .swf file. Trace basically

displays whatever you tell it to in the output panel. In this case we put in “hello_world” which is enclosed by "" meaning it

is a string.

Difference between strings and variables in the tracefunction

You can trace strings which are always constant ie “hello_world” would output hello_world or varables by not using "". Lets

take a look at variables

Variables

When declaring a variables you must give it 3 things.
- A name
- A type
- A value

and we use the keyword Var to declare a variable

So in frame 1 put

var message:String = “hello_world”

See what we did there

we gave it the name message the type String (you have to put a : after the name) and gave it the value of “hello_world”. So…

var message:String = “hello_world”
trace (message)

When we tested it it would output

hello_world

Are you amazed?

+-/*=

We can also use +, -, / and * to manipulate variables. + is the most important one for Strings because you could do

var message:String = “hello_world”
trace (message + " this is my first bit of AS)

And that would output

“Hello_world this is my first peice of AS”

Numbers

Now lets say we had
var num1:Number = 3
var num2:Number = 5
We can use the +, -, / and * to make it do different things

for example

num1 + num2 = 8
num1 – num2 = -2
num1 / num2 = 0.6
num1 * num2 = 15

And we could even trace them or better still put them into another variable by doing something like the following

var num1:Number = 3
var num2:Number = 5
var num3:Number = num1 + num2

Quick note: Remember that you dont have to assign a value to a variable when it is declared you can always just put
var goatse:String

Okay so now we know alot about variables and strings and how to use trace to debug your code I’ll let you have a play with

those until next time

To expand on your knowlage of these things here are some links you should study before going on to part 2 of this tutorial

 
Flag Post

Ok here the second tut
Variables
So, what is a variable?
Basically, it’s a word that contains information. It can be a string, a number, or any other kind of data.
Each variable can hold one value only, but the value can be changed during runtime, allowing Flash Player to save data such as score, name, speed of an object etc. Flash Player remembers the value the variable holds, and anywhere you type the name of the variable, it recognizes the value and uses it.

Sounds good. So, how do I use these variables?
Well, first of you must declare the variable, in other words create or initialize it. This is because the variable’s value might change during run-time. For example, if you have to add 10 to a variable named myScore, and myScore doesn’t already have a value, Flash Player doesn’t know what the new value should be.

Declaring by scoping

Whaddaya mean, scoping?
A variable’s scope is a reference to where the variable “lives”, in other words where it’s value can be retrieved or changed. Three types of scopes exists:
Local,
Global,
and Timeline variables.
Each one of these is declared in their own way.

Local variables “lives” in one function only, the one they’re declared in. When that function ends, the variable expires, it can no longer be retrieved or changed. You declare a local variable like this (Also see Strict data typing further down):
var variableName = value;
For example:
var myName = “John”;
Two advantages with local variables: They make the function independent from other code in the Flash file, and there can be many local variables with the same name, assuming they are declared in different functions.
Learn more about functions in AS: Functions by Inglor.

Global variables are variables that are accessible from everywhere in your Flash file. They can be changed and read from every timeline and scope, as long as you don’t have a local or timeline variable with the same name. In that case, the global variable can only be accessed from outside the other variable’s scope. When you declare a global variable, you can’t use var, which means you can’t apply Strict data typing to a global variable. Instead, you use the word _global when declaring. Like this:
_global.variableName = value;
For example:
_global.myPassword = “3ehjTEx56”;
A global variable can be declared anywhere, just remember, and this goes for all scopes, that the variable must be declared before you can access it.

Timeline variables “lives” on one timeline, the one they are declared on. I would point you to a hierarchy tutorial, but I don’t know of any good one.
The variable can be accessed from anywhere on that timeline, BUT!, it can also be accessed from another timeline, using dot syntax (.). A timeline variable is declared the same way as a local variable, with the difference that it’s not declared in a function (Strict data typing is applicable to timeline variables too, see further down):
var variableName = value;
For example:
var myDate = new Date();

When you have declared a timeline variable, it can be accessed from another movieclip like this:
targetPath.variable
For example, this adds the value of mySpeed on the main timeline (_root) to the property _x:
_x += _root.mySpeed;

It’s also worth to note that a movieclip is also a kind of variable (Thanks Inglor for that one =P). In fact, everything that returns something else than itself can be considered, since it holds a value.

Strict data typing

When you define a local or timeline variable, you can assign them a specific data type using Strict data typing. This helps a great deal when you debug your code. Instead of having to look through a whole Flash file, a message tells you the exact spot of the error.
The pattern for defining a variable using Strict data typing is:
var variableName:variableType = value;

var tells Flash Player that a new variable is being declared.

variableName is what you want your variable to be called. This is the name you will use to access the variable’s value, and the name you will use to change the value that the variable contains.

variableType tells Flash Player what kind of data the variable is supposed to hold. Use this to avoid giving a variable a value of the wrong class. Let me give an example:
var myScore:Number = 5;
This gives the variable myScore the value 5. But suppose that you later in the Flash file write this:
myScore = true;
When you then test your movie, Flash will give you an error saying “Type mismatch” and the place where you typed wrong, which helps an awful lot when you look for errors in your code.

value is, you guessed it, the value that you want to give your variable.

Naming a variable

When naming a variable, remember to always give unique names, which doesn’t interfere with any of Flash’s Keywords (built-in properties, methods etc.). A good way to avoid interfering with those is to use variable names like:
myName
myDate
myScore
myAge0
myAge1
myAge2

Also, remember that variable names is case sensitive! myScore is NOT the same as Myscore.

Deleting a variable

This is really simple. Sometimes you will not need a variable anymore, and want to free up the memory it uses. Then, simply type:
delete variableName;
For example:
delete myScore;
Simple as that.

 
Flag Post

Here’s a good way to pwn your math teachers:

Let’s say 1+1=2, but you want it to turn to 11, so write

score="1"+1

Make a dynamic text box and put the variable “score” into Var:

When testing, it should say 11.

The reason why strings add “that way” is because strings are “words and letters”. If you write "Hi"+5 inside the variable score, when tested, output will say Hi5.

For those who thought “Nqkoi1 looks very incomplete in variables”, he can’t explain clearly.

Variables are things that store your data. So if you want to store your data (Like 1), write <insertvariablename> = 1. It’s pretty useful for games, a lot.

Mouse AS

Most simplest to translate from AS to English, it’s very simple. Since Nqkoi1 explained the trace command, let’s make a graphic, convert it to a button, and write this quote:

on (release) {
trace ("Jude is awesome")
}

When you release the mouse button, you made me awesome, thanks :D The other handlers are easy to learn.

Arrays

I didn’t learn much about Arrays but from what I know they’re useful. They’re mostly a group of strings.

 
Flag Post

Thanks Jude i will post a tut about arrays so dont you worry about that

 
Flag Post

Instance Name

Instance names are useful because they are the “names” you call in the script, not the name of the movie clip.

Targeting
Targeting is a must. It is very useful.
Absolute Referencing
Absolute referencing is the most complicated and hard-wired (Easily broken if there’s a change). Here’s an example:

_root.car.frontwheel1
It’s just like saying the whole address of your home like (Green Chesterwood, Pacific Ocean #10-21 Blablabla)

Dynamic Referencing (Or something… Hate terms lolz…):

This is a more convienient way to say an address. It’s easier to find and say.

_parent.car.windshield

If there’s a change, there might not be a bug as it’s not hard-wired but…

If you’re talking about the human body and you mentioned it relatively. (It’s the small intestine by the way)

_parent._parent._parent.mouth.teeth
looked so complicated so absolute referencing is a better way.

Tips:

*Less is more
*Functions make life a bit easier for you (They’re like macros, you make the function and you can use it anytime, anywhere)
*Make sure every curly bracket ({) closes. Same goes to parentheses.

 
Flag Post

Darn i can leave you do everythingwait let me see what to add ok here are arrays
Arrays
Introduction
Arrays is possible one of the most useful things in programming. How often don’t you wanna make a game with a lot less variables. Instead of having 300 lines with different variables, you can have all the variables inside an Array. Then you can controll and have access to anything on that array. This will make the game much easier to code. So in this tutorial I have trying to ake everything about arrays clear. So please sit down and enjoy this powerful tutorial.

What is an Array?
Arrays is a kind of a variable which can contains more than one value. You can edit your array and have full control over it under runtime. Arrays is a nice way to have hold and access to many variables at once and is nice for big games like an RPG or a plattformer. An array is also very effective to use combined with different math structures. Lets take an example; Snake. Snake is a cool game where you can steer your snake and its getting longer and longer each time you pick up some ”food”. The only thing flash do is to control the first part of the snake, all this movements are putting in an array. So the array always updates which movements the first part of the snake got to the y and x position and the rest of the part will just follow that information inside the array about which way they shall go. For sure it exists other methods to code a snake game.

Creating Arrays
There is several ways to creat an array. Its enough to just define the array without telling it what it shall contain. I guess you know much about variables when you read this and you should get used to use Strict Data Typing also when creating arrays. So this is the basic structure to an Array:

var arrayName:Array = [];

This will make an empty array which doesnt contain any information. I will recommend to define arrays on a different way. You should follow this way:

var arrayName:Array = new Array();

Both of this two examples does the same, it creates a new array with name, ”arrayName”. Array names are case sensitive and every name must be uniqe, so dont name it the same as a variable, function or class. And Array can contains all types of variables (numbers, booleans and strings). You can also define the array with information, like this:

var arrayName:Array = new Array(“string”, true, 5);
trace (arrayName);

If you test that script it will return: [string,true,5] in the output window. The comma (,) split the different parts in the array. Each part got a uniqe number. Arrays are zero-based, this means that the first part (in our case “string”) got the uniqe number 0. And the next part (true)got the uniqe number 1. Now we gonna work more about reading parts of or whole arrays

Reading Arrays
Many times you will have the need of reading somehing from an array. This way you can also use it in statments to check if a part in the array is the same as something. Lets make an example now so you can try to understand better the part of reading the arrays.

var threeMonths:Array = new Array(“january”, “february”, “march”);

Lets say we in this case wanted to read the second part of the array, then we could use the trace comand which displays a message in the built-in output window in Flash.

trace(threeMonths);

If you so this script it will display [january,february,march] in the output window. You can also just trace different parts, like this:

trace(threeMonths0);

In this case it will display [january] in the output window. If you change 0 with 2 it will display [march]. If you choose 3 or higher it will display ”undefined”, because there is no variable in the array with that uniqe number, the highest number is 2. You got a function called lenght that displays the lenght of the array, for example:

trace(threeMonths.lenght())

This will display 3 because the threemonths aray contains of 3 different values.

Changing Arrays
Sometimes you will need to edit a spesific part in the array. Forexample you can replace the first value in our month array to be December, Its very easy to change a part in an array, just simly do like this:

threeMonths0 = “december”;

One array part can contains more than one value, forexample you got one array part that you want to contain both december and november you can do it like this:

threeMonths0 = [“december”, “november”];

Then both of the months got their own uniqe value; 0. To change the second value inside the first part (“november”) you must choose the second part inside the first part of the array so to change it do it like this:

threeMonths01 = “october”

Now the array ”threeMonths” first part would contain the strings [december,october] So hope you get the point with using parts inside other parts in arrays, this is a smart way of holding information that needs more variables to make sense.
Adding values
Very often when using arrays you will get the use of adding more parts inside an array. You have different ways of doing this to be sure you add the value on right place in the array. Yougot differnet commands for choosing where to put the value, the most used one is the push() command, this will add a value in the end of the array. So if you write this to our month array above:

threeMonths.push(“april”);;

this will make our array have 4 different parts. You can also choose to add a value in the beginning of the array, then you will use the command unshift(). You use the command like this:

threeMonths.unshift(“june”);;

This command will make all the currently values uniqe numbers will increase by one, so the new value will take the first place in the array and steal the first uniqe number of the array (0) from the one before we made this. So now this (june) will be number one in the array and december,october will be number 2. You can also add values in the middle of an array. In this example i want to make a new array first then put a new value in the middle so first i will show you how the splice() commnd work, the comman dwe’re gonna use to put a new value in the middle of an array then i will show you an example.

arrayName.splice(insertion point, delete value, new value);;

if you dont wanna delete or replace a value in the array just leave the delete value blank by type a 0 there. So if you see at this example you will understand it better.

var myArray:Array = new Array(1, 2, 3, 4, 5);
myArray.splice(2, 0, 6);
trace(myArray);;

If you test it the output window will display [1,2,6,3,4,5]. The splice command can be used for both removing and adding values, read more about removing values below.
You can also use a command called concat() to put two different arrays together to one.if you got two diffent arrays you can put them together like the example below. But you will have to make a new array that contains both of the array values.

var numberArray:Array = new Array(1, 2, 3);
var alphaArray:Array = new Array(“a”, “b”, “c”);
var concArray:Array = numberArray.concat(alphaArray)
trace(concArray);;

This script will return [1, 2, 3, “a”, “b”, “c”].

Removing values
Often when using arrays, you need to remove some of the parts inside the array. There are several ways to do this. You can choose which of the values in the array you wanna delete, to remove the first part or value in an array you must use the command shift() So here’s an example:

var numberArray:Array = new Array(1, 2, 3);
numberArray.shift();
trace (numberArray);;

This will display [2,3] in the output window. There is other methods to do it on too, you can remove the last value in the array by using the function pop(). So here’s an other example below.

var numberArray:Array = new Array(1, 2, 3);
numberArray.pop();
trace (numberArray);;

this will display [1,2] in the output window. Now we only need a command to remove a value in the middle of the array. Then we will use the same function as the one we used in the ’Adding Values’ chapter. We’re gonna use the splice() function. But this time we’re gonna leave the place where we should add a number blank. So here’s hows it works now:

arrayName.splice(insertion point, delete value);;

So this work the same as last time exept we dont add a new value. So here’s an example on how it works:

var numberArray:Array = new Array(1, 2, 3);
numberArray.splice(1, 1);
trace (numberArray);;

This will deisplay [1,3] in the output window. So thta’s what we have about removing values.

Manipulating arrays
Sometimes we dont only want to change a value, but we wanna change the whole array. There is alot of different functions to do that. We can use the function reverse() to reverse the order of the values in the array. Like this:

var numberArray:Array = new Array(1, 2, 3);
numberArray.reverse();
trace (numberArray);;

This will display [3,2,1] in the output window. Another function is the function sort() to make the order in alphabetic or numeric order. So an example:

var numberArray:Array = new Array(4, 2, 3);
numberArray.sort();
trace (numberArray);;

This will display [2,3,4] in the output window. There is one more sortings function called sortOn(). This function will sort out an array in alphabetic or numeric order, but you can choose what type of value in the array you want to put in order. So if you got an array with alot of different values, you can choose to only sort that value. You can also use the function join() to make all the values in the array to stings values, and you can also change a sepearator between the values, here’s an example:

var numberArray:Array = new Array(‘a’, “b”, “c”, “o”);
trace(numberArray.join(" – "));

This will display [a – b – c – o] in the output window. So thats what i got about manipulating arrays, hope you understand it.

 
Flag Post

Buttons

 
Flag Post

You might want to read the sticky for info on how to post code.

 
Flag Post

Oh well i i guess i will and thanks for the buttons editundo

Umm where is the code sticky?

 
Flag Post

Umm where is the code sticky?

It’s in the [FAQ] Making Kongregate games. READ FIRST! thread. Scroll down to the section entitled “Posting Code on the Forums.”