00:00
00:00
Newgrounds Background Image Theme

GasGrass 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!

390,054 Views | 7,981 Replies
New Topic

Response to Actionscript codes here! 2003-07-08 10:33:40


I am the worst action scripter in the world, and I find this post awesome.

I have a couple of questions:-

1) I created a moveable person using this code:
onClipEvent (load) {
// Set the move speed
moveSpeed = 10;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this._x += moveSpeed;
this.gotoAndStop(1);
// Move Right
} else if (Key.isDown(Key.UP)) {
this._y -= moveSpeed;
this.gotoAndStop(2);
// Move Up
} else if (Key.isDown(Key.DOWN)) {
this._y += moveSpeed;
this.gotoAndStop(3);
// Move Down
} else if (Key.isDown(Key.LEFT)) {
this._x -= moveSpeed;
this.gotoAndStop(4);
// Move Left
}
}

He won't stop moving his feet when he is not walking. How can I fix that?

2) Using this same MC, I want him to shoot a fireball when you press spacebar at the direction he is looking at.

Response to Actionscript codes here! 2003-07-08 14:13:08


hey i was wondering how to make walls or objects that you cannot pass through such as mario needing to jump over a tube to pass it in a game.

one more question is how do i make a character go through doors or once he gets to a certain point it can change scenes or something?

this will be greatly appreciated if you answer. c ya.

Response to Actionscript codes here! 2003-07-08 15:17:49


does anyone know a hittest script so my car bounces off a wall when it hits it..thanks

Response to Actionscript codes here! 2003-07-08 21:03:30


Using the code you already have, all you have to do is add an additional "else" statement to send the character back to the idle frame when nothing is being pressed:


onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this._x += moveSpeed;
this.gotoAndStop(1);
} else if (Key.isDown(Key.UP)) {
this._y -= moveSpeed;
this.gotoAndStop(2);
} else if (Key.isDown(Key.DOWN)) {
this._y += moveSpeed;
this.gotoAndStop(3);
} else if (Key.isDown(Key.LEFT)) {
this._x -= moveSpeed;
this.gotoAndStop(4);
} else {
this.gotoAndStop("idleFrame");//whatever it is
}

}

Ok, so you're saying he's not gonna be facing the direction he was walking when he goes to the idle frame. Well, how does Flash know which way he was facing? You have to supply that info! Then we can tailor Idle frames to do that.

First, gather information about which way you're facing as you change directions (using -1/0/+1 variables for vertical/horizontal directions makes for easily written fireball && movement code, and a text variable for direction allows you to jump around frame labels with ease). So, label the walking frames "walkup", "walkdown", "walkleft", and "walkright". Then create four idle frames, "idleup", "idledown", "idleleft", "idleright". Keep the first frame blank and empty, however, as it's a good place to put code you only want to run once (i.e, function definitions), as you will never have to go back to it.

As long as i'm at it, i'm going to change the code you have to work INSIDE the MC as opposed to on top of it, so it'd be inside the MC's 'Frame Actions' instead of in the 'Movie Clip Actions' outside the MC. It's a good practice for game making to do MC's this way.
Plus, it allows you to more easily define different codes for idle frames and walking frames, something i find almost a necessity.

moveSpeed = 10;
dir = "right"; //just some inits
horiz = 1;
vert = 0;
this.onEnterFrame = idle; //begin with idle code

//here's the idle code. it basically just waits until
//a key is pressed to do something.

function idle(){
if (Key.isDown(Key.UP)||Key.isDown(Key.DOWN)
||Key.isDown(Key.LEFT)||Key.isDown(Key.RIGHT)){
this.onEnterFrame = walk; //start walk code
}
}

//here's the function code for walking. it should
//move the clip and check to make sure that a key is down:

function walk() {
if (Key.isDown(Key.RIGHT)) {
horiz = 1; //facing positive X direction
vert = 0; //facing neutral Y direction
dir = "right";
} else if (Key.isDown(Key.UP)) {
horiz = 0; //facing neutral X direction
vert = -1; //facing negative Y direction
dir = "up";
} else if (Key.isDown(Key.DOWN)) {
horiz = 0; //neutral X direction
vert = 1; //positive Y direction
dir = "down";
} else if (Key.isDown(Key.LEFT)) {
horiz = -1; //negative X direction
vert = 0; //neutral Y direction
dir = "left";
} else {
this.gotoAndStop("idle" + dir);//automatically goes to the correct frame!!
this.onEnterFrame = idle;//back to idle code
}
this._x += moveSpeed * horiz;
this._y += moveSpeed * vert;
gotoAndStop("walk" + dir); //again, goes to the correct frame
}
gotoAndStop("idle" + dir);//leave the first frame, and never come back

