Wednesday, November 26, 2008

Flash Tutorial: Part 2 of 21

Common Problems, and Syntax and Naming Conventions

Before we start with the actual lesson (which will be lesson 3), I’d like to talk about a few things. Instead of you waiting from lesson to lesson on what to do with the program, I should give you enough tools and information for you to go on and practice flash on your own, or make a different program using other tools.

In case you missed lesson 1, or you want to check previous lessons, use the "previous posts" panel on the right of this blog.

The other day, I had Vinh and Jia run through my tutorial. One mistake was that WORD capitalized some of my if statements for me, which transferred to blogger, so that mistake was fixed. But there was a problem that Vinh and Jia both ran into, and it involved naming your instances.

Instance Names

So, a movie clip on the stage has two things – what it is, and its instance name. What it is is the thing in the library, and the instance name is what you use to refer to it in code. It might’ve been confusing because for all my things, I used the same name in the library (“player” and “player”, or “main” and “main”), but they don’t have to be the same. The thing in the library could be “bugolgi” and the instance name could be “sashimi”.

So try it out. Go into your main, and change the player’s instance name to “forcystus”, and run the code. It doesn’t work. Your new mission is to figure out how to make it work, without changing the instance name. Once you got that, understanding instance names is pretty easy.

So that was the main problem yesterday. Jia forgot to name the ledge, and Vinh forgot to name the player, ledge, and main.

Syntax and Naming Conventions

This is mostly for Vinh, or new people to programming. Yesterday I asked Vinh to see if he can upgrade the program to make the character move around using a variable, and he wrote this:

var speed = 5

if (Key.isDown(Key.LEFT))
{
_root.main.player._x = _root.main.player._x – speed;
}

There are 4 things I would change about this.

I would put a ; after the variable declaration, so var speed = 5;

Flash doesn’t require you to put the ; there, apparently, but I think it’s nice to have, and it will prevent problems in the future. ; means the line is over, and that’s it.

I would put the variable declaration on the first frame of moveTimer, and assign it there too, instead of having the variable declared everytime frame 2 is visited.

I wouldn’t use a simple variable name like speed. Remember, you don’t want your variable names to be the same as Flash keywords, so usually I would use something a bit more complex, like moveSpeed or playerSpeed.

Lastly, and this is probably something you have to learn that Vinh didn’t know about, but:

_root.main.player._x = _root.main.player._x + 1;
Is the same as
_root.main.player._x++;
Or
_root.main.player._x+=1;

So in this case, I would write
_root.main.player._x-=speed;

Naming Conventions

I will also add here that flash has a convention for their keywords. First of all, ALMOST none of their keywords will start with a capital letter. So you’ll never see If or For or Do or While, but you’ll see if, for, do, and while. Also, if the function or whatever has multiple words, the first letter of each word beyond the first is capitalized, so you’ll see things like
gotoAndPlay, attachMovie, removeMovieClip, stopAllSounds, and so on.

You can create your own naming convention for your variables and functions, but I generally have the same rule for variables; first word lowercase, then first letter of each word beyond the first uppercase, so things like moveSpeed, xSpeed, bulletIndex, etc.

For me, function names the same as variables, except each first letter capitalized, including the first, so GetRotationSpeedX, CreateItem, etc.

Functions

I talked to Vinh, so I was going to write something about how functions are useful, and why you’d want to use them, but he gets the gist of it so I’m not writing it here. But I’ll write a function you might need to use later, so you can see the formatting of it. So for example, let’s say you want to figure out whether a number is positive or negative. You might need this function:

function GetParity(whichNum)
{
if (whichNum == 0)
{ return 1;
}
else
{
var parity = whichNum/Math.abs(whichNum);
return parity;
}
}

Or in general

function FunctionName(functionparameter1, functionparameter2)
{
functionCode();
//and optionally, return statement;
}

And as usual, parameters are private, or, the variables only exist in the function, or in the for loop or whatever.

Getting and Setting

Not in Flash! You don’t usually need Get and Set in Flash, you just access the property directly. Some useful properties that most objects have include _x, _y, _width, _height, _alpha, _xscale, _yscale, _rotation, _currentframe, and more. Play around with those.

Good Classes to know

Flash has built in classes, like String. If you type String into the actionscript, highlight it, and press F1, you’ll get the help document relating to strings. Another class you will use frequently is Math. Check it out. You’ll use Math.abs, Math.floor, Math.sqrt, etc pretty often.

Text Debugging

A lot of times, especially when things aren’t working, you’ll want to put in trace events around to see what exactly your code is doing. So you might want to have something like

trace(“the index is “ + whichNum + “ and the boundary is “ + whichBound);

Textboxes

You might want to play around with input text, or dynamic text, or static text. Static text is just regular text. Input text is a text field where you can input information, and Dynamic is where it displays information. For both Input and Dynamic, they each have an instance name, so fill that out, and access the information like this.

var whichNum = Number(_root.iNum.text);

_root.dNum.text = “The number you have chosen is “ + whichNum;

Comments

Comments are always useful. Here’s the syntax:

// comment on this line
/*
Comment section
*/

So… this “lesson” was mostly to help you experiment, instead of just sitting there, waiting from lesson to lesson. You can probably create a “hello world” application now, and maybe some small calculator application.

0 Comments:

Post a Comment

<< Home