• 12
• 12
• 9
• 10
• 13

# Wall Trouble

This topic is 4863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In Flashmx 2004 i was using some code to move a cirlce around my screen. I also drew a line to make a wall. Now from the tutorial I was working off the code that made the cirlce move around worked perfectly but when it came to the cirlce not being able to move through the wall it still kept passing the ling. Here is the code I used on the ball onClipEvent (enterFrame) { if (Key.isDown(37)) { _x=_x-2; } if (Key.isDown(38)) { _y=_y-2; } if (Key.isDown(39)) { _x=_x+2; } if (Key.isDown(40)) { _y=_y+2; } if (hitTest(_level0.wall)==true) { _x=_x-2; } } any suggestions of how to fix this?

##### Share on other sites
Well - I don't use Flash, so take this with a pinch of salt - but if hitTest is working correctly, the only thing it would ever correct would be Key.isDown(39) - in all other cases, it just moves you -2 units on the x-axis if there's a hit. You'd probably be able to see this as a jump as it suddenly doubles in speed for one frame.

If your hitTest is working, then it needs to void the previous movement, so it needs to know which movement it last took - implement some flag. A better way (IMO) is make hitTest part of the movement process:

if(Key.isDown(37)){
hitTest(x, -2, _level0.wall);
}

hitTest now knows you want to move -2 units on the x-axis - so checks against _level0.wall. If this would mean hitting a wall, then hitTest doesn't implement the move, otherwise it does.

HTH,
Jim.

##### Share on other sites
onClipEvent (enterFrame) {if (Key.isDown(37)) { _x=_x-2; if (hitTest(_level0.wall)==true) {  _x=_x+2; }}if (Key.isDown(38)) {_y=_y-2;}if (Key.isDown(39)) {_x=_x+2;if (hitTest(_level0.wall)==true) {  _x=_x-2; }}if (Key.isDown(40)) {_y=_y+2;}}

This will make the ball go back, if the ball hits the wall in his new position(only if u go left or right).

xIshtarx