Deleting an ORBE that's pushed into the DOOR

BlitzPlus Forums/BlitzPlus Programming/Deleting an ORBE that's pushed into the DOOR

En929(Posted 2009) [#1]
Thanks for helping me with my last question. The advice that I was given worked, and it seems like I'm starting to get the hang of BlitzBasic already and I'm an extremely beginning programmer.

I now have another question. In my game, I'm trying to make it so that when the player (EATUM) pushes an ORBE into a door, the ORBE is deleted. But, when the ORBE is pushed into the door, I keep getting an "Object doesn't exist" error message. Even when I make the ORBE an array (like I did with my apples), I still get an error message that says "Object doesn't exist." Thus, how do I fix that?

Thanks
EN





The source code is below this line.

----------------------------------------------------------------



Graphics 900, 900
SetBuffer BackBuffer()
 


EATUM = LoadImage ("Eatum.png") 
APPLE = LoadImage("Apple.png")
ORBE = LoadImage("Orbe.png")
DOOR = LoadImage("Door.png")
BACKGROUND = LoadImage("Clouds.jpg")



Type EATUM

   Field x,y

End Type 	

		
	
Type APPLE

   Field x,y 

End Type 



Type ORBE

   Field x,y

End Type 



Type DOOR

     Field x,y

End Type



e.EATUM = New EATUM

e\x = 70
e\y = 200



For z = 1 To 5

a.Apple = New Apple
a\x = 100+30*z
a\y = 90*z
Next 


For j = 1 To 1
o.ORBE = New ORBE
o\x = 100*j
o\y = -200*j
Next 


d.DOOR = New DOOR
d\x = 50
d\y = 50


Score = 0
ScrollRightLeft = -200


While Not KeyDown (1)  


Cls

TileImage BACKGROUND,ScrollRightLeft


DrawImage (EATUM,e\x,e\y)
DrawImage (ORBE,o\x,o\y)
DrawImage (DOOR,d\x,d\y)




;puts the apples in the game. 

For a.APPLE = Each APPLE
DrawImage(APPLE,a\x,a\y)
If ImagesCollide(EATUM,e\x,e\y,0,APPLE,a\x,a\y,0) Then Delete a score = score + 1 
Next 
Text 150,150, "Score" + Score





;when the left arrow key is pressed, player moves to the left and the background moves too.

If KeyDown(203)
e\x = e\x - 3 
ScrollRightLeft = ScrollRightLeft + 3



;when the player is moving left and collides with the ORBE, the player the ORBE will be pushed in the left direction.

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\x = o\x - 3
EndIf 




;when the right arrow key  is pressed, player moves to the right and the background moves too.

If KeyDown(205)
e\x = e\x + 3
ScrollRightLeft = ScrollRightLeft - 3



;when the player is moving right and collides with the ORBE, the player will push the ORBE in the right direction too.

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\x = o\x + 3
EndIf 




;when the up key is pressed, the player moves up

If KeyDown(200)
 e\y = e\y - 3



;If the player collides with the ORBE while moving up, then the ORBE will be pushed up as well.

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\y = o\y - 3
EndIf 




;when the down key is pressed the player moves down

If KeyDown(208) 
e\y = e\y + 3



;if the player is moving down and collides with the ORBE, the ORBE will be pushed in the down direction as well

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\y = o\y + 3
EndIf 




;Now, Here is the part that is not working in my program. I'm trying to make it so that when the ORBE is pushed into the door by the player, the it disappears and is deleted, but when I do that, I get an error message that says that "the object doesn't exist." I also tried to make my ORBE an array like I did with the apples, but I still get the same error message. It says that the "Object doesn't exist." Thus, how do I fix it?

If ImagesCollide(DOOR,d\x,d\y,0,ORBE,o\x,o\y,0) Then Delete o Text 350,300, "Great Job"





Flip
Wend


Sauer(Posted 2009) [#2]
Does it say the error is in that line, or another line of your code?

Also, please use a code box for posting code, using <code>put your code here</code>, but replaces the <> with []


En929(Posted 2009) [#3]
Thanks for replying. When I run my game code and when the ORBE collides with the door, I get a message that says that there is an error in another line; the line that says:

 DrawImage (ORBE,o\x,o\y) 



But, even when I delete the above line and replace it with a "For" and "Each" loop written like this:


For o.ORBE = Each ORBE
DrawImage (ORBE,o\x,o\y)
If ImagesCollide(DOOR,d\x,d\y,0,ORBE,o\x,o\y,0) Then Delete o 
Next 
Text 350,300, "Great Job"   



When I try to play the game, it says that there is an error in the line that says:



If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\x = o\x - 3
 



As in the line:


If KeyDown(203)
e\x = e\x - 3 
ScrollRightLeft = ScrollRightLeft + 3

If ImagesCollide(EATUM,e\x,e\y,0,ORBE,o\x,o\y,0) Then o\x = o\x - 3

EndIf 




And thus the game won't work (that is, when I press any of the arrow keys, the game won't work - I get the same message that says "Object Doesn't Exist"). Thus, what do I do to get it to work correctly?

Thanks
En


Sauer(Posted 2009) [#4]
Ok, that's what I expected... you've basically deleted your last ORBE. When you delete the ORBE, directly after, create a new one. Then, when the program tries to draw it or check if there was a collision, it will be there, and the error shouldn't come up.


En929(Posted 2009) [#5]
Thanks, it almost works. I no longer get the error message anymore. I changed the collision code to:

 DrawImage (ORBE,o\x,o\y)
If ImagesCollide(DOOR,d\x,d\y,0,ORBE,o\x,o\y,0) Text 350,300, "Great Job" Delete o o.ORBE = New ORBE 


And now, I don't get an error message anymore and it's colliding the way that I wanted, but now after my ORBE is pushed into the door, another ORBE literally appears. After the first ORBE disappears, I wanted that first ORBE to be the only ORBE. Thus, what do I do now?

Thanks, you're helping a bunch!
En


Sauer(Posted 2009) [#6]
Well, rather than make a new one, if you only want one, check to see if ORBE exists before you check for collisions. You can do this by:
If o.orbe<>Null
  ;code for orbe stuff
Endif


I don't have B+ with me right now, so it might be "If o<>Null"... not entirely sure. But that's what you want to do.


Yeshu777(Posted 2009) [#7]
Already done, check the other forum post.

http://www.blitzbasic.com/Community/posts.php?topic=86975

Additionally, I would put the collision check & null check in one function & call that, returning True or False.


En929(Posted 2009) [#8]
Wow, thanks Yeshu and Sauer. Those things that you showed me worked. And Yeshu, thanks for showing me the importance of indentation. I can see that indentation also makes some of the commands work better. So, I'll start doing that now. I'll post more if I have anymore questions.

Thanks again,
En


Matty(Posted 2009) [#9]
Indentation has no effect of whether the commands work or not (unless we're talking about some variants of older languages like Cobol), it is simply for ease of understanding the code you've written.


neos300(Posted 2009) [#10]
Matty, don't forget python!