Actionscript codes here

ActionScript Codes Here


I thought I’d make a tutorial for just actionscript so I did here it is any questions you got just ask me.
Movie Clip Follows Mouse


onClipEvent(enterFrame) {
this._x = _root._xmouse;
this._y = _root._ymouse;
}

Movement with walls


onClipEvent (load) {
// Set the move speed
moveSpeed = 15;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
if (_root.Walls.hitTest(getBounds(_root).xMax,_y,true)) {
} else {
this._x += moveSpeed;
this.gotoAndStop(3);
}
// Move Right
} else if (Key.isDown(Key.UP)) {
if (_root.Walls.hitTest(_x,getBounds(_root).yMin,true)) {
} else {
this._y -= moveSpeed;
this.gotoAndStop(2);
}
// Move Up
} else if (Key.isDown(Key.DOWN)) {
if (_root.Walls.hitTest(_x,getBounds(_root).yMax,true)) {
} else {
this._y += moveSpeed;
this.gotoAndStop(5);
}
// Move Down
} else if (Key.isDown(Key.LEFT)) {
if (_root.Walls.hitTest(getBounds(_root).xMin,_y,true)) {
} else {
this._x -= moveSpeed;
this.gotoAndStop(4);
}
// Move Left
}
}
NOTE: To activate the walls, you must make a movieclip labelled “Walls”

Dragging Script


onClipEvent(mouseDown) {
if(this.hitTest(_root._xmouse, _root._ymouse)) {
this.startDrag(true);
}
}
onClipEvent(mouseUp) {
this.stopDrag();
}

If Button


on (release) {
variable += 1;
variable2 -= 1;
if (variable2 < 0) {
variable2 += 1;
variable -= 1;
}
}
Make Invisible Button


on (release) {
tellTarget ("_root.movieclip") {
_visible = false;
}
}
basic car

onClipEvent (enterFrame) {
// right
if (Key.isDown(Key.RIGHT)) {
_rotation = _rotation+move;
}
// left
if (Key.isDown(Key.LEFT)) {
_rotation = _rotation-move;
}
// up
if (Key.isDown(Key.UP)) {
if (move< {
move++;
}
} else if (move>0) {
move–;
}
// down
if (Key.isDown(Key.DOWN)) {
if (move>-10) {
move–;
}
} else if (move<0) {
move++;
}
// enter
if (Key.isDown(Key.ENTER)) {
_x = 200;
_y = 200;
_rotation = 0;
move = 0;
}
// _x & _y position
if (_rotation>180) {
_y = _y+(moveMath.cos(Math.PI/180_rotation));
_x = _x-(moveMath.sin(Math.PI/180_rotation));
}
if (_rotation<180) {
_y = _y-(moveMath.cos(Math.PI/180_rotation));
_x = _x+(moveMath.sin(Math.PI/180_rotation));
}
Movieclip runs away from mouse


onClipEvent (enterFrame) {
if (this.hitTest(_root._xmouse,_root._ymouse,true)) {
this._x = int(Math.random()*550);
this._y = int(Math.random()*400);
}
}

Character jumping script


onClipEvent (load) {
grav_y = 0;
jumping = false;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.SPACE) && !jumping) {
grav_y = 20;
jumping = true;
}
if (jumping == true) {
grav_y -= 1;
if (grav_y<=-10) {
grav_y = -10;
}
this._y -= grav_y;
}
if (_root.ground.hitTest(this._x, this._y+45, true)) {
grav_y = 0;
jumping = false;
}
}
Random Weather
onClipEvent (load) {
randomWeather = random (3);
if (randomWeather == 0) {
conditions = “Sunny”;
_root.rocket.noThrust = 3;
_root.rocket.thrust = 6;
} else if (randomWeather == 1) {
conditions = “Rainy”;
_root.rocket.noThrust = 2;
_root.rocket.thrust = 4;
} else {
conditions = “Night”;
_root.rocket.noThrust = 1;
_root.rocket.thrust = 2;
}
gotoAndStop(conditions);
_root.weatherText.text = "Weather: " + conditions;
}
SCORE SCRIPT


onClipEvent (enterFrame) {
if (this.hitTest(_root.player)) {
_root.score += 1;
if (_root.score == 100) {
_root.gotoAndPlay (frame);
}
}
}
This tutorial is for new Flash user who wish to create RPG games .
Note: This is just a start for making an RPG game, i will add a sequel to this tutorial later, if you guys post more.
You just need some Movie Clip and a few line of code to create this inventory system , it is very simple

