Tutorials>Advanced>Movement with hitTest
This tutorial will teach you how make a object move on a Key Press and stop when it comes in contact with another object.
Creating Objects
Create a box (please make this box with a fill), convert it to a MovieClip. Give the new MC the Instance Name of moveable. Now create a large box(please make this box with a fill) and convert it to a MovieClip. This large box does not need an Instance Name
Moving the box with ActionScript
We now need to give coding to the "moveable" box so it moves on keypress. Open up the actions for "moveable" box.
Add the following code:
When an arrow key is pressed the code reads which direction the mox should move. For example when the right arrow key is pressed the box will move right (obvisouly). It does this by reading at which speed the box should move. In this case it is 4. It then takes Xmove (which was defined as the "moveable" box _x cordinate) and adds move_speed to it cause the box to move to the right. Now test your movie. The box should move when any of the arrow keys are pressed.
Stoping the "moveable" box when in contact with large box
We now need to add coding to the Large box that causes the "moveable" box to stop when contact is made. The large box acts as a wall.
Add the follwing code to the large box:
This just checks with the "moveable" box and if it is hitting the large box, the "moveable" box will stop. You then can move away from the large box but will never be able to pass through it.
Test your movie and see your movement and hitTest in action.
Using different keys to move the box
You don't just have to use the arrow keys to move the box. You can use any key actully. In order to do this, you must use the keys "keycodes".
The keycodes are as follows:
Here is an example of how to use these keycodes. I will be using "a" key or 65.
In this case when "a" was presses the object would move up at a speed of 4.
End
Please note that the move_speed is dependant on the frames-per-second(fps). A move_speed of 4 with a fps of 20 will be faster than a move_speeed of 4 with a fps of 12.
This tutorial will teach you how make a object move on a Key Press and stop when it comes in contact with another object.
Creating Objects
Create a box (please make this box with a fill), convert it to a MovieClip. Give the new MC the Instance Name of moveable. Now create a large box(please make this box with a fill) and convert it to a MovieClip. This large box does not need an Instance Name
Moving the box with ActionScript
We now need to give coding to the "moveable" box so it moves on keypress. Open up the actions for "moveable" box.
Add the following code:
Code:
onClipEvent (enterFrame) {
this._x += Xmove;
this._y += Ymove;
Ymove = 0;
Xmove = 0;
if (Key.isDown(Key.UP) and Key.isDown(Key.LEFT)) {
move_speed = 3;
Ymove -= move_speed;
Xmove -= move_speed;
} else if (Key.isDown(Key.UP) and Key.isDown(Key.RIGHT)) {
move_speed = 3;
Ymove -= move_speed;
Xmove += move_speed;
} else if (Key.isDown(Key.DOWN) and Key.isDown(Key.LEFT)) {
move_speed = 3;
Ymove += move_speed;
Xmove -= move_speed;
} else if (Key.isDown(Key.DOWN) and Key.isDown(Key.RIGHT)) {
move_speed = 3;
Ymove += move_speed;
Xmove += move_speed;
} else if (Key.isDown(Key.UP)) {
move_speed = 4;
Ymove -= move_speed;
} else if (Key.isDown(Key.DOWN)) {
move_speed = 4;
Ymove += move_speed;
} else if (Key.isDown(Key.LEFT)) {
move_speed = 4;
Xmove -= move_speed;
} else if (Key.isDown(Key.RIGHT)) {
move_speed = 4;
Xmove += move_speed;
}
}
When an arrow key is pressed the code reads which direction the mox should move. For example when the right arrow key is pressed the box will move right (obvisouly). It does this by reading at which speed the box should move. In this case it is 4. It then takes Xmove (which was defined as the "moveable" box _x cordinate) and adds move_speed to it cause the box to move to the right. Now test your movie. The box should move when any of the arrow keys are pressed.
Stoping the "moveable" box when in contact with large box
We now need to add coding to the Large box that causes the "moveable" box to stop when contact is made. The large box acts as a wall.
Add the follwing code to the large box:
Code:
onClipEvent (enterFrame) {
with (_root.moveable) {
if (this.hitTest(_root.moveable._x+Xmove, _root.moveable._y+Ymove, true)) {
Ymove = 0;
Xmove = 0;
}
}
}
This just checks with the "moveable" box and if it is hitting the large box, the "moveable" box will stop. You then can move away from the large box but will never be able to pass through it.
Test your movie and see your movement and hitTest in action.
Using different keys to move the box
You don't just have to use the arrow keys to move the box. You can use any key actully. In order to do this, you must use the keys "keycodes".
The keycodes are as follows:
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
Numbpad1
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
108
Subtract
109
Decimal
110
Divide
111
F1
112
F2
113
F3
114
F4
115
F5
116
F6
117
F7
118
F8
119
F9
120
F10
121
F11
122
F12
123
F13
124
F14
125
F15
126
Backspace
8
Tab
9
Clear
12
Enter
13
Shift
16
Control
17
Alt
18
Caps Lock
20
Esc
27
Spacebar
32
Page Up
33
Page Down
34
End
35
Home
36
Left Arrow
37
Up Arrow
38
Right Arrow
39
Down Arrow
40
Insert
45
Delete
46
Help
47
Num Lock
144
; :
186
= +
187
- _
189
/ ?
191
` ~
192
[ {
219
\ |
220
] }
221
'' '
222
Here is an example of how to use these keycodes. I will be using "a" key or 65.
Code:
if (Key.isDown(65) {
move_speed = 4;
Ymove -= move_speed;
}
End
Please note that the move_speed is dependant on the frames-per-second(fps). A move_speed of 4 with a fps of 20 will be faster than a move_speeed of 4 with a fps of 12.