Fireballs? I'll put that in a seperate post, since this one is getting rather lengthy, bear with me.

Response to Actionscript codes here! 2003-07-08 21:52:31


Now, sure, we can make a fireball that is perfectly round so you can shoot it in any direction with the same code. But, since you've already gathered all this info about which way your character is facing... why can't you use the same code to make it fire in different directions? Well of course you can.

Set up the fireball MC similar to the character MC, one frame of initializations with nothing but code in it, and four frames labeled "up", "down", "left", "right", and if applicable, some kind of "destroy" frame in the event of a hit. This way, the dir variable you already have can be ported directly to the fireball. Now here's the code you put in frame one:

moveSpeed = 15; //or whatever speed you want
dir = _parent.dir; //retrieve variables from character clip
horiz = _parent.horiz;
vert = _parent.vert;
lifeSpan = 50; //fireball life span, in frames
age = 0; //fireballs life span counter

//defining functions makes for easily alterable and legible code

function fireBallMove(){
age++;
hitCheck();
if (age > lifeSpan){
this.onEnterFrame = null;
gotoAndPlay("destroy");
}
this._x += moveSpeed * horiz;
this._y += moveSpeed * vert;
}

function hitCheck(){
//hitTest code here, i'll just make something up
if (this.hitTest(enemy)){
enemy.gotoAndPlay("takeDamage");
this.onEnterFrame = null;
gotoAndPlay("destroy");
}
}

this.onEnterFrame = fireBallMove();
gotoAndStop(dir);

The "destroy" frame will be the first frame of an animation sequence that will remove the fireball from the screen. On the last frame of this sequence, there should be this action:

this.removeMovieClip();

Now, to make this fireball come into play, you have to attach it to the screen... somehow... Start out by Exporting the fireball MC:

Right click on the fireball in the Library Panel, select 'Linkage', and check 'Export for actionscript' (and 'Export in first frame'). Give it an identifier name like "fireBall". Now, stick this code in the Character MC, in both the 'idle' and 'walk' functions:


if (Key.isDown(Key.SPACE)){
layer++; //important! reusing layers will destroy other clips
this.attachMovieClip("fireBall", "fireBall" + layer, layer);
layer++; //maybe i'm a little OC, but i always increment layers twice
}

That'll attach the clip to the movie. Since all the scripts the fireball needs to move and work are in the fireball clip, you don't have to worry about doing anything else to it.

Response to Actionscript codes here! 2003-07-08 22:25:03


I eed the script for making a zooming sniper scope, as seen in Scope Assault .
Please, I need help.

Actionscript codes here!

Response to Actionscript codes here! 2003-07-08 22:30:22


At 7/8/03 03:17 PM, raidennrls wrote: does anyone know a hittest script so my car bounces off a wall when it hits it..thanks

I left out a good deal of this script, mainly because it invloves the inclusion of my entire collision engine. But heres a piece of it:

in the actions panel of the car's movie clip:
onClipEvent (enterFrame) {
pow = 0.2;
max = 20;
dec = 0.7;
if (Key.isDown(Key.RIGHT)) {
_rotation = _rotation + 10;
} else if(Key.isDown(Key.LEFT)) {
_rotation = _rotation - 10;
}
if (Key.isDown(Key.UP)) {
rot = _rotation;
thisSine = Math.sin(rot*(Math.PI/180));
thisCosine = Math.cos(rot*(Math.PI/180));
xMove += pow*int(500 * thisCosine) / 100;
yMove += pow*int(500 * thisSine) / 100;
speed = Math.sqrt((xMove*xMove)+(yMove*yMove));
if (speed>max) {
xMove *= max/speed;
yMove *= max/speed;
}
} else {
xMove *= dec
yMove *= dec
}
if (_root.checkBound("car", _x + xMove, _y + yMove)) {
_x = _x + xMove;
_y = _y + yMove;
} else {
_x = _x - (xMove * 0.2);
_y = _y - (yMove * 0.2);
xMove = 0;
yMove = 0;
}
}

The bold area is where you need to insert your own collision detecting function... The one I used, uses three parameters. The first is the name of the movieClip, which helps state which boundaries affect the object. The second is the X coordinate being checked for collision, and the third is the corresponding Y coordinate.

I doubt that made any sense at all.

Response to Actionscript codes here! 2003-07-09 07:22:10


At 7/8/03 06:38 AM, AngryBinary wrote: That depends on the code you used to make the clip follow the mouse, really. First of all, you have to

Mouse.show();

