STORY LOOP FURRY PORN GAMES C SERVICES [?] [R] RND POPULAR | Archived flashes: 227917 |
/disc/ · /res/ — /show/ · /fap/ · /gg/ · /swf/ | P0001 · P2559 · P5117 |
This is the info page for Flash #68373 |
Welcome to the Basic Flash Physics tutorial. This tutorial will teach you how to easily use physics in your Flash games and projects. Click the arrow to continue. |
First, you'll want to make an object: a circle. Make a circle, highlight it, and press F8. Make it a Movie Clip (MC). Name it "ball" and give it the instance name (at the top left corner of the Properties tab) of "ball". |
Now, go to the actions tab with the ball highlighted and type this in: |
onClipEvent(load){ yv = 0; xv = 0; g = 0.6; f = 0.9; } |
Oh no! It's not copy-able! You'll learn more if you type it in yourself. So do it! |
Explanation |
Line 1: When the MC loads Line 2-3: Y and X velocities Line 4: Gravity Line 5: Friction |
Now, if you play the Flash, nothing will happen. We'll get to that later. I know you want to actually make it move, but all of this is neccessary. Trust me. |
More code. Add this to previous code. onClipEvent(enterFrame){ if (Key.isDown(Key.RIGHT)) { xv += 2; } if (Key.isDown(Key.LEFT)) { xv -= 2; } } |
Explanation onClipEvent(enterFrame){ if(Key.isDown(Key.RIGHT)){ xv++; } if (Key.isDown(Key.LEFT)){ xv--; } } Line 1: On every frame of the MC Line 2-4: If Right is down, increase the X velocity by 2. |
Explanation Continued onClipEvent(enterFrame){ if(Key.isDown(Key.RIGHT)){ xv++; } if (Key.isDown(Key.LEFT)){ xv--; } } Line 5-7: When left is down, decrease the X velocity by 2. |
Now, this STILL doesn't do anything. Don't bite my head off, though. It will all make sense in the end. |
Add this onto the previous code right before the last bracket ( } ) if(Key.isDown(Key.UP)){ yv-= 2; } if (Key.isDown(Key.DOWN)){ yv+=2; } |
Explanation if(Key.isDown(Key.UP)){ yv -= 2; } if (Key.isDown(Key.DOWN)){ yv += 2; } Line 1-3: If UP is down, decrease the Y velocity by 2. Line 4-6: If DOWN is down, increase the Y velocity by 2. |
This will STILL NOT DO ANYTHING!! AHHH!!! Yeah, that's probably what your doing right now, right? No? Fine. By the way: As I'm making this tutorial, I'm filling in the code on this ball over here. |
Now, to make this ball move! Add this code before the last bracket ( } ). _x += xv; _y += yv; |
Explanation: _x += xv; _y += yv; Line 1: Add the value of xv to the ball's x coordinates. If the x velocity is UNDER 0, the ball will move left. If it is over 0, the ball will move right. |
Explanation continued: _x += xv; _y += yv; Line 2: Do the same with the yv and the y coordinates. If the y velocity is under 0, the ball will move up. If it is over 0, it will move down. |
Use the arrow keys to move my ball. |
There will be three problems with your ball. The first problem is it will move very stiffly. It will be jumpy, and you want it smooth! Mine is fixed, but I will show you how it's done. The second problem is it will never stop! Don't worry, we'll get to that. The last problem is it will never fall. It will just float. Sometimes you want that, but I want my ball to fall. |
I will address the problems one at a time. The first one - the easiest - is a problem with the frame rate. Click the selection tool (or press V). On the bottom of your screen, in the properties tab, you will see "Framerate: 12 fps". Change the 12 to 18, 24, or 30. I changed mine to 24 and it's pretty smooth. |
Now your ball will be like mine. |
Next, the stopping problem. This can be fixed with a few lines of code. Add this before the last bracket ( } ) if (!Key.isDown(Key.LEFT)&&!Key.isDown(Key.RIGHT)){ xv *= f; } if (!Key.isDown(Key.UP)&&!Key.isDown(Key.DOWN yv *= f; } |
Next, the stopping problem. This can be fixed with a few lines of code. Add this before the last bracket ( } ) if (!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) { xv *= f; } if (!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) { yv *= f; } |
Lines 1-3: When neither RIGHT nor LEFT are down, multiply the X velocity by the value of f (friction). Lines 4-6: When neither UP nor DOWN are down, multiply the Y velocity by the value of f. Math lesson: Multiply a whole number by a decimal and you will get a smaller number. Lowering the velocities should slow the ball down. |
This will be the result. |
The ball will still not stop; it will just slow down a lot. Just add this code before the last bracket ( } ). if (xv > -1 && xv < 1){ xv = 0; } if (yv > -1 && yv < 1){ yv = 0; } |
Explanation if (xv > -1 && xv < 1){ xv = 0; } if (yv > -1 && yv < 1){ yv = 0; } Lines 1-3: If the xv is greater than -1 and less than 1, stop the ball. Lines 4-6: Do the same with the yv. |
There, now it stops. Finally! |
Now, the falling problem. This part is COMPLETELY OPTIONAL. If you don't want the ball to fall, just skip through this part. |
First, you'll want to take out the following code. if (yv>-1 && yv<1) { yv = 0; } AND if (!Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) { yv *= f; } |
Then, replace the code with... yv += g; All this does is add the value of g (gravity) to the Y velocity. |
Here it is with gravity. If the ball falls offscreen, go to the previous slide and then come back to this. It will be back! |
We still have a problem, though. The ball falls offscreen if you're not careful!! We can fix this with a few easy lines of code. |
if (_x >= 550 - (_width/2) || _x <= 0 + (_width/2)){ xv = -xv; } if (_y >= 400 - (_width/2) || _y <= 0 + (_width/2)){ yv = -yv; } |
Explanation: |
Lines 1-3: If the edge of the ball goes outside of the stage on the right or left side, reverse its X velocity. |
Explanation continued: |
Lines 4-6: If the edge of the ball goes outside of the stage on the top or bottom of the ball, reverse its Y velocity. |
As you suggested, I put this in the tutorial. Here's what happens! |
Well, this tutorial is over. Done. Told you it was easy. There are ways to keep the ball from going offscreen; you know, collision detection. If you want to learn how, send me a PM, or check out some other tutorials on www.emanueleferonato.com. That guy is amazing at Flash! Also, visit me at |
ActionScript [AS1/AS2]
Frame 2stop();Instance of Symbol 89 MovieClip "ball" in Frame 5onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; }Instance of Symbol 89 MovieClip "ball" in Frame 7onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv++; } if (Key.isDown(37)) { xv--; } }Instance of Symbol 89 MovieClip "ball" in Frame 11onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv++; } if (Key.isDown(37)) { xv--; } }Instance of Symbol 89 MovieClip "ball" in Frame 12onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv++; } if (Key.isDown(37)) { xv--; } }Instance of Symbol 89 MovieClip "ball" in Frame 13onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } }Instance of Symbol 89 MovieClip "ball" in Frame 14onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv++; } if (Key.isDown(37)) { xv--; } if (Key.isDown(38)) { yv--; } if (Key.isDown(40)) { yv++; } }Instance of Symbol 89 MovieClip "ball" in Frame 16onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv++; } if (Key.isDown(37)) { xv--; } if (Key.isDown(38)) { yv--; } if (Key.isDown(40)) { yv++; } _x = (_x + xv); _y = (_y + yv); }Instance of Symbol 89 MovieClip "ball" in Frame 17onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } _x = (_x + xv); _y = (_y + yv); }Instance of Symbol 89 MovieClip "ball" in Frame 20onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } _x = (_x + xv); _y = (_y + yv); }Instance of Symbol 89 MovieClip "ball" in Frame 24onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } _x = (_x + xv); _y = (_y + yv); if ((!Key.isDown(37)) && (!Key.isDown(39))) { xv = xv * f; } if ((!Key.isDown(38)) && (!Key.isDown(40))) { yv = yv * f; } }Instance of Symbol 89 MovieClip "ball" in Frame 27onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } _x = (_x + xv); _y = (_y + yv); if ((!Key.isDown(37)) && (!Key.isDown(39))) { xv = xv * f; } if ((!Key.isDown(38)) && (!Key.isDown(40))) { yv = yv * f; } if ((xv > -1) && (xv < 1)) { xv = 0; } if ((yv > -1) && (yv < 1)) { yv = 0; } }Instance of Symbol 89 MovieClip "ball" in Frame 32onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } _x = (_x + xv); _y = (_y + yv); if ((!Key.isDown(37)) && (!Key.isDown(39))) { xv = xv * f; } if ((xv > -1) && (xv < 1)) { xv = 0; } yv = yv + g; }Instance of Symbol 89 MovieClip "ball" in Frame 36onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; } onClipEvent (enterFrame) { if (Key.isDown(39)) { xv = xv + 2; } if (Key.isDown(37)) { xv = xv - 2; } if (Key.isDown(38)) { yv = yv - 2; } if (Key.isDown(40)) { yv = yv + 2; } _x = (_x + xv); _y = (_y + yv); if ((!Key.isDown(37)) && (!Key.isDown(39))) { xv = xv * f; } if ((xv > -1) && (xv < 1)) { xv = 0; } if ((_x >= (550 - (_width / 2))) || (_x <= (0 + (_width / 2)))) { xv = -xv; } if ((_y >= (400 - (_width / 2))) || (_y <= (0 + (_width / 2)))) { yv = -yv; } yv = yv + g; }Symbol 46 Buttonon (release) { getURL ("http://www.newgrounds.com", "_blank"); }Symbol 48 MovieClip Frame 40stop();Symbol 74 Buttonon (release) { _root.play(); }Symbol 75 MovieClip Frame 1function onEnterFrame() { if (!loaded) { var _local3 = _root.getBytesLoaded() / _root.getBytesTotal(); if (_local3 >= 1) { play(); bar._x = initX; loaded = true; } else { bar._x = initX + ((_local3 - 1) * bar._width); } } var _local4 = getTimer() - time; timeAccum = timeAccum + _local4; while (timeAccum >= FRAME_TIME) { var _local2 = 0; while (_local2 < timeClips.length) { if (timeClips[_local2]._currentframe < timeClips[_local2]._totalframes) { timeClips[_local2].nextFrame(); } else { timeClips[_local2].gotoAndStop(1); } _local2++; } if (loaded && (_currentframe < _totalframes)) { nextFrame(); } timeAccum = timeAccum - FRAME_TIME; } time = time + _local4; } stop(); _root.stop(); var initX = bar._x; var time = getTimer(); var FRAME_TIME = 33.3333333333333; var timeAccum = 0; var loaded = false; timeClips = [bargfx, tank.mc0, tank.mc1, tank.mc2, tank.mc3, tank.mc4, tank.mc4.mc0, tank.mc4.mc1, tank.mc4.mc0.mc0, tank.mc4.mc0.mc0.mc0.mc0, tank.mc4.mc0.mc0.mc0.mc1, tank.mc4.mc0.mc0.mc0.mc2, tank.mc4.mc0.mc0.mc0.mc3, tank.mc4.mc0.mc0.mc1, tank.mc4.mc0.mc0.mc2, tank.mc5.mc0]; var i = 0; while (i < timeClips.length) { timeClips[i].stop(); i++; }Symbol 75 MovieClip Frame 51Symbol 85 Buttonon (release) { _root.nextFrame(); }Symbol 91 Buttonon (release) { _root.prevFrame(); }Symbol 137 Buttonon (release) { getURL ("http://stickyonline.tk", "_blank"); }
Library Items
Symbol 1 Graphic | Used by:Timeline | |
Symbol 2 Graphic | Used by:75 | |
Symbol 3 Graphic | Used by:75 | |
Symbol 4 Graphic | Used by:5 | |
Symbol 5 MovieClip | Uses:4 | Used by:75 |
Symbol 6 Graphic | Used by:8 | |
Symbol 7 Graphic | Used by:8 | |
Symbol 8 MovieClip | Uses:6 7 | Used by:75 |
Symbol 9 Graphic | Used by:75 | |
Symbol 10 Graphic | Used by:75 | |
Symbol 11 Graphic | Used by:75 | |
Symbol 12 Graphic | Used by:19 48 | |
Symbol 13 Graphic | Used by:19 48 | |
Symbol 14 Graphic | Used by:19 48 | |
Symbol 15 Graphic | Used by:19 48 | |
Symbol 16 Graphic | Used by:19 48 | |
Symbol 17 Graphic | Used by:19 48 | |
Symbol 18 Graphic | Used by:19 48 | |
Symbol 19 MovieClip | Uses:12 13 14 15 16 17 18 | Used by:75 |
Symbol 20 Graphic | Used by:75 | |
Symbol 21 Graphic | Used by:22 | |
Symbol 22 MovieClip | Uses:21 | Used by:75 |
Symbol 23 Graphic | Used by:24 | |
Symbol 24 MovieClip | Uses:23 | Used by:75 |
Symbol 25 Graphic | Used by:29 | |
Symbol 26 Graphic | Used by:29 | |
Symbol 27 Graphic | Used by:28 | |
Symbol 28 MovieClip | Uses:27 | Used by:29 32 |
Symbol 29 MovieClip | Uses:25 26 28 | Used by:43 |
Symbol 30 Graphic | Used by:32 | |
Symbol 31 Graphic | Used by:32 | |
Symbol 32 MovieClip | Uses:30 31 28 | Used by:43 |
Symbol 33 Graphic | Used by:36 | |
Symbol 34 Graphic | Used by:36 39 | |
Symbol 35 Graphic | Used by:36 | |
Symbol 36 MovieClip | Uses:33 34 35 | Used by:40 |
Symbol 37 Graphic | Used by:39 | |
Symbol 38 Graphic | Used by:39 | |
Symbol 39 MovieClip | Uses:37 34 38 | Used by:40 |
Symbol 40 MovieClip | Uses:36 39 | Used by:43 |
Symbol 41 Graphic | Used by:42 | |
Symbol 42 MovieClip | Uses:41 | Used by:43 |
Symbol 43 MovieClip | Uses:29 32 40 42 | Used by:75 |
Symbol 44 Graphic | Used by:75 | |
Symbol 45 Graphic | Used by:46 | |
Symbol 46 Button | Uses:45 | Used by:75 |
Symbol 47 Graphic | Used by:48 | |
Symbol 48 MovieClip | Uses:12 13 14 15 16 17 18 47 | Used by:75 |
Symbol 49 Graphic | Used by:75 | |
Symbol 50 Graphic | Used by:75 | |
Symbol 51 Graphic | Used by:75 | |
Symbol 52 Graphic | Used by:75 | |
Symbol 53 Graphic | Used by:75 | |
Symbol 54 Graphic | Used by:75 | |
Symbol 55 Graphic | Used by:75 | |
Symbol 56 Graphic | Used by:75 | |
Symbol 57 Graphic | Used by:75 | |
Symbol 58 Graphic | Used by:75 | |
Symbol 59 Graphic | Used by:75 | |
Symbol 60 Graphic | Used by:75 | |
Symbol 61 ShapeTweening | Used by:75 | |
Symbol 62 Graphic | Used by:75 | |
Symbol 63 ShapeTweening | Used by:75 | |
Symbol 64 ShapeTweening | Used by:75 | |
Symbol 65 Graphic | Used by:75 | |
Symbol 66 Graphic | Used by:74 | |
Symbol 67 Graphic | Used by:74 | |
Symbol 68 Graphic | Used by:74 | |
Symbol 69 Graphic | Used by:74 | |
Symbol 70 Graphic | Used by:74 | |
Symbol 71 Graphic | Used by:73 | |
Symbol 72 Graphic | Used by:73 | |
Symbol 73 MovieClip | Uses:71 72 | Used by:74 |
Symbol 74 Button | Uses:66 67 68 69 70 73 | Used by:75 |
Symbol 75 MovieClip | Uses:2 3 5 8 9 10 11 19 20 22 24 43 44 46 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 74 | Used by:Timeline |
Symbol 76 ShapeTweening | Used by:81 | |
Symbol 77 ShapeTweening | Used by:81 | |
Symbol 78 ShapeTweening | Used by:81 | |
Symbol 79 ShapeTweening | Used by:81 | |
Symbol 80 Graphic | Used by:81 | |
Symbol 81 MovieClip | Uses:76 77 78 79 80 | Used by:Timeline |
Symbol 82 Font | Used by:83 87 92 93 94 95 96 97 98 99 100 101 102 103 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 132 | |
Symbol 83 Text | Uses:82 | Used by:Timeline |
Symbol 84 Graphic | Used by:85 | |
Symbol 85 Button | Uses:84 | Used by:Timeline |
Symbol 86 Sound | Used by:Timeline | |
Symbol 87 Text | Uses:82 | Used by:Timeline |
Symbol 88 Graphic | Used by:89 | |
Symbol 89 MovieClip | Uses:88 | Used by:Timeline |
Symbol 90 Graphic | Used by:91 | |
Symbol 91 Button | Uses:90 | Used by:Timeline |
Symbol 92 Text | Uses:82 | Used by:Timeline |
Symbol 93 Text | Uses:82 | Used by:Timeline |
Symbol 94 Text | Uses:82 | Used by:Timeline |
Symbol 95 Text | Uses:82 | Used by:Timeline |
Symbol 96 Text | Uses:82 | Used by:Timeline |
Symbol 97 Text | Uses:82 | Used by:Timeline |
Symbol 98 Text | Uses:82 | Used by:Timeline |
Symbol 99 Text | Uses:82 | Used by:Timeline |
Symbol 100 Text | Uses:82 | Used by:Timeline |
Symbol 101 Text | Uses:82 | Used by:Timeline |
Symbol 102 Text | Uses:82 | Used by:Timeline |
Symbol 103 Text | Uses:82 | Used by:Timeline |
Symbol 104 Graphic | Used by:Timeline | |
Symbol 105 Text | Uses:82 | Used by:Timeline |
Symbol 106 Text | Uses:82 | Used by:Timeline |
Symbol 107 Text | Uses:82 | Used by:Timeline |
Symbol 108 Text | Uses:82 | Used by:Timeline |
Symbol 109 Text | Uses:82 | Used by:Timeline |
Symbol 110 Text | Uses:82 | Used by:Timeline |
Symbol 111 Text | Uses:82 | Used by:Timeline |
Symbol 112 Text | Uses:82 | Used by:Timeline |
Symbol 113 Text | Uses:82 | Used by:Timeline |
Symbol 114 Text | Uses:82 | Used by:Timeline |
Symbol 115 Text | Uses:82 | Used by:Timeline |
Symbol 116 Text | Uses:82 | Used by:Timeline |
Symbol 117 Text | Uses:82 | Used by:Timeline |
Symbol 118 Text | Uses:82 | Used by:Timeline |
Symbol 119 Text | Uses:82 | Used by:Timeline |
Symbol 120 Text | Uses:82 | Used by:Timeline |
Symbol 121 Text | Uses:82 | Used by:Timeline |
Symbol 122 Text | Uses:82 | Used by:Timeline |
Symbol 123 Text | Uses:82 | Used by:Timeline |
Symbol 124 Text | Uses:82 | Used by:Timeline |
Symbol 125 Text | Uses:82 | Used by:Timeline |
Symbol 126 Text | Uses:82 | Used by:Timeline |
Symbol 127 Text | Uses:82 | Used by:Timeline |
Symbol 128 Text | Uses:82 | Used by:Timeline |
Symbol 129 Text | Uses:82 | Used by:Timeline |
Symbol 130 Text | Uses:82 | Used by:Timeline |
Symbol 131 Graphic | Used by:Timeline | |
Symbol 132 Text | Uses:82 | Used by:Timeline |
Symbol 133 Graphic | Used by:137 | |
Symbol 134 Graphic | Used by:137 | |
Symbol 135 Graphic | Used by:137 | |
Symbol 136 Graphic | Used by:137 | |
Symbol 137 Button | Uses:133 134 135 136 | Used by:Timeline |
Instance Names
"ball" | Frame 3 | Symbol 89 MovieClip |
"ball" | Frame 5 | Symbol 89 MovieClip |
"ball" | Frame 7 | Symbol 89 MovieClip |
"ball" | Frame 11 | Symbol 89 MovieClip |
"ball" | Frame 12 | Symbol 89 MovieClip |
"ball" | Frame 13 | Symbol 89 MovieClip |
"ball" | Frame 14 | Symbol 89 MovieClip |
"ball" | Frame 16 | Symbol 89 MovieClip |
"ball" | Frame 17 | Symbol 89 MovieClip |
"ball" | Frame 20 | Symbol 89 MovieClip |
"ball" | Frame 24 | Symbol 89 MovieClip |
"ball" | Frame 27 | Symbol 89 MovieClip |
"ball" | Frame 32 | Symbol 89 MovieClip |
"ball" | Frame 36 | Symbol 89 MovieClip |
"bar" | Symbol 75 MovieClip Frame 1 | Symbol 5 MovieClip |
"bargfx" | Symbol 75 MovieClip Frame 1 | Symbol 8 MovieClip |
"tank" | Symbol 75 MovieClip Frame 1 | Symbol 22 MovieClip |
"bargfx" | Symbol 75 MovieClip Frame 2 | Symbol 8 MovieClip |
"bargfx" | Symbol 75 MovieClip Frame 20 | Symbol 8 MovieClip |
Special Tags
FileAttributes (69) | Timeline Frame 1 | Access local files only, Metadata not present, AS1/AS2. |
Labels
"LOAD" | Symbol 75 MovieClip Frame 1 |
"COMPLETE_STOP" | Symbol 75 MovieClip Frame 2 |
|