Archive

Posts Tagged ‘Add new tag’

Game development using Flash.

June 29th, 2008

Game development using Flash.

hi readers, am writing this blog entry for those flash developers who want to get their hands into flash game development. I will use AS2.0 syntax for the examples. When I have to learn something then code snippets really help. So I will keep writing those sample codes, you can copy / paste those in your Flash IDE and instantly experiment on them.
Basic Maths
First of all, we need to learn basic Maths, I will not go deep into mathematics, so here is the guideline for basic maths
-> Learning basic geometry and basic coordinate system.
-> Learning basic trignometry,Learning sines, cosines and tangents and learning how to apply them.
for instance, if you are given two points (x1, y1) and (x2,y2) on the plane, you should be able to findout the angle forming b/w those points w.r.t normal.

Differences b/w Flash and cartesian coordinate system:
we need to clear out few differences between standard rules and flash rules. We are aware of Cartesian coordinate system, in which x represents horizontal axis and y represents vertical axis and the center point is (x=0,y=0). But in flash the point (0,0) lies at the top left corner of the stage and contrary to cartesian coordinate system the value of y increases as we go down.
Getting position of the object:
We can retrieve and set the x and y values for any movieclip using movieclip’s class properties _x and _y.
for instance:
I can create a movieclip using createEmptyMovieClip method
var container:MovieClip = this.createEmptyMovieClip(”container”, this.getNextHighestDepth());
// then I create a textfield inside t he newly created movieclip.
var label:TextField = container.createTextField(”label”, 1, 0, 0, 150, 20);
label.text = “Hi boy”;

Now a movieclip ‘container’ has been created on the stage and now we can get its x and y values. By default it’s x and y values are 0,0. You can confirm this using famous trace command.
trace(container._x);
trace(container._y);
object movement and user interactivity
If you execute this code, you will see “hi boy” written at top left of the stage.
Now you can easily change these values as well. Moreover if u want your movieclip “hi boy” to constantly change its position, you can use event “enterframe” like following:
this.onEnterFrame=function()
{
container._x++;
if(container._x > 500)
{
container._x = 0;
}
}

This will keep movieclip container in a motion at x-axis. I put a condition cz it was neccessary otherwise container movieclip would have disappeared and left the stage for ever.
Now instead putting textfield in the container object you can draw any shape or object and move it.
In games, user interaction is must. You put user interactivity using various keyboard and mouse events. A simple key event can be used as follows:
if (Key.isDown(Key.RIGHT)) {
container._x++;
}
In the above example instead moving the object constantly, we are moving it whenever user presses ‘right’ arrow key.
Now it was the very basics of flash coordinates, object movements and user interactivity. stay tunned for the next releases.

Share/Save/Bookmark

Jawad Khan Flash Actionscript , , , , ,