Choose fontsize:
Welcome, Guest. Please login or register.
Did you miss your activation email?

 

Pages: [1] 2  All   Go Down
  Print  
Author Topic: Basic Movement  (Read 25546 times)
0 Members and 1 Guest are viewing this topic.
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« on: September 04, 2005, 03:11:47 PM »
[h1]Basic Movement[/h1]
Okay, in this tutorial, I will teach you how to make a MC move.

*Notes*
I used Macromedia Flash MX 2004 for this tutorial.
This is the Timeline:
I expect you to know what a variable is,
30 FPS (frames per second) is what I always use for games, and you should too (it's nice and smooth).


Part one: Basic Movement Script
Okay, I'll explain everything about basic movement.

The Syntax:
When writing any script involving Movie Clips, we need to make them first! So, let's make a circle, like this one (20x20 circle, use the circle tool):


Okay, so we have a circle, now let’s convert it to a Movie Clip. To do this, select the circle and press F8. As soon as you press F8, it'll ask you to name it, let's name this one hero, and don't forget to check the 'Movie Clip' radial button!
This is what is should look like.


Okay, we have our hero Movie Clip, now we need to make him move! We'll use the arrow keys instead of W, A, S, D.

Select the hero Movie Clip on the Main Frame, and click the 'Actions' bar:

Okay, so, now we have a Movie Clip, but no actions on him! Let's add the movement script I've made:
Code:
onClipEvent (load) {
_root.up=true
_root.down=true
_root.left=true
_root.right=true
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP) && _root.up == true) {
_y-=5
}
if (Key.isDown(Key.DOWN) && _root.down == true) {
_y+=5
}
if (Key.isDown(Key.LEFT) && _root.left == true) {
_x-=5
}
if (Key.isDown(Key.RIGHT) && _root.right == true) {
_x+=5
}
}

Okay, now I have to break this up, and explain it:

