methods of hitTest
- downset13
-
downset13
- Member since: Jun. 17, 2003
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
Bare with me... I have quite a bit of basic programming experience, sadly none of it involves Actionscript. I'm going about honing up on my skills by making an over head adventure type game, not isometric, just flat 2D (chrono trigger -esk). I'm able to make my character move and stop when he hits the object "wall", as shown by the following:
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) &&
this.char_right.hitTest(this._parent.wall) == false){
_x += 5;
}
else if(Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)&&
this.char_left.hitTest(this._parent.wall) == false){
_x -= 5;
}
else if(Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)&&
this.char_bottom.hitTest(this._parent.wall) == false){
_y += 5;
}
else if(Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)&&
this.char_top.hitTest(this._parent.wall) == false){
_y -= 5;
}
}
Basically inside of my character movie clip I have 4 other clips that act as each of his cardinal boundaries, such as left, right, top, bottom. That way if he runs into an object on the right, he can still move the other 3 directions. While this works for one object, I tried to copy and past my "wall" object to use as another boundary, lo and behold it doesn't work. The character only stops when he confronts the original wall. Is there a way to get him to recognize the other instances of "wall" without adding a crapton of lines of code or am I just going about this all the wrong way?
Thanks,
Chris
- Magical-Zorse
-
Magical-Zorse
- Member since: May. 10, 2008
- Offline.
-
- Forum Stats
- Member
- Level 39
- Melancholy
I guess I did this for shits and giggles
while(_root.walls.hitTest(_x, _y+radius, true)){
_y++;
}
while(_root.walls.hitTest(_x, _y-radius, true)){
_y--;
}
while(_root.walls.hitTest(_x-radius, _y, true)){
_x--;
}
while(_root.walls.hitTest(_x+radius, _y, true)){
_x++;
}
make sure you've got a mc with the instance name of walls
- downset13
-
downset13
- Member since: Jun. 17, 2003
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
Not exactly sure how that'd help with the current problem, but thanks anyways. What I really want to know is before I go head on and try to work this thing out and make it uber complicated, I wanted to know if it was possible to have multiple instances of a movie clip and have the code of the hitTest recognize and use them all. If not I know I need to just rework the whole thing.
- downset13
-
downset13
- Member since: Jun. 17, 2003
- Offline.
-
- Forum Stats
- Member
- Level 04
- Blank Slate
okay, so I wrote the code a different way that seemed totally flawless... but alas...
Inside my hero I have this code:
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) &&
_root.wallRight == false){
_x += 5;
}
else if(Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)&&
_root.wallLeft == false){
_x -= 5;
}
else if(Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)&&
_root.wallDown == false){
_y += 5;
}
else if(Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)&&
_root.wallUp == false){
_y -= 5;
}
}
Now in each wall I have this code:
onClipEvent (enterFrame) {
if (hitTest(this._parent.character.char_bottom)) {
_root.wallDown = true;
} else if (hitTest(this._parent.character.char_top)) {
_root.wallUp = true;
} else if (hitTest(this._parent.character.char_left)) {
_root.wallLeft = true;
} else if (hitTest(this._parent.character.char_right)) {
_root.wallRight = true;
} else {
_root.wallRight = false;
_root.wallLeft = false;
_root.wallUp = false;
_root.wallDown = false;
}
}
Now the problem is... i can make another movie clip and paste the exact same code and my hero walks right through it! I don't see how that could be if the code is the exact same. Can someone please point out my supidity here?
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
try being more specific. rather than parent. or this. be specific and write the full paths starting at _root.
it might help

