PDA

View Full Version : Yet Another Hit-test Problem...



happy-hour
July 21st, 2004, 03:11 PM
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.

happy-hour
July 21st, 2004, 03:15 PM
You'll probably be wanting the file...

happy-hour
July 21st, 2004, 03:16 PM
ok. for some reason it wont upload...

elPooter
July 21st, 2004, 03:16 PM
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;
}
}

happy-hour
July 21st, 2004, 03:18 PM
got it! heres the file:

elPooter
July 21st, 2004, 03:19 PM
Did the code work?

happy-hour
July 21st, 2004, 03:22 PM
No, but now i got the file uploaded (see above post) you can try it yourself.

elPooter
July 21st, 2004, 03:23 PM
Cant try it. it isnt MX, sorry.

P.S. How do you attach fla files?

happy-hour
July 21st, 2004, 03:24 PM
it should be.

happy-hour
July 21st, 2004, 03:26 PM
a straight upload. should i zip it first?

elPooter
July 21st, 2004, 03:27 PM
Ok.

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

happy-hour
July 21st, 2004, 03:31 PM
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.

elPooter
July 21st, 2004, 03:37 PM
heres help:

elPooter
July 21st, 2004, 03:38 PM
Yay! It uploaded!

happy-hour
July 21st, 2004, 03:45 PM
thats all very well but that code wont work on mine.

elPooter
July 21st, 2004, 03:46 PM
Oh, thats the only hitTest code I know. Sorry :(

happy-hour
July 22nd, 2004, 05:56 AM
ok. Thanks for your help.

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

dal platinum
July 22nd, 2004, 07:46 AM
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.

happy-hour
July 22nd, 2004, 08:20 AM
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:

happy-hour
July 22nd, 2004, 08:46 AM
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.