Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > General > General Webmaster Talk
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs
 
 

General Webmaster Talk General webmaster discussion forums - In this forum and its sub forums you can discuss general webmaster related issues or even issues that does not related to Webmastering.
Sub Forums: Running a forum :: Blogs and Blogging :: Word Press Forums :: Digital Photography

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-26-2004, 03:48 PM
Senior Member
0 posts this year. needs some grease!
New user, who has not interacted much yet.
 
Join Date: Aug 2004
Posts: 328
Thanks: 0
Thanked 0 Times in 0 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Default Tutorials>Advanced>Movement with hitTest

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:
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;
}
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.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Webmaster Resources
UK WW SEO Tools
Find UK Hosts
 
The Forum Rules
Forum Rules - MUST READ
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
affiliates ambitious asbestos cancer bid bidding directory bid directory bid for position business call center software clothes contest cricket writer dacx ameyo delisted designer discount drishti ecommerce email list email marketing finance forum free web hosting free website hosting global business directory google handbags huge email database jewelry link submissions niche of mesothelioma phones electronics php file value pr7 directory replica social bookmark startup sunglasses technology telecommunications wallets web website website for sale website promotion websites for sale wholesale yahoo backlink

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Webmasters seeing movement in Serps gkd_uk Google 2 09-19-2007 08:28 AM
enjoy with Language Movement Day offer rakib Directories 1 02-21-2007 09:31 AM
Advanced PHP Coder required temi Job Opportunities 7 04-28-2005 06:38 PM
Tutorial>Advanced>AS to code tween... crowebird General Webmaster Talk 0 10-06-2004 01:09 AM
Tutorials>Advanced>Save Data crowebird General Webmaster Talk 0 09-23-2004 01:40 AM


All times are GMT. The time now is 03:40 PM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0