Yet Another Hit-test Problem

Hey guys,

Ok. I have a simple movement script within the “ball” movie clip. The coding is all over the place so i dont blame you if it looks confusing.

I have made a boarder around the screen which is named “wall”. Basicly i don’t want the ball to pass this wall and when it touches the wall i want it to stop. Nothing fancy like bouncing off it, just stop it moving.

Thanks in advance,

    Rob.

You’ll probably be wanting the file…

ok. for some reason it wont upload…

Try this:

 
onClipEvent (enterFrame) {
	if (_root.walls.hitTest(getBounds(_root).xMax, _y, true)) {
		this._x -= 5;
	}
	if (_root.walls.hitTest(getBounds(_root).xMin, _y, true)) {
		this._x += 5;
	}
	if (_root.walls.hitTest(_x, getBounds(_root).yMin, true)) {
		this._y += 5;
	}
	if (_root.walls.hitTest(_x, getBounds(_root).yMax, true)) {
		this._y -= 5;
	}
}
 

got it! heres the file:

Did the code work?

No, but now i got the file uploaded (see above post) you can try it yourself.

Cant try it. it isnt MX, sorry.

P.S. How do you attach fla files?

it should be.

a straight upload. should i zip it first?

Ok.

P.S. How do you upload a fla? sorry, im new at the attaching.

when you go to the post reply screen scroll down a little and you can click the big “manage attachments” button.

From there you can browse for the file, then “upload” using the button. Just close the window (using the button) and submit reply using the bottom button.

BTW here’s a zip file incase it makes a difference.

heres help:

Yay! It uploaded!

thats all very well but that code wont work on mine.

Oh, thats the only hitTest code I know. Sorry :frowning:

ok. Thanks for your help.

Im thinking of redoing the code then i’ll see if it works.:-/

instead of using a movieclip, and hitTesting, have you tried just using the boundaries of the movie?

like:

this bit can go on frame 1

 
_root.mtop = 0
_root.mleft = 0
_root.mbottom = 200
_root.mright = 400

this bit can go on an mc (on root)


onClipEvent(EnterFrame) {
 if (this._y<_root.mtop) {
  this._y = _root.mtop
 } else if (this._y>_root.mbottom) {
  _this._y = _root.mbottom
 }
 if (this._x<_root.mleft) {
 this._x = _root.mleft
 } else if (this._x>_root.mright) {
  this._x = _root.mright
 }
}

As ever, I can’t test if this works, because I’m not at home, but it looks like it should do.

It sort of works exept the movie is 200 x 200 so the ball can only move around the bottom corner. I tried chaning the _root.mright values but im not sure how they work.

Its a nice idea and it definitly works but im hoping to create a small town in which a character can walk around. Thats why i have the different images on each key press (so the character apears to be walking in the direction moving). Im still drawing so i just used a basic ball with arrows.

I was wondering if i could insert the hittest into each keypress code.
The current code (movement to the right):

 if (Key.isDown(39)) {
  this._x += 5;
  gotoAndPlay(4);
 }

i want to combine 2 “if” conditions. In writting it would sound like. IF Key is pressed move in direction (using the x or y adding or subracting) and go to frame “?” (where the image of the ball is. 1 to 5). Then add the hit test. so IF “ball” touches “wall” stop by adding or subtracting 5 from x or y value.

In the case of moving right this value would be this._x -=5;

I think…

Im getting confused just trying to explain it. :puzzle:

I have decided to scrap my coding because it was usless and use the code in the ball and wall .fla which seemed to work quite well. Thanks for your help guys. Have a look at this .fla

Try going throught the gaps at the top and bottom. Confused? So am I.