Forums

ForumsProgramming Forum

How to make a perfect hitTest in as2?

Posted May 30, '11 at 1:57pm

Oseb

Oseb

29 posts

I think I know what you're trying to do, Oseb, but if you want to detect collision between the object and the coordinate (200,200), then you need to remove the _root._x=

Just have this as you're code:
onClipEvent(enterFrame){
if(this.hitTest(200,200,true)){
this._y-=5
}


I think that this code was ok but i need it without x coordinate. But when i deleted the X coordinate (the first 200) then it didnt work.

 

Posted May 30, '11 at 2:07pm

arobegamr

arobegamr

130 posts

No, you would again be checking collision between symbol/symbol, which would not be a perfect collision, but a collision between bounding boxes. If you want to detect the corners of your circle, these are your points, in order clockwise starting from the top-left
(_x means circle._x, _y means circle._y)

(_x+Math.cos(-2.356)*radius,_y+Math.sin(-2.356)*radius)
(_x+Math.cos(-0.785)*radius,_y+Math.sin(-0.785)*radius)
(_x+Math.cos(0.785)*radius,_y+Math.sin(0.785)*radius)
(_x+Math.cos(2.356)*radius,_y+Math.sin(2.356)*radius)

 

Posted May 30, '11 at 2:12pm

Oseb

Oseb

29 posts

HitTest between a circle and a rectangle. I think this code is ok for it:

onClipEvent(enterFrame){
if(this.hitTest(200,200,true)){
this._y-=5
}

But the only problem is that this is one point i want that if my circle reach Y = 200 then he stops thats all i want to do. But when i delet the first 200 of the code then its wont work, so how scould this code sounds?

 

Posted May 30, '11 at 2:14pm

arobegamr

arobegamr

130 posts

So, you want your character to not be able to cross the line y=200?

 

Posted May 30, '11 at 2:21pm

Oseb

Oseb

29 posts

yes

 

Posted May 30, '11 at 2:25pm

arobegamr

arobegamr

130 posts

Alright, so your code will differ based on where the character's registration point is.

Center: if(circle._y+circle._height/2>200){
Top: if(circle._y+circle._height>200){
Bottom: if(circle._y>200){

 

Posted May 30, '11 at 2:39pm

Oseb

Oseb

29 posts

Thanks!

And after these codes (which i add to the symbols in my circle symbol) i add this code:

onClipEvent(enterFrame){
if(this.hitTest(200,200,true)){
this._y-=5
}

And if yes i still have one last problem, that when he reach 200 he will bounce and bounce back (if i hold the down arrow button). And what i want is that when my character reach Y=200 then he stops and doesn't bounce back. I swear its my last question.

 

Posted May 31, '11 at 11:04pm

arobegamr

arobegamr

130 posts

Actually, you don't need the hitTest function at all in this case. I assume that you are placing the code to move the character after the code to detect if the player is below the point.

Try this:
if(Key.isDown(Key.DOWN)){
if(this._y+5>200){
this._y=200
}else{
this._y+=5
}
}

 

Posted Jun 1, '11 at 11:46am

Oseb

Oseb

29 posts

AWESOME!
Worked well. Thanks
My only last problem is with add gravity
Thanks again

 

Posted Jun 1, '11 at 11:44pm

arobegamr

arobegamr

130 posts

If you want to add gravity:

onClipEvent(load){
this.jumping=1
this.jump=0
}
onClipEvent(enterFrame){
if(this.jumping){
this.jump-=.5
if(this._y-this.jump>200){
this._y=200
this.jumping=0
this.jump=0
}else{
this._y-=this.jump
}
}else{
if(Key.isDown(Key.UP)){
this.jump=5
this.jumping=1
}
}
}

 
Reply to How to make a perfect hitTest in as2?

You must be logged in to post a reply!