Best viewed in Firefox

Awesty Productions

Speed up & Speed down

November 20th, 2006 by awesty

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

onClipEvent(load){
    _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.

onClipEvent(enterFrame){
    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.

You can find more tutorials here. ;)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed

52 Comments »

Comment by Bob
2006-11-20 21:58:22

Very Cool! I can implement this into my astronaut game as different moon rocks or something.

Thanks :D!

 
Comment by hats
2006-11-20 23:59:52

ahhh you beaut :op cheers for that matey

 
Comment by Webber
2006-11-21 09:08:41

maybe a nice tutorial on how to make a timer now? plz? xD

 
Comment by awesty
2006-11-21 15:04:55

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.

 
Comment by Bob
2006-11-21 21:47:51

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

 
Comment by awesty
2006-11-22 20:23:50

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.

 
Comment by joey
2006-11-26 10:04:19

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

 
Comment by awesty
2006-11-27 16:15:17

I will write it tonight. Maybe start it in an hour or so.

 
Comment by RDB2006
2006-12-10 23:26:57

ty! that was a gd tut! Wud be gd to use for a scene where your character walks on glue or sumfing

 
Comment by RDB2006
2006-12-10 23:28:30

u know millions about flash.. maybe one day u cud put all ur tutorials and knowlage in 2 a game we cud all play :D

 
Comment by awesty
2006-12-11 15:51:56

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

 
Comment by RDB2006
2006-12-14 06:43:17

so r u makin n e new tuts at the momment?

 
Comment by awesty
2006-12-15 12:07:27

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.

 
Comment by Tai
2006-12-19 07:56:36

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

 
Comment by Tai
2006-12-19 07:58:49

and heres the object code im trying to make it speed up

onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_global.speed = 10;
}
}

 
Comment by awesty
2006-12-19 17:46:33

Okay, in your code, you have this._x -= 4 AND this._x -= speed. You only need the speed in there.

 
Comment by Tai
2006-12-20 07:09:26

it still doesnt work :(

 
Comment by awesty
2006-12-20 09:14:54

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.

 
Comment by Tai
2006-12-21 07:32:42

yep i its fixed thxs

 
Comment by Bryce K :P
2006-12-30 10:33:14

Hey Awesty im making a racing game and i didn’t know were to put it so i decided to put it here :P 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 :P

 
Comment by awesty
2006-12-31 13:18:05

Yep, just send it to me.

 
Comment by no1jock
2007-01-03 01:27:13

when two movie clips hit each other, your man goes behind it. Is there anyway to make it stay ontop of the other mc?

 
Comment by Bryce K
2007-01-03 09:10:36

@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

 
Comment by redfeatherraven
2007-01-08 05:17:46

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.

 
Comment by Grifo
2007-01-12 06:32:08

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.

 
Comment by Timmy
2007-01-13 07:20:18

How do u make it so if u press a button then it will speed up??

 
Comment by awesty
2007-01-13 16:04:59

@redfeatherraven: Yea, I know what you mean. It would work.

@Timmy:

if(Key.isDown(SOMEKEY)){
_global.speed = NUMBER;
}

 
Comment by Grifo
2007-01-15 06:35:54

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.

 
Comment by awesty
2007-01-15 14:05:38

@Grifo: Can you please post that here:

http://www.awestyproductions.c.....s/request/

Otherwise I will forget.

 
Comment by yoga
2007-01-15 15:40:25

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

 
Comment by m7hj5
2007-01-16 09:15:20

Is it possible to make a movieclip stop on its last frame?

if it is could u plez send me the script for it

 
Comment by awesty
2007-01-16 10:09:37

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

 
Comment by yoga
2007-01-17 11:21:30

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

 
Comment by yoga
2007-01-17 11:51:53

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.

 
Comment by awesty
2007-01-20 11:33:24

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.

 
Comment by Timmy
2007-01-29 08:27:12

yah the code u gave me before dosent work

 
Comment by awesty
2007-01-29 08:39:04

Did you change SOMEKEY and NUMBER?

 
Comment by Timmy
2007-01-30 00:26:50

yes..lol

 
Comment by Kurt
2007-02-13 11:47:37

do you know how to make them disappear if it gets touched?

 
Comment by awesty
2007-02-13 16:00:22

Yes.

if(something hits this){
this.swapDepths(getNextHighestDepth());
this.removeMovieClip();
}

 
Comment by Karan K
2007-02-14 07:01:41

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:

 
Comment by Karan K
2007-02-14 07:02:20

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

 
Comment by Karan K
2007-02-14 07:03:17

OMG, double post, please delete it and this.
Sorry.

 
Comment by Kurt
2007-02-14 09:47:44

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

 
Comment by Kurt
2007-02-14 09:52:08

oops likes should be like

 
Comment by awesty
2007-02-14 16:39:07

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

 
Comment by Karan K
2007-02-19 08:50:19

Karan* xD
Thanks =D

 
Comment by Trunk Monkey
2007-04-28 07:56:54

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

Comment by awesty
2007-05-01 20:42:26

Dont use negative numbers. That would make it go in the wrong direction.

 
 
Comment by samin hood Subscribed to comments via email
2007-05-17 05:38:33

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!

Comment by Warrantica Subscribed to comments via email
2007-06-26 22:48:03

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

 
Comment by awesty
2007-07-03 18:00:23

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.

 
 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.