Speed up & Speed down
In this tutorial you will learn how to make your character speed up/speed down when it touches another movieclip. You will need to have either Flash MX 2004 or Flash 8, basic or professional, it doesn’t really matter.
An example of what we will be making (Link). Use the arrow keys to move. Every time you hit one of the other movieclips you speed will change.
You need to have taken this (Moving movieclips with the arrow keys) tutorial
First of all you need to make the movieclip you want to speed up/speed down and move around the stage. Mine is just a simple square.
Make it a movieclip, the name doesn’t really matter, and give it an instance name of man.
Now place this code on the “man” MC (movie clip).
_global.speed = 5;
}
onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
this._x -= speed;
}
if(Key.isDown(Key.RIGHT)){
this._x += speed;
}
if(Key.isDown(Key.UP)){
this._y -= speed;
}
if(Key.isDown(Key.DOWN)){
this._y += speed;
}
}
Most of this should be familiar to you if you have taken any of my other tutorials except the first part. onClipEvent(load) basically means ‘run the following code when this MC loads’, so then it doesn’t run every frame like onClipEvent(enterFrame). The next part is just setting a variable. This variable is different though, it is a global variable. Unlike normal variables, this variable can be accessed throughout the whole flash movie, not just that MC. The following code is explained in Moving MovieClips with the Arrow Keys
Now make the other movieclips that will make your “man” speed up or slow down. Once you have done that place this code on one of them.
if(this.hitTest(_root.man)){
_global.speed = 10;
}
}
That means, when this frame is entered, if this movieclip touches the “man” movieclip, the global variable speed equals 10. So then instead of the “man” MC moving by 5 pixels a frame, it moves by 10 pixels a frame.
You can apply that code to as many MC’s as you like, but just change the 10 to whatever speed you like.















