forums

TNT Basic Forums > Programming Q&A
Sprite Collision Problems
< Last Thread     Next Thread >
Author
Thread        Post A Reply

10-25-2005 21:22

Posted by:
AGhost

Find more posts by AGhost

I am programming a 2D game and was trying to work with sprite collisions. The only problem is, as soon as the game launches it ends (says I have collided).

Here is collision code:

If Sprite Col (129,130) then
set pen colour red
draw text 150,50,"YOU WIN!"
winner = 1


So what might be wrong?

AGhost

10-25-2005 22:47

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

your if applies only to the set pen color command. I mean, if they collide, the pen gets red, if they don't collide it doesn't get red, but anyway YOU WIN will be written.

you probably meant something like this:

If Sprite Col (129,130)

set pen colour red
draw text 150,50,"YOU WIN!"
winner = 1

end if

this means the 3 lines are part of the "if"

10-26-2005 00:29

Posted by:
AGhost

Find more posts by AGhost

Here is the code. I tried the fix but it still says it doesn't work.

Can you spot the problem?



repeat

If Sprite Col (129,130)
set pen colour red
draw text 150,50,"YOU WIN!"
winner = 1
end if

set pen colour yellow
draw text 960,50, As String (X)
draw text 980,50, "Remaining"

player1Speed=speed
if pressed(13) ' w = up
if player1Dir <> 3 then player1Dir=1
else if pressed (1) ' s = down
if player1Dir <> 1 then player1Dir=3
else if pressed (0) ' a = left
if player1Dir <> 4 then player1Dir=2
else if pressed (2) ' d = right
if player1Dir <> 2 then player1Dir=4
else if pressed (56) ' left shift = turbo
player1Speed=speed*2
end if

if player1Dir=1
newX=player1X
newY=player1Y-player1Speed
spr=1
else if player1Dir=2
newX=player1X-player1Speed
newY=player1Y
spr = 2
else if player1Dir=3
newX=player1X
newY=player1Y+player1Speed
spr=3
else if player1Dir=4
newX=player1X+player1Speed
newY=player1Y
spr=4
end if

load images 129
if spr=1 then sprite 129,player1x,player1y,002
if spr=2 then sprite 129,player1x,player1y,003
if spr=3 then sprite 129,player1x,player1y,001
if spr=4 then sprite 129,player1x,player1y,000

player1x=newX
player1y=newy

speed=speed


draw frame

until winner>0

10-26-2005 00:44

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

ohhh man yiu got a lot of problems here ;) first thing to do is to go through the code thinking "what will the computer do?" like MANY newbies you made a few mistakes with the repeat thing. Don't forget everything you put inside repeats over and over!

my comments are throughout the code

good luck!

-------------------
repeat

--> here i forgot to mention something last time. sprite col doesn't work like that. You need to call If Sprite Col (129,130)>-1 (it gives the if of the sprite it has collided with, If Sprite Col (129,130)=130 should work too)
If Sprite Col (129,130)
set pen colour red
draw text 150,50,"YOU WIN!"
winner = 1
end if

set pen colour yellow
draw text 960,50, As String (X)
draw text 980,50, "Remaining"

