Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Sprite Hit again

Author
Message
Rob from Scotland
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Scotland
Posted: 26th Mar 2008 20:20
A pupil of mine wrote the following program. I can see nothing wrong but Sprite Hit() isn't working as he and I think it should. Where are we going wrong?

gosub setVariables
gosub makeSprites
gosub moveSprite
end

makeSprites:
load image "bitmaps\rect.bmp",1:sprite 1,x,y,1
load image "bitmaps\redend.bmp",2:sprite 2,600,440,2
return:`from makeSprites

setvariables:
x=300
y=240
speed=4
load sound "cymbal.wav",999
return:`from setvariables

moveSprite:
`test sound 999
play sound 999
do
if rightkey()=1 and x<600 then inc x,speed
if leftkey()=1 and x>0 then dec x,speed
if downkey()=1 and y<447 then inc y,speed
if upkey()=1 and y>0 then dec y,speed
wait 1:`to slow movement
sprite 1,x,y,1
if sprite hit(2,1)<>0 then play sound 999
if sprite collision(2,1)<>0 then play sound 999
loop
return:`from moveSprite

The images used to make the sprites are just simple squares made with Paint. I would have just used the values of x and y to signify that the end had been reached but I don't want to tell him to use my simple value of x and y method when his idea of using sprite hit()might be useful when his redend sprite might not be in the bottom right of the screen.

http://www.freewebs.com/robslearningfun/
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 26th Mar 2008 22:56
It's a 'quirk' in the way that sprite collision works and what happens depends on the colour bitdepth of the screen mode used. (You should always set it).

In your program, you haven't used Set Display Mode, so it defaults to 16bit.

Colour values classed as transparent are not only restricted to the value 0. They depend on the screen bitdepth and are not checked during collision test.

In your program, the sprite's collisions are not being detected due to their colour values. You didn't supply them, so I'm guessing the they are probably primary colours like Red and green.

If they are, then their colour values are probably 255,0,0 and 0,255,0 respectively. With two of the three RGB values being zeros in both sprites, the collision isn't detected. So, you need to change the colour values of at least one sprite.

Run the code snippet of yours (which I altered slightly to correct the layout, account for the missing images/sound and to efficiently demonstrate the problem):



Now change the bitdepth on the first line from 16 to 32 and run it again without changing anything.

Now, collision is detected - and if you look at the code you'll see that all I did was change the red value of one of the images used for the sprites from 0 to 1. (Note: As you have already seen, in 16 bitdepth that alone is not enough).

Normally, this is not a problem as sprite images are rarely a single primary colour and the problem doesn't occur.

TDK_Man

Rob from Scotland
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Scotland
Posted: 27th Mar 2008 00:21
Thanks TDK

I'll get on to this first thing tomorrow morning. Disability (MS) makes working at night very difficult, but I'm sure that you know what you are talking about.

I'll find out tomorrow morning and let you, and all our readers know.

http://www.freewebs.com/robslearningfun/
Rob from Scotland
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Scotland
Posted: 28th Mar 2008 22:07
As expected, TDK's advice worked.

I tried just changing the colour of the sprites from red to orange and from green to dark green and this worked with Ross, the pupil's program.

However, TDK's advice also contained some good general DarkBASIC programming points and I'll certainly make sure to introduce these to my pupils as soon as I can.

Thanks again.

http://www.freewebs.com/robslearningfun/
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 4th Apr 2008 03:26
nice one TDK
this is great, more teachers should come on the forums; there's a lot of knowledge floating around these boards.

Login to post a reply

Server time is: 2025-06-04 03:33:56
Your offset time is: 2025-06-04 03:33:56