Action script in frame
Note: This will create an inventory system in Flash movie frame one . You can move the inventory system anywhere in your flash movie by editing the code . Copy these code to your Flash movie , frame one .
currentslotnum = 1;
stop ();
function addToslot (item) {
if (!item.found) {
item._x = eval (“itemSlot” + currentslotnum)._x;
item._y = eval (“itemSlot” + currentslotnum)._y;
item.found = true;
currentslotnum++;
}
}

Now , you will need some Movie Clips work as the items . Create the Movie Clip with the images you want , then assign these lines of code to the Movie Clip .
onClipEvent (enterFrame) {
if (_root.character.hitTest (this)) {
_root.addToslot (this);
}
}

Now , you will need to create some Movie Clips which will store those items . Create the Movie Clip then name it itemSlot1 , itemSlot2 , itemSlot3 , etc .
Note: If you have 2 items , you just need to create 2 Movie Clips to store items .
Note: Name the Movie Clip itemSlot + Form 1 to the number base on the items you created in the movie .Almost done now …

Movie Clips - Character
The last step is just create a Movie Clip work as the character ( Player controllable Movie Clip ) . After it , name the Movie Clip “character” ( Of course , without the quotation marks . ) and assign these code to it .

onClipEvent (load) {
moveSpeed = 19;
}
onClipEvent (enterFrame) {
if (Key.isDown (Key.RIGHT)) {
this._x += moveSpeed;
} else if (Key.isDown (Key.UP)) {
this._y -= moveSpeed;
} else if (Key.isDown (Key.DOWN)) {
this._y += moveSpeed;
} else if (Key.isDown (Key.LEFT)) {
this._x -= moveSpeed;
}
}
Creating the DNA effect like in the FlashBox banner.
www.flashbox.cjb.net
Create an object like a cotton bud.Give it an instance name of dna1

You can use any colours but the colours I used are as follows:
A radial gradient set with colours from left to right:
255.255.255 at alpha 100%
255,255,200 at alpha 50%
255,255,255 at alpha 100%
243,126,12 at alpha 40%
255,255,255 at alpha 0%
Now create a layer and name it actions. In the first keyframe give action:

for (i=2; i<30; i=i+1)

{

duplicateMovieClip ("/dna1", “dna” add (i), i);

setProperty ("/dna" add i, _rotation, getProperty("/dna" add (i-1), _rotation)+11);

setProperty ("/dna" add i, _x, getProperty("/dna" add (i-1), _x)+10);

}

setProperty ("/dna1", _visible, “0”);
In the next keyframe give action

for(i=2; i < 30; i = i+1)

{ setProperty ("/dna" add i, _rotation, getProperty("/dna" add i, _rotation) + 5);

}

And finally give the third keyframe

gotoAndPlay (2);

And that’s it.

Any questions?

wow how long did it take you to do this…

Heres how to save the game:(requires flash 6)
Object.prototype.setCookie = function(c, n, v) {
var so = SharedObject.getLocal©;
so.data[n] = v;
so.flush();
};
Object.prototype.getCookie = function(c, n) {
var so = SharedObject.getLocal©;
return so.data[n];
};

Just let this code run once, and it will do the job (please don’t ask me how it works, I don’t know, it just works)

Now we can use two functions:
getCookie(“name”,“itemname”)
setCookie(“name”,“itemname”, var_to_save)

of course name stands for how to call the file, itemname is the subject in the file (you can have as many as you want) and when saving a variabele, it needs to know what to save.

As you can see, you save 1 variabele at a time. In a game, many variabeles influence the progress and need to be saved. I’m working on a game too and didn’t want to save each variabele at a time.

This is where arrays come in handy.
Just think of an array as a database, it contains as many variabeles as you want.

You can declare an array in 3 ways, I use only one, because it give’s the array’s subjects ogical names.

_root.database = new Array(name, age, sex);

Now you have a database Array wich contains 3 variabeles.
You can acces these variabeles using _root.database.name.

After asigning vars to it, you can export it using

this.setCookie(“database”,“item1”,_root.database)

and reload it using:

_root.database = this.getCookie(“database”,“item1”)

*Originally posted by Scootman *
**wow how long did it take you to do this… **

Not very

whoa…be easier to read if you put it in AS tags though :slight_smile: Havn’t read it all but looks nice

Yes, as tags is a must :slight_smile: And could you also post an example of what you’re tutorializing? It’s kinda hard to tell just like that :beam:

tutorial please :p: :egg: