00:00
00:00
Newgrounds Background Image Theme

Neptuno84 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Actionscript codes here!

389,198 Views | 7,981 Replies
New Topic

Actionscript codes here! 2003-05-02 03:02:36


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


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-02 05:12:13


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.

Response to Actionscript codes here! 2003-05-02 12:54:07


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.


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-02 12:58:09


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

Response to Actionscript codes here! 2003-05-02 22:26:07


Hey what was that book?

Response to Actionscript codes here! 2003-05-03 06:27:35


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


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-03 06:39:52


Hey guys, post more questions, i want to help.


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-03 06:46:16


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;
}


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-04 03:47:59


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;
}
}


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-04 05:01:58


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();
}


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-04 05:05:44


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.

Response to Actionscript codes here! 2003-05-04 06:05:54


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.


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-04 06:20:26


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

Response to Actionscript codes here! 2003-05-04 10:09:01


Post somthing, this topic is dying!


Swing a Little more!

.. ROCK OUT!

BBS Signature

Response to Actionscript codes here! 2003-05-04 10:13:59


sweet!
<3 <3 <3

Response to Actionscript codes here! 2003-05-04 10:49:24


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

Response to Actionscript codes here! 2003-05-04 12:28:54


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?

Response to Actionscript codes here! 2003-05-04 13:26:05


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.

Response to Actionscript codes here! 2003-05-04 13:37:46


Oh, and thank you eviLudy, and The_Super_Flash_Bros!

Response to Actionscript codes here! 2003-05-04 13:41:05


Thank you eviLudy, and The_Super_Flash_Bros!

Response to Actionscript codes here! 2003-05-04 14:59:59


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

BBS Signature

Response to Actionscript codes here! 2003-05-04 15:24:27


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)

Response to Actionscript codes here! 2003-05-04 15:52:48


How do you replace the mouse?

For a sniper game i.e.

Response to Actionscript codes here! 2003-05-04 15:59:09


put this on a movieclip that u want 2 be the mouse:

onClipEvent(load){
this.startDrag(true);
Mouse.hide()
}

thats it :)

HIH

Tom

Response to Actionscript codes here! 2003-05-04 16:21:58


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

Response to Actionscript codes here! 2003-05-04 16:28:36


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:

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)

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! :):)

Response to Actionscript codes here! 2003-05-04 23:14:52


How do i make a MUTE button?

Response to Actionscript codes here! 2003-05-04 23:18:18


Weee!

Actionscripting: My nightmare!

Response to Actionscript codes here! 2003-05-04 23:31:09


is there a way to make rain constantly?

Response to Actionscript codes here! 2003-05-04 23:37:23


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...