STORY   LOOP   FURRY   PORN   GAMES
• C •   SERVICES [?] [R] RND   POPULAR
Archived flashes:
228072
/disc/ · /res/     /show/ · /fap/ · /gg/ · /swf/P0001 · P2560 · P5120

<div style="position:absolute;top:-99px;left:-99px;"><img src="http://swfchan.com:57475/78562943?noj=FRM78562943-29DC" width="1" height="1"></div>

Flash Physics Tutorial.swf

This is the info page for
Flash #68373

(Click the ID number above for more basic data on this flash file.)


Text
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 2
stop();
Instance of Symbol 89 MovieClip "ball" in Frame 5
onClipEvent (load) { xv = 0; yv = 0; g = 0.6; f = 0.9; }
Instance of Symbol 89 MovieClip "ball" in Frame 7
onClipEvent (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 11
onClipEvent (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 12
onClipEvent (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 13
onClipEvent (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 14
onClipEvent (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 16
onClipEvent (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 17
onClipEvent (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 20
onClipEvent (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 24
onClipEvent (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 27
onClipEvent (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 32
onClipEvent (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 36
onClipEvent (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 Button
on (release) { getURL ("http://www.newgrounds.com", "_blank"); }
Symbol 48 MovieClip Frame 40
stop();
Symbol 74 Button
on (release) { _root.play(); }
Symbol 75 MovieClip Frame 1
function 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 51
Symbol 85 Button
on (release) { _root.nextFrame(); }
Symbol 91 Button
on (release) { _root.prevFrame(); }
Symbol 137 Button
on (release) { getURL ("http://stickyonline.tk", "_blank"); }

Library Items

Symbol 1 GraphicUsed by:Timeline
Symbol 2 GraphicUsed by:75
Symbol 3 GraphicUsed by:75
Symbol 4 GraphicUsed by:5
Symbol 5 MovieClipUses:4Used by:75
Symbol 6 GraphicUsed by:8
Symbol 7 GraphicUsed by:8
Symbol 8 MovieClipUses:6 7Used by:75
Symbol 9 GraphicUsed by:75
Symbol 10 GraphicUsed by:75
Symbol 11 GraphicUsed by:75
Symbol 12 GraphicUsed by:19 48
Symbol 13 GraphicUsed by:19 48
Symbol 14 GraphicUsed by:19 48
Symbol 15 GraphicUsed by:19 48
Symbol 16 GraphicUsed by:19 48
Symbol 17 GraphicUsed by:19 48
Symbol 18 GraphicUsed by:19 48
Symbol 19 MovieClipUses:12 13 14 15 16 17 18Used by:75
Symbol 20 GraphicUsed by:75
Symbol 21 GraphicUsed by:22
Symbol 22 MovieClipUses:21Used by:75
Symbol 23 GraphicUsed by:24
Symbol 24 MovieClipUses:23Used by:75
Symbol 25 GraphicUsed by:29
Symbol 26 GraphicUsed by:29
Symbol 27 GraphicUsed by:28
Symbol 28 MovieClipUses:27Used by:29 32
Symbol 29 MovieClipUses:25 26 28Used by:43
Symbol 30 GraphicUsed by:32
Symbol 31 GraphicUsed by:32
Symbol 32 MovieClipUses:30 31 28Used by:43
Symbol 33 GraphicUsed by:36
Symbol 34 GraphicUsed by:36 39
Symbol 35 GraphicUsed by:36
Symbol 36 MovieClipUses:33 34 35Used by:40
Symbol 37 GraphicUsed by:39
Symbol 38 GraphicUsed by:39
Symbol 39 MovieClipUses:37 34 38Used by:40
Symbol 40 MovieClipUses:36 39Used by:43
Symbol 41 GraphicUsed by:42
Symbol 42 MovieClipUses:41Used by:43
Symbol 43 MovieClipUses:29 32 40 42Used by:75
Symbol 44 GraphicUsed by:75
Symbol 45 GraphicUsed by:46
Symbol 46 ButtonUses:45Used by:75
Symbol 47 GraphicUsed by:48
Symbol 48 MovieClipUses:12 13 14 15 16 17 18 47Used by:75
Symbol 49 GraphicUsed by:75
Symbol 50 GraphicUsed by:75
Symbol 51 GraphicUsed by:75
Symbol 52 GraphicUsed by:75
Symbol 53 GraphicUsed by:75
Symbol 54 GraphicUsed by:75
Symbol 55 GraphicUsed by:75
Symbol 56 GraphicUsed by:75
Symbol 57 GraphicUsed by:75
Symbol 58 GraphicUsed by:75
Symbol 59 GraphicUsed by:75
Symbol 60 GraphicUsed by:75
Symbol 61 ShapeTweeningUsed by:75
Symbol 62 GraphicUsed by:75
Symbol 63 ShapeTweeningUsed by:75
Symbol 64 ShapeTweeningUsed by:75
Symbol 65 GraphicUsed by:75
Symbol 66 GraphicUsed by:74
Symbol 67 GraphicUsed by:74
Symbol 68 GraphicUsed by:74
Symbol 69 GraphicUsed by:74
Symbol 70 GraphicUsed by:74
Symbol 71 GraphicUsed by:73
Symbol 72 GraphicUsed by:73
Symbol 73 MovieClipUses:71 72Used by:74
Symbol 74 ButtonUses:66 67 68 69 70 73Used by:75
Symbol 75 MovieClipUses: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 74Used by:Timeline
Symbol 76 ShapeTweeningUsed by:81
Symbol 77 ShapeTweeningUsed by:81
Symbol 78 ShapeTweeningUsed by:81
Symbol 79 ShapeTweeningUsed by:81
Symbol 80 GraphicUsed by:81
Symbol 81 MovieClipUses:76 77 78 79 80Used by:Timeline
Symbol 82 FontUsed 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 TextUses:82Used by:Timeline
Symbol 84 GraphicUsed by:85
Symbol 85 ButtonUses:84Used by:Timeline
Symbol 86 SoundUsed by:Timeline
Symbol 87 TextUses:82Used by:Timeline
Symbol 88 GraphicUsed by:89
Symbol 89 MovieClipUses:88Used by:Timeline
Symbol 90 GraphicUsed by:91
Symbol 91 ButtonUses:90Used by:Timeline
Symbol 92 TextUses:82Used by:Timeline
Symbol 93 TextUses:82Used by:Timeline
Symbol 94 TextUses:82Used by:Timeline
Symbol 95 TextUses:82Used by:Timeline
Symbol 96 TextUses:82Used by:Timeline
Symbol 97 TextUses:82Used by:Timeline
Symbol 98 TextUses:82Used by:Timeline
Symbol 99 TextUses:82Used by:Timeline
Symbol 100 TextUses:82Used by:Timeline
Symbol 101 TextUses:82Used by:Timeline
Symbol 102 TextUses:82Used by:Timeline
Symbol 103 TextUses:82Used by:Timeline
Symbol 104 GraphicUsed by:Timeline
Symbol 105 TextUses:82Used by:Timeline
Symbol 106 TextUses:82Used by:Timeline
Symbol 107 TextUses:82Used by:Timeline
Symbol 108 TextUses:82Used by:Timeline
Symbol 109 TextUses:82Used by:Timeline
Symbol 110 TextUses:82Used by:Timeline
Symbol 111 TextUses:82Used by:Timeline
Symbol 112 TextUses:82Used by:Timeline
Symbol 113 TextUses:82Used by:Timeline
Symbol 114 TextUses:82Used by:Timeline
Symbol 115 TextUses:82Used by:Timeline
Symbol 116 TextUses:82Used by:Timeline
Symbol 117 TextUses:82Used by:Timeline
Symbol 118 TextUses:82Used by:Timeline
Symbol 119 TextUses:82Used by:Timeline
Symbol 120 TextUses:82Used by:Timeline
Symbol 121 TextUses:82Used by:Timeline
Symbol 122 TextUses:82Used by:Timeline
Symbol 123 TextUses:82Used by:Timeline
Symbol 124 TextUses:82Used by:Timeline
Symbol 125 TextUses:82Used by:Timeline
Symbol 126 TextUses:82Used by:Timeline
Symbol 127 TextUses:82Used by:Timeline
Symbol 128 TextUses:82Used by:Timeline
Symbol 129 TextUses:82Used by:Timeline
Symbol 130 TextUses:82Used by:Timeline
Symbol 131 GraphicUsed by:Timeline
Symbol 132 TextUses:82Used by:Timeline
Symbol 133 GraphicUsed by:137
Symbol 134 GraphicUsed by:137
Symbol 135 GraphicUsed by:137
Symbol 136 GraphicUsed by:137
Symbol 137 ButtonUses:133 134 135 136Used by:Timeline

Instance Names

"ball"Frame 3Symbol 89 MovieClip
"ball"Frame 5Symbol 89 MovieClip
"ball"Frame 7Symbol 89 MovieClip
"ball"Frame 11Symbol 89 MovieClip
"ball"Frame 12Symbol 89 MovieClip
"ball"Frame 13Symbol 89 MovieClip
"ball"Frame 14Symbol 89 MovieClip
"ball"Frame 16Symbol 89 MovieClip
"ball"Frame 17Symbol 89 MovieClip
"ball"Frame 20Symbol 89 MovieClip
"ball"Frame 24Symbol 89 MovieClip
"ball"Frame 27Symbol 89 MovieClip
"ball"Frame 32Symbol 89 MovieClip
"ball"Frame 36Symbol 89 MovieClip
"bar"Symbol 75 MovieClip Frame 1Symbol 5 MovieClip
"bargfx"Symbol 75 MovieClip Frame 1Symbol 8 MovieClip
"tank"Symbol 75 MovieClip Frame 1Symbol 22 MovieClip
"bargfx"Symbol 75 MovieClip Frame 2Symbol 8 MovieClip
"bargfx"Symbol 75 MovieClip Frame 20Symbol 8 MovieClip

Special Tags

FileAttributes (69)Timeline Frame 1Access local files only, Metadata not present, AS1/AS2.

Labels

"LOAD"Symbol 75 MovieClip Frame 1
"COMPLETE_STOP"Symbol 75 MovieClip Frame 2




http://swfchan.com/14/68373/info.shtml
Created: 11/4 -2019 01:38:34 Last modified: 11/4 -2019 01:38:34 Server time: 29/04 -2024 07:20:37