--> This may not be the clearest way to do it - but it works. (I don,t reckon it's necessary to check keys, set a variable, then check the variable. Yo ucould do it all once)

player1Speed=speed
if pressed(13) ' w = up
if player1Dir <> 3 then player1Dir=1
else if pressed (1) ' s = down
if player1Dir <> 1 then player1Dir=3
else if pressed (0) ' a = left
if player1Dir <> 4 then player1Dir=2
else if pressed (2) ' d = right
if player1Dir <> 2 then player1Dir=4
else if pressed (56) ' left shift = turbo
player1Speed=speed*2
end if

--> Here you move the sprite relative to its original center. Just think about the maths. The center never moves, so your sprite will never leave the center farther than player1Speed. You need to move the sprite relative to itself, eg newY= newY-player1Speed

if player1Dir=1
newX=player1X
newY=player1Y-player1Speed
spr=1
else if player1Dir=2
newX=player1X-player1Speed
newY=player1Y
spr = 2
else if player1Dir=3
newX=player1X
newY=player1Y+player1Speed
spr=3
else if player1Dir=4
newX=player1X+player1Speed
newY=player1Y
spr=4
end if

--> BIG mistake here! don't forget we're in the main loop. If you load here, the computer will reload images every frame. (Did you ever play a game doing this?) If loading takes 30 seconds, you'll have to wait 3o seconds between each frame! Load everything once in the beginning and it will work better ;)

load images 129
if spr=1 then sprite 129,player1x,player1y,002
if spr=2 then sprite 129,player1x,player1y,003
if spr=3 then sprite 129,player1x,player1y,001
if spr=4 then sprite 129,player1x,player1y,000

player1x=newX
player1y=newy

--> i don't see what's this line use. It does nothing.You set speed to itself. It does NOTHING!

speed=speed


draw frame

until winner>0

10-26-2005 00:45

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

when i mentionned the sprite is relative to the center, i didn't see your 2 variables were made equal in the end of the loop. You don't need this! Une only one it will work as good

10-26-2005 10:46

Posted by:
AGhost

Find more posts by AGhost

Alright Here is my fixed code:


repeat


set pen colour yellow
draw text 960,50, As String (X)
draw text 980,50, "Remaining"

set pen colour yellow
draw text 965,100, As String (Y)
draw text 980,100, "Lives"

if spr=1 then sprite 129,player1x,player1y,002
if spr=2 then sprite 129,player1x,player1y,003
if spr=3 then sprite 129,player1x,player1y,001
if spr=4 then sprite 129,player1x,player1y,000

if pressed(13) ' w = up
if player1Dir <> 3 then player1Dir=1
else if pressed (1) ' s = down
if player1Dir <> 1 then player1Dir=3
else if pressed (0) ' a = left
if player1Dir <> 4 then player1Dir=2
else if pressed (2) ' d = right
if player1Dir <> 2 then player1Dir=4
end if

if player1Dir=1
player1X=player1X
player1Y=player1Y-player1Speed
spr=1
else if player1Dir=2
player1X=player1X-player1Speed
player1Y=player1Y
spr = 2
else if player1Dir=3
player1X=player1X
player1Y=player1Y+player1Speed
spr=3
else if player1Dir=4
player1X=player1X+player1Speed
player1Y=player1Y
spr=4
end if


If Sprite Col (129,130) = 130
player1x=150
player1y=400
y = y - 1
set pen colour red
draw text 150,50,"Avoid the Nanites!"
draw frame
wait mouse click
end if

draw frame

until winner>0



Is there any redraw command that I can do in order to clear the text and then redraw it?

Thanks,
AGhost

10-26-2005 22:43

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

> Is there any redraw command that I can do in order to clear the text and then redraw it?

i dunno if there is but the usual way is to set the pen to black (or the color you want the background to be) and draw a rectangle over the whole screen! ;)

11-02-2005 10:21

Posted by:
AGhost

Find more posts by AGhost

repeat


set pen colour yellow
draw text 960,50, As String (X)
draw text 980,50, "Remaining"

set pen colour yellow
draw text 965,100, As String (Y)
draw text 980,100, "Lives"

if spr=1 then sprite 129,player1x,player1y,002
if spr=2 then sprite 129,player1x,player1y,003
if spr=3 then sprite 129,player1x,player1y,001
if spr=4 then sprite 129,player1x,player1y,000

if pressed(13) ' w = up
if player1Dir <> 3 then player1Dir=1
else if pressed (1) ' s = down
if player1Dir <> 1 then player1Dir=3
else if pressed (0) ' a = left
if player1Dir <> 4 then player1Dir=2
else if pressed (2) ' d = right
if player1Dir <> 2 then player1Dir=4
end if