or something to that effect...

Then, you either 'null;' the function you have set to make the clip follow the mouse (clip.customFollowMouseFunction = null;), and/or just remove the clip from the scene (clip.removeMovieClip();)

Thanks alot. All I did was put Mouse.show(); in the timeline actions and it worked lol. Thanks again.

Response to Actionscript codes here! 2003-07-09 18:13:27


ok so this is a big ass topic and im not looking through all of it. What is the action script to switch images? like... in create-a-ride you click to change the spoiler and stuff. what is the script to do that?


Blah Blah Blah..

Response to Actionscript codes here! 2003-07-09 19:54:58


At 7/9/03 06:13 PM, Openwounds wrote: ok so this is a big ass topic and im not looking through all of it. What is the action script to switch images? like... in create-a-ride you click to change the spoiler and stuff. what is the script to do that?

Simple:

tellTarget("spoiler") {
nextFrame();
}

I'm not sure if nextFrame() is the actual name of the function, but you get the idea.

Response to Actionscript codes here! 2003-07-09 20:20:59


does anyone know how to make walls in a game so you can't pass throu it but do stuff like jump on it or over it.

does anyone know how to make it so that once a movie clip gets to a certain point it could change scenes or something such as walking through a door. please help.

how do i make a movie clip do an action like shoot a lazor by pressing the "a" button or something? please help.

Response to Actionscript codes here! 2003-07-09 20:26:38


At 7/9/03 07:54 PM, ninjadeath wrote:
At 7/9/03 06:13 PM, Openwounds wrote: ok so this is a big ass topic and im not looking through all of it. What is the action script to switch images? like... in create-a-ride you click to change the spoiler and stuff. what is the script to do that?
Simple:

tellTarget("spoiler") {
nextFrame();
}

I'm not sure if nextFrame() is the actual name of the function, but you get the idea.

uhm yeah sorta,but will that work if you have multiple things that can change? if i set that it wont be able to keep the other options the same would it


Blah Blah Blah..

Response to Actionscript codes here! 2003-07-10 00:56:10


Hey evilLudy, you think you could throw that AS for the inventory system into a quick fla for me to look at? If you could that would be great help.

Response to Actionscript codes here! 2003-07-10 01:59:46


this is miy 3rd or 4th time sending a post about this question because no one yet has answered.please some one help me.

how do u make walls? and also once a person pass through a certain point make it change scenes and also something like a person walking through a wall?

Response to Actionscript codes here! 2003-07-10 13:41:53


TheGrrrrrr81 wrote:

how do u make walls? and also once a person pass through a certain point make it change scenes and also something like a person walking through a wall?

onClipEvent (enterFrame) {
if (_root.guy, hittest(_root.wall)) {
[actions resulting from hit]
}
}

Thats simple for a guy hittest with a wall.

Response to Actionscript codes here! 2003-07-10 13:44:55


Forgot to say, where the actions are just make it like _root.guy._x -= 5 or some action for walls and changing scenes where you put actions just put a goto action.

Response to Actionscript codes here! 2003-07-12 14:12:24


I won't let this topic die ! It's so usefull !!!
eviLudy, I love you !


I play lottery. I always lose.

LOOK at my CAR VIDEOS on YouTUBE !!

BBS Signature

Response to Actionscript codes here! 2003-07-12 16:24:06


This may seem like a stupid question but...how do you add music to a movie...I've created one but i would like some music in it

Do you hav a actionscript for this???

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


At 7/12/03 04:24 PM, NightCrawler64 wrote: This may seem like a stupid question but...how do you add music to a movie...I've created one but i would like some music in it

Do you hav a actionscript for this???

you can use AS but it is not necessary. To add music to your movie just go to file-> import to library and then browse and find your music file. Then you'll find it in the library. You can drag it from the library or select the frame where you want the sound to be and open the property inspector. Select your music file and WALLAH! The music is added.

IF you need more information for all the sound options, go here:

http://www.angelfire.com/in4/star_cleaver/tut.html#section5

Hope that helps

-Star_Cleaver


I could surely die

If I only had some pie

Club-a-Club Club, son

BBS Signature

Response to Actionscript codes here! 2003-07-12 23:55:55


How do you make a walking actionscript(when you press left or right he/she will move his/her legs and go to the left or right). Also i need a jump actionscript.

Response to Actionscript codes here! 2003-07-13 10:42:36


What is code for usual preloader???

I can't download the one from NG because I have Flas number 5.

Response to Actionscript codes here! 2003-07-13 12:28:10


