PDA

View Full Version : Collisions help


Jedimace
06-15-2007, 01:10 PM
ok, i want it so when something runs into something else, for it to do something. i have no idea what the code is for this, so could you help me?

Noct
06-15-2007, 05:02 PM
Welcome aboard, purple lightsaber and all...

What you need it a hitTest.
It is an evaluation that checks if two clips bounding boxes are "touching".

It is usually something that has to be constantly checked, so the code generally needs to be in an event that fires repeatedly, like a setInterval, or an onEnterFrame.

this.onEnterFrame=function(){
if(this.movClip1.hitTest(movClip2)){
//do something
}}


http://www.actionscript.org/resources/articles/61/1/Movement-and-simple-hittest/Page1.html
http://www.kirupa.com/developer/actionscript/hittest.htm
http://www.flashkit.com/tutorials/Interactivity/Using_th-Ram_Sakt-177/index.php
http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary534.html

Jedimace
06-16-2007, 05:55 AM
Thank You soooooo much!!!!!!!:)

Jedimace
06-16-2007, 07:27 AM
I ran into another problem tho. I have this code for the wall: onClipEvent (enterFrame) {
if (this.hitTest (_root.you) == true) {
if (_root.direction1 = "Left") {
_root.you._x += 5
}
if (_root.direction1 = "Right") {
_root.you._x -= 5
}
if (_root.direction1 = "Down") {
_root.you._y -= 5
}
if (_root.direction1 = "Up") {
_root.you._y += 5
}
}
}
and this code for the player (you) onClipEvent (enterFrame) {
if (Key.isDown(37)) {
_x=_x-_root.speed;
_root.direction1 = "Left"
}
if (Key.isDown(38)) {
_y=_y-_root.speed;
_root.direction1 = "Up"
}
if (Key.isDown(39)) {
_x=_x+_root.speed;
_root.direction1 = "Right"
}
if (Key.isDown(40)) {
_y=_y+_root.speed;
_root.direction1 = "Down"
}
}
onClipEvent (enterFrame) {
_root.yo = _root.direction1
}
on the frame i have that speed = 5. Whenever i run into the wall, nothing happens! I should stop but i go right through it. I checked over it like 10 times i am out of ideas.:confused:I attached the flash 8 file and swf. Also the direction1 variable becomes up when i run into the wall and it makes no sense either.

Jedimace
06-21-2007, 10:52 AM
OK, i figured out how to do walls easy, but yet again it doesn't work. i put in:
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
this._x += 5
}
if (Key.isDown(Key.LEFT)) {
this._x -= 5
}
if (Key.isDown(Key.UP)) {
this._y -= 5
}
if (Key.isDown(Key.DOWN)) {
if (_level10.player.hitTest (_level10.wall) == true) {
this._y += 0
} else {
this._y += 5
}
}
}
Can you tell me if anything is wrong, it should stop him when he goes down into the wall. That's all the code i put in.

felipe
06-28-2007, 09:26 PM
First, what do you mean by "do something".. all you really need to do is, with the hitTest, check for something different, and then plug in the new code for it to "do something" in this new if statement