Actionscript codes here!
- 274,597 Views
- 8,486 Replies
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
I just got myself a book Flash MX actionscript ans i've learned a lot already. So i'll post some codes. You should post too!
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;
}
}
If you need a script or anything, post your question, or if you have a good script. Post it here!
~EviLudy
- SunderEX
-
SunderEX
- Member since: Apr. 2, 2003
- Offline.
-
- Forum Stats
- Member
- Level 09
- Blank Slate
heres is some simple code for ammo in a shooting game:
make 2 variables in first frame
currentammo=6; //or whatever//
ammoclips=10; //or whatever//
Then, in the actionscript window for ure gun movie clip place:
onClipEvent(mouseUp) {if(_root.currentammo>0) {gotoandPlay(//the frame with shooting animation//); _root.currentammo-=1}}
To make a reload scene u have to make a reload animation, and then in the gun movie clip after the previous thing,:
if(Key.isDown(Key.SPACE)) {if(_root.ammoclips>0) {gotoandPlay(//frame with reload animation//}}
Put in the last frame of the reload animation:
_root.currentammo=6;// or any other number// _root.ammoclips-=1;
Then you should have proper gun mechanics. Of course you could replace the if(Key.isDown(Key.Space)) with on(Keypress(Key.SPACE), and that actually might be better, but i havent completely got the actionscript lingo so please pardon the syntax errors.
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
Dressup items:
For every item of clothes you want to have draggable do this
Make the item a button
Put these actions in the button:
on (press) {
startDrag ("");
}
on (release) {
stopDrag ();
}
And after that, you convert the button to another symbol, and that symbol must be a movieclip.
NOTE: you can also use the dragging script above, that one's much less complicated.
- m0ssy
-
m0ssy
- Member since: Nov. 30, 2002
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
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<8) {
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));
}
//mossy
- T-W-Endo
-
T-W-Endo
- Member since: Oct. 20, 2002
- Offline.
-
- Forum Stats
- Member
- Level 07
- Blank Slate
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
The book is called: Teach yourself easy Flash MX actionscript by Bob van Duuren
Here are some other handy scripts:
INVENTORY SCRIPT
___________________________________
You need to create global booleans that, when true, will cause certain movie clips and buttons be visible.
I'm not absolutely sure this will work with nested buttons and/or movie clips, since I've tried similar things and had them not work for reasons unexplained.... Well, anyway, I'd suggest making a movie clip of the subscreen you want, and inside it have all the items. In the actionscript for the movie clip put stuff like:
this.item1_instance_name._visible = false;
this.item2_instance_name._visible = false;
this.item3_instance_name._visible = false;
if(haveItem1 == true){this.item1_instance_name._visible = true;}
if(haveItem2 == true) {this.item2_instance_name._visible = false;}
if(haveItem3 == true) {this.item3_instance_name._visible = false;}
//where "haveItem1" is the name of a global boolean that will initially be false, but will be toggled to true when you pick up the item.
Then in the main stage there could be a button that would have the actionscript:
on(release){
if(_root.subscreen_movieclip._visible == false){_root.subscreen_movieclip._visible = true}
else{_root.subscreen_movieclip._visible = false}
}
Don't forget to name your instances of each movie clip.
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;
}
}
NOTE: if you want to have the character to stop at a ground point, you must make a movieclip and label it ground
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
Hey guys, post more questions, i want to help.
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
Here are more scripts:
insert some a dynamic text box give it the var name score.
add this code to something.
SCORE SCRIPT
_____________________________________
onClipEvent (enterFrame) {
if (this.hitTest(_root.player)) {
_root.score += 1;
if (_root.score == 100) {
_root.gotoAndPlay (frame);
}
}
}
RPG INVENTORY SCRIPT
_____________________________________
Add this code to the item clip:
onClipEvent (enterFrame) {
if (_root.character.hitTest(this)) {
_root.addToslot(this);
}
}
The player that walks around controlled by the user should be labelled Character and the box where the icon of the item comes should be lebelled slot
Random Appearance
__________________________________________
onClipEvent(load) {
chanceOfAppearing = 10;
chance = 0;
}
onClipEvent(enterFrame) {
chance++;
if (Random(chanceOfAppearing) < chance) {
this._x = Random(550);
this._y = Random(400);
chance = 0;
} else {
this._x = -100;
}
}
Random Wandering
_____________________________________________
onClipEvent(load) {
wanderAmount = 300;
leftLimit = 10;
rightLimit = 540;
chanceOfJump = 50;
xPosition = 275;
speed = 10;
chanceOfChange = 0;
}
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
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;
}
}
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
Gun Tutorial
________________________________
First you make a movie clip, that shows the gun and put it on the bottom of the flash Document, then put this code in the actions of the gun:
onClipEvent (enterFrame) {
this._x = _root._xmouse += 20;
}
Then you make an laser dot, wich is the dot the gun aims to automaticly, and also make it a movieclip. Then put this code into the laserdot movieclip:
onClipEvent (enterFrame) {
this._x = _root._xmouse;
this._y = _root._ymouse;
}
onClipEvent (load) {
Mouse.hide();
}
- PikaExploder
-
PikaExploder
- Member since: Apr. 8, 2001
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
Tat inventory code is mega 1337!!!1 No really, dude, I'm like getting a book as soon as I can. That script is tight.
I know what a function is(mathematical terms like f(x)=5x*2 and shit) but not in flash. And eval? Wtf? I'm like a basic scripter. I gotta learn this shit.
I was trying to make something today, just a little test. The test was, when you hit shift, the score goes down randomly. I had this.
onClipEvent(enterFrame){
if (Key.isDown(Key.SHIFT)){
_root.score-= Math.random()*10;
}
}
Everything was good. But when it came to rounding I had no clue. So... whats a script for rounding to the nearest tenth, one, ten, hundred, thousand, etc? I know it involves the Math.round code but I'm stuck from there.
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
I am not that masterful in rounding variables, because I've never realy needed it. But i think I can help:
to round up you use:
Math.ceil();
to round down you use:
Math.floor();
to round to the nearest:
Math.round();
But its not that simple, your code should be somthing like this:
Round up:
score = Math.ceil(score);
Round Down:
score = Math.floor(score);
Round To t5he nearest:
score = Math.round(score);
I hope that helped.
- titbread
-
titbread
- Member since: Dec. 2, 2000
- Offline.
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 5/4/03 06:05 AM, eviLudy wrote: to round up you use:
Math.ceil();
to round down you use:
Math.floor();
to round to the nearest:
Math.round();
no idea these existed.. thanxs very much buddy... very very helpful!!! n(wow u do learn something... everyday)!!!
~tit
- EviLudy
-
EviLudy
- Member since: Aug. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 39
- Blank Slate
- cr0mZX
-
cr0mZX
- Member since: Feb. 14, 2002
- Offline.
-
- Forum Stats
- Member
- Level 21
- Blank Slate
- The-Super-Flash-Bros
-
The-Super-Flash-Bros
- Member since: Sep. 2, 2002
- Offline.
-
- Forum Stats
- Member
- Level 30
- Game Developer
At 5/4/03 05:05 AM, PikaExploder wrote: Tat inventory code is mega 1337!!!1 No really, dude, I'm like getting a book as soon as I can. That script is tight.
I know what a function is(mathematical terms like f(x)=5x*2 and shit) but not in flash. And eval? Wtf? I'm like a basic scripter. I gotta learn this shit.
I was trying to make something today, just a little test. The test was, when you hit shift, the score goes down randomly. I had this.
onClipEvent(enterFrame){
if (Key.isDown(Key.SHIFT)){
_root.score-= Math.random()*10;
}
}
Everything was good. But when it came to rounding I had no clue. So... whats a script for rounding to the nearest tenth, one, ten, hundred, thousand, etc? I know it involves the Math.round code but I'm stuck from there.
if you want to round to the nearest someting, you just need to get a bit creative with the Math.round() function.
say you wanted to round to the nearest 10. there is not pre-made function for this, but you can do it in the following way:
myvar = Math.round(myvar/10)*10
or to the nearest 50:
myvar = Math.round(Math.round(myvar/50))*50
as you can see, you can get to the nearest any number by dividing your variable by that number, rounding it then multiplying it back.
HIH
Tom
- Grapes-Lock
-
Grapes-Lock
- Member since: Dec. 2, 2002
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
Ok, thanks, but can someone tell me how to play an movie clip when 2 symbls touch eachother
I know it sounds confusing (iam not english) but can someone please help?
- PikaExploder
-
PikaExploder
- Member since: Apr. 8, 2001
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
The action for two mcs or whatever touching or overlapping is hitTest. The code you would want to use is:
onClipEvent(enterFrame){
if (_root.mc1.hitTest(_root.mc2){
gotoAndPlay(2);
}
}
I think that's it. If you're putting this code in one mc, you can substitute the "_root.mc1" with "this". Hope it helped.
- PikaExploder
-
PikaExploder
- Member since: Apr. 8, 2001
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
Oh, and thank you eviLudy, and The_Super_Flash_Bros!
- PikaExploder
-
PikaExploder
- Member since: Apr. 8, 2001
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
Thank you eviLudy, and The_Super_Flash_Bros!
- StarCleaver
-
StarCleaver
- Member since: Jan. 3, 2003
- Offline.
-
- Forum Stats
- Member
- Level 29
- Blank Slate
At 5/3/03 06:27 AM, eviLudy wrote: The book is called: Teach yourself easy Flash MX actionscript by Bob van Duuren
hey i've seen that book before. but how advanced does it cover? does it go in-depth on stuff like XML for multiplayer games and usind XML for Flash webpages? just wondering. cuz i'd like to compare that to the book i bought: Macromedia Flash MX Actionscript Bible by Robert Reinhardt and Joey Lott.
I could surely die
If I only had some pie
Club-a-Club Club, son
- Grapes-Lock
-
Grapes-Lock
- Member since: Dec. 2, 2002
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
At 5/4/03 01:26 PM, PikaExploder wrote: The action for two mcs or whatever touching or overlapping is hitTest. The code you would want to use is:
onClipEvent(enterFrame){
if (_root.mc1.hitTest(_root.mc2){
gotoAndPlay(2);
}
}
I think that's it. If you're putting this code in one mc, you can substitute the "_root.mc1" with "this". Hope it helped.
well, when i try to past it, an error pops up, saying, there is an error in the code (yea i called them mc1 and mc2)
- Liei
-
Liei
- Member since: Apr. 22, 2003
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
How do you replace the mouse?
For a sniper game i.e.
- The-Super-Flash-Bros
-
The-Super-Flash-Bros
- Member since: Sep. 2, 2002
- Offline.
-
- Forum Stats
- Member
- Level 30
- Game Developer
put this on a movieclip that u want 2 be the mouse:
onClipEvent(load){
this.startDrag(true);
Mouse.hide()
}
thats it :)
HIH
Tom
- titbread
-
titbread
- Member since: Dec. 2, 2000
- Offline.
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 5/4/03 03:59 PM, The_Super_Flash_Bros wrote: put this on a movieclip that u want 2 be the mouse:
onClipEvent(load){
this.startDrag(true);
Mouse.hide()
}
easy easy easy peasy, u never ever forget this script cos it's so damn easy
~tit
- PikaExploder
-
PikaExploder
- Member since: Apr. 8, 2001
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
At 5/4/03 03:24 PM, Strawberry_Rock wrote:At 5/4/03 01:26 PM, PikaExploder wrote: The action for two mcs or whatever touching or overlapping is hitTest. The code you would want to use is:well, when i try to past it, an error pops up, saying, there is an error in the code (yea i called them mc1 and mc2)
onClipEvent(enterFrame){
if (_root.mc1.hitTest(_root.mc2){
gotoAndPlay(2);
}
}
I think that's it. If you're putting this code in one mc, you can substitute the "_root.mc1" with "this". Hope it helped.
Ah woops sorry, just typed it. Here's the right code that I copy and pasted:
onClipEvent (enterFrame) {
if (_root.mc1.hitTest(_root.mc2)) {
gotoAndPlay(2);
}
}
hope it helps if it works.
Ps: I ordered Actionscript Bible today! :):)
- Link7
-
Link7
- Member since: Mar. 2, 2003
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
- perpetuous-dreamer
-
perpetuous-dreamer
- Member since: May. 5, 2002
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
- Br00d
-
Br00d
- Member since: Mar. 13, 2003
- Offline.
-
- Forum Stats
- Member
- Level 06
- Blank Slate
- Hadji-San
-
Hadji-San
- Member since: Jun. 17, 2002
- Offline.
-
- Forum Stats
- Member
- Level 14
- Blank Slate
yeah ypou have to make a moive background or something if you want it in game...
also in case anyone didnt read my post, on some scripts i really need, here they are again.
if i have random movement on an enemy MC, how would i make it randomly attack when its near me? and whats the command for the random attacking and dying after being hit? like if i hit key down and it hits another MC, make MC die animation. anyone get it? i think you might need to see my .swf to understand it...

