Im making a maze that ball travels through. I am succesful on one wall. But when I come down to the other side it brings it up. How do make the side a hitTest? or can give me advice if theres better solutions. Thanks....
I have attached it to the ball named circle. I have include the file
//the hitTest
if (hitTest(_level0.wall)==true) {
_y = _y-10;
this below is the full code
onClipEvent (enterFrame) {
if (Key.isDown(37)) {
_x=_x-10;
}
if (Key.isDown(38)) {
_y=_y-10;
}
if (Key.isDown(39)) {
_x=_x+10;
}
if (Key.isDown(40)) {
_y=_y+10;
}
//the hit test
if (hitTest(_level0.wall)==true) {
_y = _y-10;
I would do a coordinate based hit test which checks the direction you are trying to travel in.
Your current makes an adjustment to the position, and then does an 'undo' on that adjustment if there is a hit.
I would check to see if there is a hit on the target coordinates before making the actual adjustment, like so:
code:
onClipEvent (enterFrame) {
var xd = 0; // direction of movement
var yd = 0;
if (Key.isDown(37)) {
xd = -10;
}
if (Key.isDown(38)) {
yd = -10;
}
if (Key.isDown(39)) {
xd = 10;
}
if (Key.isDown(40)) {
yd = 10;
}
// check if there is a collision with the target coords
if (!_level0.wall.hitTest(_x+xd,_y+yd))
{
// if not, make the adjustment
_x += xd;
_y += yd;
}
}
Also, there's another way to do it, the method I used in these mazes:
In these mazes, I don't use hitTest at all, instead I keep track of where the valid paths exiting each cell are and only allow movement along the valid paths, tweening from the center of one cell to the next.
So rather than using hitTest, I am checking a data structure to see if a bit is set that corresponds to the desired direction of movement.