forums

TNT Basic Forums > Programming Q&A
Making a sprite stop when it colidates with another sprite.
< Last Thread     Next Thread >
Author
Thread        Post A Reply

05-20-2008 15:11

Posted by:
goliatmo

Click Here to Email goliatmo   Find more posts by goliatmo

Im trying to make a sprite stop when it connects with another sprite. So far I have made this, but i does not work to good.
Is there a brilliant way to do this, wich work perfectly? If so it would bring happyness to my heart if some one bothered to tell me how!

---

window mode 300,300
load images 1

int x=70,y=70,a=2

while not (space)
sprite 1,x,y,0

if up then y=y-2
if down then y=y+2
if left then x=x-2
if right then x=x+2

sprite 2,200,200,1

if a=sprite col (1,2)
if y<200 then y=y-2
if y>200+60 then y=y+2
if x<200-60 then x=x-2
if x>200 then x=x+2
end if



draw frame
wend

---

PS. Sorry if this question has allready been asked and answeard, I tried to search and found nothing.

05-20-2008 16:48

Posted by:
goliatmo

Click Here to Email goliatmo   Find more posts by goliatmo

uhm.. I should probubly explain what my problem is..

If you, forexample, are standing at the top right corner and are pressing left, the moving sprite should be standing still, but instead it moves up until it reaches the top of my standing still-sprite and then continues to move left (If you are still holding the left button, ofcourse)

Thanks to whoever may help me..

05-20-2008 22:50

Posted by:
appleide

Click Here to Email appleide   Find more posts by appleide

>> if left then x=x-2

try if left and x>=200 then x=x-2

05-21-2008 15:31

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

First of all I see you hardcoded the location of the sprite (200,200) this is not too good, try using variables instead

If your sprite is rectangle, you may have an easier time with sprite col right. What often works best is to check if there is an obstacle *before* actually moving sprite, rather than moving it, checking if there's something and then moving it back. Therefore, you could try something like this (pseudo-code)

if left

' check if we'd collide with something on the left
if not sprite col rect 0, x-2, y to x-2+sprite_width, y+sprite_height
' no collision on the left, it's safe to move
x = x - 2
end if

end if


note that I do not quite remember the syntax of sprite col rect so maybe it should be better to check the docs

another possible approach is to remember in what direction the user is moving

int direction

if left then direction=1
if right then direction=2
if up then direction=3
if down then direction=4

if direction=1 then x=x-1
if direction=2 then x=x+2
...

if sprite col...

'collision, move back
if direction=1 then x=x+1
if direction=2 then x=x-2
...

end if

this second approach however works less well because it can have trouble with handling moving both horizontally and vertically at once

12-03-2009 14:43

Posted by:
soft

Find more posts by soft

colision

Hello!
I am French and I am going to write from my best English -_- here is my code:
int x
sprite 0,x,0,0
if left
x=x-6
end if
if right
x=x+6
end if
sprite 1,200,0,1
if sprite col(0,1)<>-1
if left
x=x+6
end if
if right
x=x-6
end if
end if

Here is my problem:
When the sprite 0 collides with the sprite 1 yew is pushed away but when I want to leave in the other direction(management) the sprite 0 goes into the sprite 1 is by in the direction opposite.
Can you give me a code sourceof collision functional?

12-04-2009 14:56

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Not sure what you mean here...

Could you post again, asking the question in French - I'll get one of our many French speakers to take a look at it for you...

Thanks

Danny (nod the mod)

12-05-2009 15:43

Posted by:
soft

Find more posts by soft

bonjour:

mon code:
int x
sprite 0,x,0,0
if left
x=x-6
end if
if right
x=x+6
end if
sprite 1,200,0,1
if sprite col(0,1)<>-1
if left
x=x+6
end if
if right
x=x-6
end if
end if

et... mon problème:
quand le sprite 0 entre en collision avec le sprite 1 et que l'utilisateur appuie sur droite le sprite 0 est arreté,mais quand l'utilisateur appuie sur gauche pour se dégager,le sprite 0 s'en va vers la droite au lieu de la gauche comme l'utilisateur l'a demandé.
pouvez-vous m'expliquer comment faire pour que le sprite 0 aille dans la bonne direction et puisse se dégager ou
me donner un code de collision qui fonctionne?

12-06-2009 15:51

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Ok, taken a look...

I'm still not sure what you are trying to do here. It looks like you are moving sprite 0, then trying to put it back if there is a collision. If this is the case, the usual way to do this is to store the original coordinates, move the sprite, then if a collision occurs you copy one or both of the original coordinates back. For Example, something like this:

oldX=X
oldY=Y

' - Move sprite here -

if spriteCol(0,1)=1 rem a collision with sprite 1
  X=oldX
  Y=oldY
end if


Remember that sprite collisions go by the actual positions on screen, so you need to 'draw frame' to update those positions.

I hope this helps - but if not just let us know...

Danny (nod the mod)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.