hitTest Tutorial (writen)

by Cybersief

in Completed Works

< 'Dance!' by Cybersief

hitTest Tutorial (writen)

NA //(C) 2007 Sief Naber
//----------------------------------hitTest() Tutorial Automatic Test----------------------------------
//First, put an object, such as a circle, onto the stage, and make it a movie clip. Give it the instance name of "Circle"
//Next, put a wall to the right (It doesn't have to be there, its for the sake of the tutorial) and make it a movie clip. Give it the instance name of "wall"
//were going to do a test, instead of an interactivity test.
//on the circle add this script to it:
onClipEvent(load) {
var speed = 10;
}
onClipEvent(enterFrame) {
this._x-=speed;
}
//what the 'var' code does is that any time it reads 'speed' it refers to it as '10'.
//what this code does is, the onClipEvent(enterframe) script makes it so that the circle repeats whats in the {}'s every frame
//this._x-=10 what that does is, 'this' refers to the object and ._x refers to the x axcess of the object and -=10 minuses the x axcess by 10 frames per second.
//now press 'CTRL+ENTER' to test your movie. And as you see the object is moving, but the wall isn't stopping it.
//now on the circle again, inbetween the 'onClipEvent(enterframe) {' and '}' put in this code:
if(this.hitTest(_root.wall)) {
this._x+=speed;
}
//here is the hitTest function, 'if' is basicly self explanatory, 'this' again refers to the object the script is on.
//hitTest is basicly, it tests whenever it hits the object refered to inside the '()'. When you click on an object with the 'selection tool'
//the blue square thats around the object, thats what it tests for, for both those squares to hit each other. Now inside it says '_root.wall'
//'_root' refers to the root time line, and 'wall' is the instance name for the wall on the stage. Now inside the script is 'this._x+=speed;'
//the _x+= has to be the oposite of where the object is hitting. So the circle going at '-=' so the oposite is '+=' and 'speed' is the varable
//created before. Now if you test the movie, you see now that the circle should stop once it hits the wall.
//there ya go, now theres many ways to use this code, so get creative.

//----------------------------------hitTest() Tutorial Manual Test----------------------------------
//Now for this test were gonna put an object and make it be controled with the arrow keys.
//Now make an object like a circle and a wall in the stage such as the ones from the previous tutorial.
//Now for the circle add this script to it:
onClipEvent(load) {
var speed = 10;
}
onClipEvent(enterFrame) {
if(Key.isDown(Key.LEFT)) {
this._x-=speed;
}
if(Key.isDown(Key.RIGHT)) {
this._x+=speed;
}
if(Key.isDown(Key.UP)) {
this._y-=speed;
}
if(Key.isDown(Key.DOWN)) {
this._y+=speed;
}
}
//What this script does; The onClipEvent(load) and such I explained before. But the 'if(Key.isDown' this says basicly that
//if a key is down '(Key.LEFT)' that key. Then 'this', pointing to the object, and _x-=speed; basicly it minuses the _x of speed.
//Now if you test your movie (CTRL + ENTER) then you can see that you can move your object araound the stage! Well now if you use
//code for the wall as mentioned before, you can see that when you press against the wall going to the right you cant go any further.
//now if you go up to the wall in any other direction, nothing will happen.

//Any questions? Email them to: Siefnaber93@yahoo.com or you can reach me on my MSN: Siefnaber@hotmail.com or my AIM: Cybersief
//NOTE: DO NOT EMAIL ME AT MY MSN! I do not check that one, only yahoo.
> 'Motion Tween Tutorial' by Cybersief

Description

Jun 27th 2007
Tags:
adobe cs flash general help hit hittest macromedia test tutorial writen
Views:
17
Comments:
1
Score:
1
Favorites:
1
I have this in an Script document, thats whats with all the //'s
Hope it helps! (btw, need help, you can comment too ;D)

Comments

WCP Says:

I needed some help with actionscript :3