Code:
onClipEvent (load) {

When the Movie Clip first enters the frame...

Code:
_root.up=true
_root.down=true
_root.left=true
_root.right=true

The variables up, down, left, and right get set to true.

Code:
onClipEvent (enterFrame) {

As long as the Movie Clip is in the current frame (there's only one frame!)...

Code:
if (Key.isDown(Key.UP) && _root.up == true) {
_y-=5
}
if (Key.isDown(Key.DOWN) && _root.down == true) {
_y+=5
}
if (Key.isDown(Key.LEFT) && _root.left == true) {
_x-=5
}
if (Key.isDown(Key.RIGHT) && _root.right == true) {
_x+=5
}

Here's the hard part, but this is what it means:
If the up key, is pressed down "if (Key.isDown(Key.UP)" and "&&" the variable _root.up is set to true "_root.up == true" then the Movie Clip hero's current _y position gets subtracted by 5, the _y+=5 stores the value of (x += 5) in the variable x, so that's how we get the Movie Clip to move! The same for the other; left, down, right and up. Here's a neat little table to explain the _x and _y positions:

Position_x_y
+=5RightDown
-=5LeftUp
Final Product:
<a href="http://www.graphicaddicts.net/ssj//Basic%20Movement.swf" target="_blank">http://www.graphicaddicts.net/ssj//Basic%20Movement.swf</a>
Okay, so there’s our basic movement script and my explanation of it.
Please, save the .fla you've made. I'll basically use the exact same script in my hitTest tutorial.
hitTest tutorial is finished! Take a look:hitTest Tutorial

If you like this tutorial, please Click Here to register.

Click the link below to download the Flash file for this tutorial.
Basic Movement FLA
« Last Edit: February 10, 2006, 05:53:26 PM by ssjskipp » Logged
Global Moderator
*

Karma: -8
Gender: Male
Posts: 1,168



View Profile
« Reply #1 on: September 04, 2005, 03:54:38 PM »
Damn this is way to far for me,
nice tut though
Logged
Tutorial Writer
*

Karma: 9
Gender: Male
Posts: 1,254


I'm mighty tighty whitey and I'm smugglin' plums.


View Profile WWW
« Reply #2 on: September 04, 2005, 04:09:02 PM »
Action script is a bitch to grasp at first, thanks for the tut =]
Logged
Administrator
*

Karma: 51
Gender: Male
Posts: 2,638



View Profile WWW
« Reply #3 on: September 04, 2005, 04:25:16 PM »
Looks good, if you could upload an example .fla that would be great. Also, grab a 40x40 if you want. If not I will just use a generic Flash Icon 40x40 thumbnail for the tutorials page.
Logged
Veteran
*

Karma: -18
Gender: Male
Posts: 311


i came, i saw, i left


View Profile
« Reply #4 on: September 06, 2005, 09:34:40 AM »
yea action script can be a biatch! nice tut!
Logged
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« Reply #5 on: September 06, 2005, 11:31:43 AM »
Thanks for the reviews, I'll be making more tutorials on Action Script ^_^
Logged
Regular
*

Karma: 1
Gender: Male
Posts: 88



View Profile
« Reply #6 on: September 06, 2005, 02:04:07 PM »
OMG <3 action script is what I need to learn.  Wonderful tut,  I am going home tonight after work to try it.
+rep

If I wanted to use w, a, s, d keys would it just be:

Key.isDown(Key.W)  ??
« Last Edit: September 06, 2005, 02:07:59 PM by bluehaze » Logged
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« Reply #7 on: September 07, 2005, 03:58:02 PM »
Nope, not Key.W, not really even close XD. What you want it a Key Code. Every key has it's own number, that is called the KeyCode! If you want to use a Key Code, you'd do Key.isDown([key code]), like that. Remember, not Key.isDown(Key.[keycode]) <that = BAD!
Here's a list of key codes, they can also be obtained through flash, look in the actipnscript help files. Here's my list:
Code:
Letter or number key
 Key code
 
A
 65
 
B
 66
 
C
 67
 
D
 68
 
E
 69
 
F
 70
 
G
 71
 
H
 72
 
I
 73
 
J
 74
 
K
 75
 
L
 76
 
M
 77
 
N
 78
 
O
 79
 
P
 80
 
Q
 81
 
R
 82
 
S
 83
 
T
 84
 
U
 85
 
V
 86
 
W
 87
 
X
 88
 
Y
 89
 
Z
 90
 
0
 48
 
1
 49
 
2
 50
 
3
 51
 
4
 52
 
5
 53
 
6
 54
 
7
 55
 
8
 56
 
9
 57

Numbpad 0
 96
 
Numbpad 1
 97
 
Numbpad 2
 98
 
Numbpad 3
 99
 
Numbpad 4
 100
 
Numbpad 5
 101
 
Numbpad 6
 102
 
Numbpad 7
 103
 
Numbpad 8
 104
 
Numbpad 9
 105
 
Multiply
 106
 
Add
 107
 
Enter
 13
 
Subtract
 109
 
Decimal
 110
 
Divide
 111
Logged
Regular
*

Karma: 1
Gender: Male
Posts: 88



View Profile
« Reply #8 on: September 08, 2005, 11:56:30 AM »
Thanks!  I got this tut and the wall tut both to work.  Even though I used your code I was playing with it a little bit just trying to get my feet wet with the coding.  Is there a way to open up the coding area to more than one line?  Its hard to code like that! O_o
Logged
Administrator
*

Karma: -489
Gender: Male
Posts: 1,207



View Profile
« Reply #9 on: September 11, 2005, 09:14:34 PM »
um drag the window open further.. and minimize the surrounding windows, it was really never a problem unless your like working in 800/600
Logged
Regular
*

Karma: 1
Gender: Male
Posts: 88



View Profile
« Reply #10 on: September 12, 2005, 11:51:05 AM »
ew yuck i am in 1152 x 864, thanks.
Logged
Monny
Guest
« Reply #11 on: October 30, 2005, 11:16:36 PM »
Question... Do you KNOW the definition of the word "syntax"?  You kinda used it wrongly, sir.

Pretty good tut, nonetheless.
Logged
Tutorial Writer
*

Karma: 9
Gender: Male
Posts: 1,254


I'm mighty tighty whitey and I'm smugglin' plums.


View Profile WWW
« Reply #12 on: November 02, 2005, 10:34:35 AM »
Syntax
A systematic, orderly arrangement.

He used it just fine
Logged
Tutorial Writer
*

Karma: 4
Gender: Male
Posts: 1,114



View Profile WWW
« Reply #13 on: November 09, 2005, 10:38:16 PM »
=p! I used it right, =o!
Logged
Graphic Moderator
*

Karma: 16
Gender: Male
Posts: 2,181

...


View Profile WWW
« Reply #14 on: January 31, 2006, 02:55:02 AM »
i took this tut a while ago and i think its great.  i was just playing around today with actionscript and i got a rectangle to move around the screen with this code

Code:
on(keyPress "<Up>") {
_y-=5
}
on(keyPress "<Down>") {
_y+=5
}
on(keyPress "<Left>") {
_x-=5
}
on(keyPress "<Right>") {
_x+=5
}

and in your tut you used this one to make him move

Code:
onClipEvent (load) {
_root.up=true
_root.down=true
_root.left=true
_root.right=true
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP) && _root.up == true) {
_y-=5
}
if (Key.isDown(Key.DOWN) && _root.down == true) {
_y+=5
}
if (Key.isDown(Key.LEFT) && _root.left == true) {
_x-=5
}
if (Key.isDown(Key.RIGHT) && _root.right == true) {
_x+=5
}
}

i was just wondering if their is some faults with mine since it seems so much simpiler and quicker than the way you did and could save some time. Huh
Logged
Pages: [1] 2  All   Go Up
  Print  
 
Jump to:      

BioRUST [ In: 2951 // Out: 2428 ] Flawsome [ In: 446 // Out: 1726 ] MickM [ In: 2403 // Out: 2118 ] SMF Topsites [ In: 0 // Out: 2021 ] Graphic Addicts Topsites [ In: 2435 // Out: 3389 ] SweDesignz [ In: 865 // Out: 1647 ] Dr. Photoshop [ In: 1301 // Out: 1634 ] cagedflame [ In: 992 // Out: 1381 ]
Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC
Phobos design by Bloc | XHTML | CSS


Google visited last this page Today at 05:15:09 PM