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.

Newcomers DBPro Corner / Sprite Collision and Deletion

Author
Message
Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 24th Apr 2007 14:23
Well, here's the deal:

I'm trying to make it so when the bullet which is created (as a sprite) when you press the spacebar, and if it collides with the enemy (which is also a sprite), then both the bullet and the enemy dissapear. I'm getting the error: "" The code which i'm using for the collision detection and the deletion of the sprites is on lines 213-218, when the code is pasted into the DBP IDE. I've tried lots of variations for what i'm trying to do, but to no avail. Any help?

Thanks for your time!




Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 26th Apr 2007 13:44
*Bump*

Any help?


TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 26th Apr 2007 16:05
Quote: "I'm getting the error: """


Not seen that one before...

So, it depends what the error is.

Your code is a bit of a mess and the way you have written it is certain to cause you problems in the future. For example, you use GOTO main_menu but that's the next line, so the Goto is redundant (shouldn't really use Goto anyway).

Your move_bullet procedure at first glance appears to be recursive and I'm not sure what you are doing with the delete image commands.

But the problems can all be fixed later - for now, let us know what the error message actually is...

TDK_Man

Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 28th Apr 2007 03:48
Heh, sorry about that, the error is: "Runtime Error 501 - Image Number Illegal at line 201."


TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 29th Apr 2007 06:55
Line 201 is actually 'IF FiredBullet=1', but you can't always trust DB's runtime error line number reporting.

The error message is always reliable though and that one suggests that the image number on a Paste Image or a Sprite line is at fault.

As the line following 201 is GOSUB move_bullet, it suggests that the error is in the move_bullet procedure.

First of all, it should be IMAGE EXIST(5) not IMAGE EXIST (5), so change that first. (I would have used IF IMAGE EXIST(5)=0 but AFAIK NOT used like that in DBP is supported).

OK, when in the move_bullet procedure, your code says:

If image 5 doesn't exist then make sprite 5 using image 9 and if it does then call the move_bullet procedure again.

As you don't load or create image 5 anywhere, all it can ever do is create the sprite, so what's the Else part for?

Anyway, the procedure carries on and one of the things it does is delete image 9 when sprites 4 and 5 collide.

It's my guess that when this happens, the next time move_bullet is called, the missing image 9 is causing the problem. Try NOT deleting any images that are used repeatedly in making sprites - just delete the sprite (or hide it/place it off screen until it's needed again).

TDK_Man

Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 29th Apr 2007 07:37 Edited at: 29th Apr 2007 07:37
Hm, I tried just deleting the sprite, and also fixed up the IMAGE EXIST functions, but now i'm getting the error: "Runtime Error 302 - Sprite does not exist at line 212"

This is the code i'm compiling now:




TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 29th Apr 2007 10:01 Edited at: 29th Apr 2007 10:04
Trace through your code one line at a time imagining what the computer would do for each line.

The error message tels you what problem you are looking for - a sprite related command which uses a sprite number which doesn't exist.

You won't be able to ask here for someone to tell you how to fix every single error in your programs - learning how to debug your code is part of the learning process.

That's why I always stress to new programmers the importance of using proper program layout, NOT using Goto and always using indented code. They all make finding and fixing errors so much easier.

Besides, you ignored some of the last advice I offered - the Goto Main_Menu is still there...

TDK_Man

Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 29th Apr 2007 10:12
Alright, thanks for the advice.


TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 29th Apr 2007 10:14
Lol!

I was just typing a reply to another post and when I'd finished, I found you had already solved the problem.

How come you can debug other people's faulty programs but not your own?

TDK_Man

Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 29th Apr 2007 10:16
Heh, I don't know actually, but that problem was fairly simple, and i've had that problem myself a bit before as well.


TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 30th Apr 2007 03:44
Well, my money is on the fact that he's deleted Sprite 4 too. On returning to the procedure, he re-creates sprite 5, but not number 4. (Though the code in the post has been updated since I last looked at it).

I find it's sometimes better to point someone in the right direction rather than say exactly what an error is. I find you learn more that way and you get a sense of achievement when you crack it!

Quote: "(as the whole condition can't be true anyway) but it will"


What if the second part includes an 'Or'? If DB didn't check all of it, this wouldn't be seen. For example:

If A=0 And B=0 And D=0 Or E=12 Then CLS

If A=1 and E=12 are you saying that as A returns false then it shouldn't do the CLS?

TDK_Man

Veron
17
Years of Service
User Offline
Joined: 22nd Nov 2006
Location:
Posted: 30th Apr 2007 12:58
Heh, I totally forgot about this thread, I sat down, killing my brain for a good solid hour, and solved the problem myself, but thanks for the help, TDK and WindowsKiller, it's much appreciated.


TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 30th Apr 2007 17:52
Quote: "Why would I say that?"


I thought you did...

Quote: "One would assume that DarkBasic will not check the 2nd and 3rd condition if SPRITE EXIST(4) is false (as the whole condition can't be true anyway)"


I was just pointing out that if DB didn't check the whole line it wouldn't know about the 'Or' part.

Quote: "Moreover, your example is ambiguous, you need to add parentheses if you mix AND and OR this way."


How can it be ambiguous if I give an example that actually works fine in DB. Don't forget we are talking about DB here - not other languages or how we think it should work.

TDK_Man

TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 2nd May 2007 22:09 Edited at: 2nd May 2007 22:10
I repeat what you said:

Quote: "you need to add parentheses if you mix AND and OR this way"


I provided an example in DB that works - which proves that in DB the statement you made is not true.

Quote: "It's probably only working the way you expect it to by pure chance..."


Oh, so it might work differently next time I do it?

Look, your diagnosis of Veron's problem was spot on. I've never said you were wrong.

I was just pointing out that it was wrong to assume that DB didn't check the whole of the line with a list of conditions as it clearly does - something that DB maybe does differently from some other programming languages.

So, we appear to be arguing about what you think DB should do and what I say DB does do - whether it should do it or not is a different matter entirely!

We could argue about this all day, but it's pointless as we are both right.

TDK_Man

Login to post a reply

Server time is: 2024-09-25 21:28:44
Your offset time is: 2024-09-25 21:28:44