At 7/12/03 11:55 PM, FusionGuy wrote: How do you make a walking actionscript(when you press left or right he/she will move his/her legs and go to the left or right). Also i need a jump actionscript.

That's alot top ask but i'll try to sum it up all nice for you.=)

The way i'm gonna show you how to do this will be a simple one where the guy will walk to the Left when you hit the left arrow and vice versa with the right. Its going to be the same animation for both walking left and right except it will be flipped over.

So anways, make your walking animation. If you want a good tutorial on walking animations you can see Percival's movies. He has a good one on the Walk Cycle. Okay, back to the topic. Make your walk animation and put it on an MC. Just copy all of the frames to an MC. You know what i mean i hope. Then on the first frame of your MC add a frame(F5) and make it a picture of the guy just standing there. then on the last frame of your walking animation put the actions:

gotoAndPlay(2);

this will make your animation "loop".

Then on the first frame of your MC put a stop(); action.

Now exit editing mode and place your MC on the main stage if it is not already there. Then put these actions on it:

onClipEvent (enterFrame) {
if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
this.gotoAndStop(1);
}
if (Key.isDown(Key.LEFT)) {
this.play();
this._x -= 15;
this._xscale = 100;
}
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this.play();
this._x += 15;
this._xscale = -100;
}
}

Now, see where it says "+=15" in both places? That's how far it will move when you hit the Left and Right arrow keys. Change those numbers to match your movements of your character. and that should be it. If that doesn't work tell me and i'll answer questions.

Now about the jumping script. Go look for a topic called "Look at This"(you might have to use the search bar) and on the first page there is a Jumping Script. That should help you.

Hope that Helps

-Star_Cleaver


I could surely die

If I only had some pie

Club-a-Club Club, son

BBS Signature

Response to Actionscript codes here! 2003-07-13 12:34:14


At 7/13/03 10:42 AM, mahonen wrote: What is code for usual preloader???

I can't download the one from NG because I have Flas number 5.

I thought the NG preloader was compatible with Flash 5? Does it give you an error or something? If not i guess you can use the

_framesloaded

and

_totalframes

methods. They would be something like this:

if (_framesloaded >= _totalframes) {
gotoAndPlay ("Scene 1", "start");
} else {
_root.loader._xscale = (_framesloaded/_totalframes)*100);
}

I just got that straight from the AS dictionary. I'm sure you can figure it out can't you? If not, tell me and i'll lend an extra hand.=)


I could surely die

If I only had some pie

Club-a-Club Club, son

BBS Signature

Response to Actionscript codes here! 2003-07-13 13:54:40


At 7/13/03 12:34 PM, Star_Cleaver wrote:
At 7/13/03 10:42 AM, mahonen wrote: What is code for usual preloader???

I can't download the one from NG because I have Flas number 5.

Hum, thats wierd, I have flash 5 and the preloader works with mine. maybe its a new preloader. Anyway star_cleaver gave you enough script to make your own, :)

Response to Actionscript codes here! 2003-07-13 13:58:08


Damn it, I posted with my bro's(inforcer) acount again.

Response to Actionscript codes here! 2003-07-13 17:26:11


//Dumb
//But Easy "AI"
onClipEvent (enterFrame) {
dec = random(1000)+5;
}
onClipEvent (enterFrame) {
if (dec<=100) {
_x += 10;
}
if (dec >100 && dec<=400) {
_x -= 10;
}
if (dec >300 && dec<=500) {
_y += 10;
}
if (dec>500 && dec<=700) {
_y -= 10;
}
if (dec>700&& dec<=1000) {
_y -= 10;
}
}

Put that inside of a Mc

Response to Actionscript codes here! 2003-07-13 23:19:12


At 7/13/03 12:28 PM, Star_Cleaver wrote:
At 7/12/03 11:55 PM, FusionGuy wrote:
That's alot top ask but i'll try to sum it up all nice for you.=)

Thanx alot it worked perfectly.

Response to Actionscript codes here! 2003-07-13 23:59:46


ok i got a problem you guys can probably help me with.
I need to make a button after my intro that links to my website.I'd greatly appreicate to help.Thank you very much for at least hearing me out and {hopefully}your anwser to my question.

Response to Actionscript codes here! 2003-07-14 07:53:31


just use getURL like this:

on(release){
getURL("http://www.newgrounds.com/", "_blank")
}

the '"_blank"' part just tells it to open the site in a new window. Its the option you want for NG but if you want to change it you can look in the AS dictionary for the other options.


I could surely die

If I only had some pie

Club-a-Club Club, son

BBS Signature

Response to Actionscript codes here! 2003-07-14 08:06:40


Hey that's a cool sig pic!