# Actionscript codes here

**URL:** <https://forum.kirupa.com/t/actionscript-codes-here/27453>\
**Category:** flash\
**Created:** [August 2, 2003, 3:54pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453 "2003-08-02T15:54:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![denacioust](https://avatars.discourse-cdn.com/v4/letter/d/ac8455/32.png) [@denacioust](https://forum.kirupa.com/u/denacioust)\
**Post date:** [August 2, 2003, 3:54pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/1 "2003-08-02T15:54:19Z")

</div>

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+(move_Math.cos(Math.PI/180_\_rotation));  
\_x = \_x-(move_Math.sin(Math.PI/180_\_rotation));  
}  
if (\_rotation\<180) {  
\_y = \_y-(move_Math.cos(Math.PI/180_\_rotation));  
\_x = \_x+(move_Math.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](http://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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 2, 2003, 3:58pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/2 "2003-08-02T15:58:11Z")

</div>

Any questions?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 2, 2003, 9:07pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/3 "2003-08-02T21:07:14Z")

</div>

wow how long did it take you to do this…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 2, 2003, 9:58pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/4 "2003-08-02T21:58:59Z")

</div>

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”)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 3, 2003, 2:14pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/5 "2003-08-03T14:14:40Z")

</div>

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

Not very

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 19, 2003, 1:26pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/6 "2003-08-19T13:26:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 19, 2003, 1:34pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/7 "2003-08-19T13:34:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 21, 2003, 1:00pm UTC](https://forum.kirupa.com/t/actionscript-codes-here/27453/8 "2003-08-21T13:00:01Z")

</div>

tutorial please :p: 🥚