Very Cool! I can implement this into my astronaut game as different moon rocks or something.
Thanks :D!
ahhh you beaut :op cheers for that matey
maybe a nice tutorial on how to make a timer now? plz? xD
Wow, lol three comments already.
Yeah, I might do a timer one. But I still have to do that AI one I promised, and I think there are a few others I promised that I have forgot.
A timer would be interesting but I think awesty has a full plate right now. Since a friend and I are making a fighting game a timer could be cool.
Maybe once you’ve done all that you could explain how to make your own simple codes? Since I am interested in coding that would be awesome, but only do what you can. No pressure :p
Well that is kind of what I am trying to do with all the tutorials, teach you how to do something, explain it and then you can go off and play around with it until you understand it.
I might make a tutorial if you like when I get some time.
hey it me! um well i was wondering when the A.I tutorial was coming out i could really go for 1 now. plz get it up soon it would help alot
I will write it tonight. Maybe start it in an hour or so.
ty! that was a gd tut! Wud be gd to use for a scene where your character walks on glue or sumfing
u know millions about flash.. maybe one day u cud put all ur tutorials and knowlage in 2 a game we cud all play
Lol. I know alot of people that know HEAPS more about flash than me.
I was going to make a game for the armor games Christmas contest but I have only just started it and there is only 9 days to go including today so I doubt I will finish it, or even get it half done. I am not motivated enough to finish a game.
so r u makin n e new tuts at the momment?
I haven’t made any for a while, since I couldn’t be bothered. But I will try to write some over the next few days.
heres my code see wats wrong with it bcuz it wont speed up
onClipEvent(load){
_global.speed = 2;
}
onClipEvent(enterFrame){
if(fight == false){
if(Key.isDown(Key.LEFT) && fight != true){
this._x -= 4;
this._xscale = 100;
this._x -= speed;
this.gotoAndStop(2);
}else if(Key.isDown(Key.RIGHT) && fight != true){
this._x = 4;
this._xscale = -100;
this._x = speed;
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
if(Key.isDown(Key.SPACE)){
this.gotoAndStop(3);
fight = true;
}
if(Key.isDown(Key.CONTROL)){
this.gotoAndStop(4);
fight = true;
}
if(Key.isDown(Key.AlT)){
this.gotoAndStop(5);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
}
and heres the object code im trying to make it speed up
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_global.speed = 10;
}
}
Okay, in your code, you have this._x -= 4 AND this._x -= speed. You only need the speed in there.
it still doesnt work
onClipEvent(load){
_global.speed = 2;
}
onClipEvent(enterFrame){
if(fight == false){
if(Key.isDown(Key.LEFT) && fight != true){
this._xscale = 100;
this._x -= speed;
this.gotoAndStop(2);
}else if(Key.isDown(Key.RIGHT) && fight != true){
this._xscale = -100;
this._x += speed;
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
if(Key.isDown(Key.SPACE)){
this.gotoAndStop(3);
fight = true;
}
if(Key.isDown(Key.CONTROL)){
this.gotoAndStop(4);
fight = true;
}
if(Key.isDown(Key.AlT)){
this.gotoAndStop(5);
fight = true;
}
if(this._currentframe == 1 || this._currentframe == 2){
fight = false;
}
}
Try that.
yep i its fixed thxs
Hey Awesty im making a racing game and i didn’t know were to put it so i decided to put it here
you can veiw my game at
http://www.swfup.com/swf-view.php?id=3999
and as you can see when you speed up the backround can’t keep up with the car can you help me? Ill send you the actionscript VIA e-mail
Yep, just send it to me.
when two movie clips hit each other, your man goes behind it. Is there anyway to make it stay ontop of the other mc?
@no1jock: Yes, rightclick the movieclip that you want to be in front then select Arrange>> Bring to Front, or you can add the thing that speeds up to a new layer
think this would work with a friction/gravity variable too?
for instance you can go up to x pixels per second normally, then you poke one of these and it boosts you somewhat over x pps while gravity/friction slowly drags you back down to x pps.
i’ll try to make somethin simple to show you.
I’m surely no good at ActionScript, Awesty is way better than me(by the way you’re good man), I’d rather just make the drawing of the whole thing while someone like him codes it all ^^.
Almost all the tutorials are really helpful,though this Tutorial is pretty basic, but it sure comes in handy sometimes. I’ll keep lookin for interesting ones, though the one about platform games(the one I was most interested) doesn’t seem to work for me.
Oh yeah, Red Feather Raven, you can do that you’ve written above. I’ve already tried it out, in a speed-up game of mine with a character that rollerblades.Though I use a diferent speed up method, in which your speed goes up constantly, and if you quit pressing move key, you’ll still go forward a bit.I like it a lot.
And sorry for my writting, I’m Brazilian and my english is close to pessimal.
How do u make it so if u press a button then it will speed up??
@redfeatherraven: Yea, I know what you mean. It would work.
@Timmy:
if(Key.isDown(SOMEKEY)){
_global.speed = NUMBER;
}
Awesty, if it’s possible, I would like if you made a tutorial on combo building style. I guess that’s a required feature of a fighting game and maybe other gam developers would aprecciate it.
Thanks in advance.
@Grifo: Can you please post that here:
http://www.awestyproductions.c.....s/request/
Otherwise I will forget.
hi,
i want to develop a simple game using flash..
i want a cursor chaser where everytime my score increases the cursor chaser’s speed will also increase..
pls help me
Is it possible to make a movieclip stop on its last frame?
if it is could u plez send me the script for it
@Yoga: I will help you, but you have to tell me what you need help with first.
@m7hj5: Put stop(); on the last frame of the MC.
hi, thanks for the reply. Actually i need to develop an archery game. It works like this… when the cursor hits the archery board, the score wil be displayed on the scoreboard. There will be a mouse chaser, where it moves toward the mouse cursor. When the player’s score increases the speed of the chaser will also increase. if the chaser hits the cursor then the player will loose one of his life..
Now i need help on how to create a mouse chaser and also how to increase the speed of the chaser once the player’s score increases.
Please help
and another thing, i dowan to use classes to ceate the mouse chaser…just a simple coding on the action layer (because im going to put all my coding in one action layer) to create the mouse chaser without using any classes..
thanks in advance.
Okay, so you will need to use trig to make the arrow face the cursor. You need to figure out the adjacent (The difference between the MCs _x and the cursors _x) and the opposite (The difference between the MCs _y and cursors _y). Then you can figure out the angle with those numbers.
I am going to leave the rest up to you, because otherwise you wont really learn anything.
yah the code u gave me before dosent work
Did you change SOMEKEY and NUMBER?
yes..lol
do you know how to make them disappear if it gets touched?
Yes.
if(something hits this){
this.swapDepths(getNextHighestDepth());
this.removeMovieClip();
}
Here is the code for my man:
onClipEvent(load){
_global.speed = 5;
}
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x += 5;
}
if(Key.isDown(Key.LEFT)){
this._x -= 5;
}
if(Key.isDown(Key.UP)){
this._y -= 5;
}
if(Key.isDown(Key.DOWN)){
this._y += 5;
}
}
Here is the code for my speed up object:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_global.speed = 10
}
}
And my slow down object:
Here is the code for my man:
onClipEvent(load){
_global.speed = 5;
}
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x += 5;
}
if(Key.isDown(Key.LEFT)){
this._x -= 5;
}
if(Key.isDown(Key.UP)){
this._y -= 5;
}
if(Key.isDown(Key.DOWN)){
this._y += 5;
}
}
Here is the code for my speed up object:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_global.speed = 10
}
}
And my slow down object:
onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_global.speed = 2
}
}
It doesn’t speed up or slow down when it goed through them :O! Help please, thanks in advance!
-Karan
OMG, double post, please delete it and this.
Sorry.
Awesty i think I need to change some stuff in the code you gave me but give me an example likes names like .Bob. instead of some of the code.
If I don’t need to change the code the error is :
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 6: ‘)’ expected
if(something hits this){
Total ActionScript Errors: 1 Reported Errors: 1
oops likes should be like
@Karen: Instead of this._x -= 5, you need to change it to this._x -= speed;.
Do that for all of them.
@Kurt: Please dont tell me you used the exact code I gave you? When I said something hits this, I meant something as in a MC, hits as in a hitTest and this as in this. I expected you to just fix it up.
Karan* xD
Thanks =D
I tryed to make it so the thing would slow down when it hit it but it makes it go in the wrong direction (I think that is good) but i want to know how to slow it down
Dont use negative numbers. That would make it go in the wrong direction.
I love this, but please can you help me, ive got this script:
onClipEvent(enterFrame){
if(this.hitTest(_root.def)){
_root.speed=1.5;
}
}
works fine, but im making a defense game with loads of enemys, so i copy and paste them, but when one of them touchs “def” (or a trench it will be) they ALL slow down to 1.5 could you please email or reply here to how i can make it only change the speed var for the mc that it touching “def”
Ive being stuck on it for months!
Correct me if i’m wrong.
did you use something else on ur enemy, not “speed”.
cuz if u use speed, it will be the same as ur “def”.
Well since you are changing speed to 1.5, and they all move by speed they ARE all going to go slow. Try making an array with a separate speed for each enemy.