Extreme newbie needs some help with moving objects

BlitzPlus Forums/BlitzPlus Beginners Area/Extreme newbie needs some help with moving objects

Trainwiz(Posted 2009) [#1]
I've been trying to do my first real game in BlitzPlus, it's just a ship trying to dodge blackholes while keeping track of your score. It starts up fine, but two problems come up, both similar. First off, if I try to move left or right (the only two keys available) it says "Object does not exist" and closes. If I let a blackhole go off the screen without hitting the player, it also says "object does not exist" and closes. It always points me to the lines of code concerning the movement of my little ship.

Function TestKeys()
 ;If left key is pressed, move player 10 pixels left
	If KeyDown(LEFTKEY)
		
		player\x = player\x - 10
		
		
		;If player moves offscreen, keep him onscreen
		If player\x <= 0
			player\x = 10
		EndIf

And if the blackhole goes offscreen.
(Specifically the ImagesCollide portion)
If ImagesCollide(shipimage,player\x,player\y,0,blackholeimage,blackhole\x,blackhole\y,0)
	CreateExplosion()
	PlaySound(explosionsound)
	blackhole\draw = 0
	player\draw = 0
	Delete player
EndIf 


I thought it had something to do with player\x, but it seems fine to me, here are the types and whatnot just in case.

Type user
	Field x,y ;coordinates
	Field draw  ;1 if player should be drawn, 0 if he shouldn't
	Field frame
End Type

Global player.user = New user  ;Create a new player ship
player\x = 400 ;Player is in the middle of scren
player\y = 440	;Player is near the bottom
player\draw = 1
player\frame = 0


Help please?


Sauer(Posted 2009) [#2]
Your problem is that your program is trying to access your player type instance when it doesn't exist. When you say "DELETE player", the next time it goes through the loop, player doesn't exist, hence "object does not exist".

The best way to check for this is the statement:

If player <> Null


... which basically checks to see if it exists. You would want to do this before anything that is associated with player. If player does equal Null, then you want to have a game over screen or whatever, and if it doesn't, carry out your instructions as you have them.

If I had to guess, you don't need or want to delete the actaul type instance of player. Since the player is the main part of the game, and you only have one, you don't need to delete it. You can keep the same type instance and change a variable like player\lives or player\dead or something like that.

I hope that made sense :)


Trainwiz(Posted 2009) [#3]
Well, the movement part now works, but I'm still getting the object does not exist error when the first blackhole leaves the screen. On top of that, when they collide, the explosion doesn't work.
Edit: Explosions work now, so does every other part of the game, except it still gets the error when the first blackhole goes off.
Edit: Oh, I get it now. It works, thanks =>


Sauer(Posted 2009) [#4]
Glad to hear it.