if player1Dir=1
player1X=player1X
player1Y=player1Y-player1Speed
spr=1
else if player1Dir=2
player1X=player1X-player1Speed
player1Y=player1Y
spr = 2
else if player1Dir=3
player1X=player1X
player1Y=player1Y+player1Speed
spr=3
else if player1Dir=4
player1X=player1X+player1Speed
player1Y=player1Y
spr=4
end if


If Sprite Col (129,130) = 130
player1x=150
player1y=400
y = y - 1
set pen colour red
draw text 150,50,"Avoid the Nanites!"
draw frame
wait mouse click
end if

If y = 0 then loser = 1
If x = 0 then winner = 1

draw frame

until winner>0 or loser>0


What isn't working? It just ends the loop!

AGhost

11-02-2005 12:17

Posted by:
sabshire

Location:
nitro, wv

Find more posts by sabshire

re: problem...

AGhost, here's what you have at the end of your loop...


If Sprite Col (129,130) = 130
player1x=150
player1y=400
y = y - 1
set pen colour red
draw text 150,50,"Avoid the Nanites!"
draw frame
wait mouse click
end if

If y = 0 then loser = 1
If x = 0 then winner = 1

draw frame

until winner>0 or loser>0


The problem is that you never defined x or y, so they default to 0. So, provided the collision doesn't occur in the first iteration of the loop, y will equal 0, and x will equal 0, so the two if tests at the end will set both loser and winner equal to 1, and thus your loop will end.

11-03-2005 22:10

Posted by:
AGhost

Find more posts by AGhost

Sorry, Here is the whole code (not just Repeat):



int player1Dir=1
float player1x=150,player1y=400
float player1Speed=1.0
int winner = 0, loser = 1
int x = 10
int y = 5
int spr = 1


fade down
hide mouse
graphics mode 1040,768
fade up
set pen colour white
frame rect 0,0 to 640,480

load images 128
load images 130
load images 129

draw picture 128
sprite 130,500,300,002


repeat


set pen colour yellow
draw text 960,50, As String (X)
draw text 980,50, "Remaining"

set pen colour yellow
draw text 965,100, As String (Y)
draw text 980,100, "Lives"

if spr=1 then sprite 129,player1x,player1y,002
if spr=2 then sprite 129,player1x,player1y,003
if spr=3 then sprite 129,player1x,player1y,001
if spr=4 then sprite 129,player1x,player1y,000

if pressed(13) ' w = up
if player1Dir <> 3 then player1Dir=1
else if pressed (1) ' s = down
if player1Dir <> 1 then player1Dir=3
else if pressed (0) ' a = left
if player1Dir <> 4 then player1Dir=2
else if pressed (2) ' d = right
if player1Dir <> 2 then player1Dir=4
end if

if player1Dir=1
player1X=player1X
player1Y=player1Y-player1Speed
spr=1
else if player1Dir=2
player1X=player1X-player1Speed
player1Y=player1Y
spr = 2
else if player1Dir=3
player1X=player1X
player1Y=player1Y+player1Speed
spr=3
else if player1Dir=4
player1X=player1X+player1Speed
player1Y=player1Y
spr=4
end if


If Sprite Col (129,130) = 130
player1x=150
player1y=400
y = y - 1
set pen colour red
draw text 150,50,"Avoid the Nanites!"
draw frame
wait mouse click
end if

If y = 0 then loser = 1
If x = 0 then winner = 1

draw frame

until winner>0 or loser>0


If loser = 1
set pen colour red
draw text 100,80,"You Loose!"

else If winner = 1
set pen colour red
draw text 100,80,"You Win!"
end if

draw frame
wait mouse click

close canvas



Still doesn't work!

11-03-2005 23:04

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

well, you told the loop to end whenever either winner or loser is greater than 0. the problem is, look at this line:

int winner = 0, loser = 1

loser is grater than 0 right from the start. so loop ends right from the start.

11-04-2005 01:25

Posted by:
AGhost

Find more posts by AGhost

Thanks ton...

... I feel incredibly stupid right now

AGhost

11-11-2005 11:11

Posted by:
AGhost

Find more posts by AGhost

Why isn't this Working?

Why isn't this Working?

fade down
hide mouse
graphics mode 1040,768
fade up
set pen colour white
frame rect 0,0 to 1040,768

load sprite 131

sprite 131,50,30,000
wait mouse click

sprite 131,50,30,001
wait mouse click

load images 128
load images 130

It just shows black for 2 clicks then skip to the game.

11-11-2005 14:08

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

you didn't use «draw frame» - you must use it everytime you want to show up

11-12-2005 13:26

Posted by:
AGhost

Find more posts by AGhost

fade down
hide mouse
graphics mode 1040,768
fade up
set pen colour black
frame rect 0,0 to 1040,768

load images 131

sprite 131,50,30,000
set pen color black
draw frame
wait mouse click

sprite 131,50,30,001
set pen color black
draw frame
wait mouse click

Fill Rect 0,0 To 1040,768
draw frame

load images 128
load images 130

set pen colour black
frame rect 0,0 to 1040,768
draw frame

draw picture 128
draw frame

x = 10
y = 5

load images 129

It worked, but not it shown Sprite 131 all the time. Is there a way to clear/erase sprites?

Ghost

11-12-2005 14:17

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

> Is there a way to clear/erase sprites?

check the help there's a command to do that (sprite off or soething similar)

11-13-2005 15:36

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

sprite off [sprite_id]

'sprite off' kills all sprites, 'sprite off sprite_id' kills just the one.

Danny (nods)

11-14-2005 20:40

Posted by:
AGhost

Find more posts by AGhost

How would you load more than 1 sprite/image. Right not I am using multiple 'load images' but it doesn't work (displays all as same sprite)

AGhost

11-14-2005 22:48

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

the last argument of the sprite command is the id of the image:

sprite 131,50,30,001

here you use image 1 of the current bank
if you want multiple images, include all your images in the same bank, load it, and just vary the last argument

11-14-2005 23:05

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Sprite images...

Remember, you could put all of your images in one bank, you don't need one bank per image. A sprite is just a picture, and a bank is just a handy way of grouping together related images, if it's easier just put them all in the same place and refer to them by the number in the bank. You can have over 16,000 images in one bank...

Danny (nods)

11-16-2005 19:57

Posted by:
AGhost

Find more posts by AGhost

My multiple banks still isn't working. BTW if I put them all in one bank, how would I do collisions?

I have tried to use Set Image Bank but I don't know if I am using it right:

load images 129
load images 130

set image bank 001,130

sprite 130,0,0,001

set image bank 001,129

sprite 129,0,0,001


Is that right?

11-16-2005 22:44

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

please put them all in the same bank! it will help a lot ;)

> BTW if I put them all in one bank, how would I do collisions?

i don't understand - banks are never used in collision detection!
collisions refer to sprite ids, not their images, so no matter where images come from they're not involved in collisions.

ex:

put 2 images in bank 128.They will be numbered image 0 and image 1. then show 2 sprites like this:

sprite 1,x1,y1,0 ---> sprite #1 uses image #0
sprite 2,x2,y2,1 ---> sprite #2 uses image #1

if sprite col(1,2)>-1 ---> collision between sprite #1 and sprite #2 (the bank is in no way involved here)

11-16-2005 22:48

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

unless you use hundreds of sprites, a bank is just a group of images you want to be loaded at the same time. (For example, you do level 1 and level 2- each level uses different sprites. So create a bank for level 1 and another one for level 2. Before level 1, load the appropraite bank. Then, when it's completed, you unload the bank and load the bank for level 2. THAT is the main use of banks!)

Or perhaps if you have animated sprites with many images, you could group all the images of an animation into a bank to make things clearer.

But for 2 images you really don't need that ;)

Hope it helps

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.