I need some help with this

BlitzPlus Forums/BlitzPlus Programming/I need some help with this

En929(Posted 2009) [#1]
I have a part in a game that I've been building where the character is to jump into the door to get to the another part in the game. But, when the player (HENRY) jumps into the door, I get an error message in the "MOVEMENT" variable that says "Object doesn't exist." Thus, I was wondering how do I fix my game so that I don't get that message?

Here's the screenshot

http://authorhenryemphrey.tripod.com/gallery/


and below is how I have my game coded. I added notes in the code so that one can go straight to where I think the problem is.


   

Graphics 1640, 1000
SetBuffer BackBuffer ()
timer = CreateTimer(20)

;Loading and storing the images that the game will use 

HENRY = LoadAnimImage ("Move2.png",400,370,0,13)
PLATFORM = LoadImage ("Platform.png")
DOOR = LoadImage ("Door.png")
BACKGROUND = LoadImage ("BackgroundTree.jpg")
BACKGROUND2 = LoadImage ("Spaceship.jpg")
MONSTER = LoadImage ("Ball.png")

;---------------------------------------------

;Setting the Fields

Type HENRY

	Field x,y
	
	Field frame 
	

End Type 

Type BACKGROUND

	Field x,y
	
	Field image

End Type


Type PLATFORM

	Field x,y
	
	Field image
	
End Type

Type MONSTER

	Field x,y
	 
	Field image

End Type


Type DOOR

	Field x,y
	
	Field image
	
End Type 



;----------------------------

;Setting the original names to variables

h.HENRY = New HENRY
h\x = 500
h\y = 300
h\frame = 0


b.BACKGROUND = New BACKGROUND
b\x = 150
b\y = 150
b\image = BACKGROUND

p.PLATFORM = New PLATFORM
p\x = 200
p\y = 370
p\image = PLATFORM




m.MONSTER = New MONSTER

m\x = 600
m\y = 200
m\image = MONSTER



d.DOOR = New DOOR

d\x = 200
d\y = -200


;-----------------------------

;Miscellaneous and accessories





MOVEMENT = 1




;-----------------------------------

;the game loop


While Not KeyDown(1)

		Cls 
	
	
;making it so that the player appears to move right		
		
		
If KeyDown(205)

		b\x = b\x - 3
		h\frame = h\frame + 1
		
		If h\frame > 2 Then 
		h\frame = 0
		
		EndIf 
		
EndIf 

;making it so that the player appears to move left



If KeyDown(203)
		

		
		b\x = b\x + 3
		h\frame = h\frame + 1
		

		If h\frame > 12 Then
		h\frame = 6


       EndIf

EndIf   




;this allows the player to stand on the platform

If ImagesCollide(HENRY,h\x,h\y,0,PLATFORM,(b\x + p\x),p\y,0) Then 

		
	    h\y = 300
	
Else 

		h\y = h\y + 10


		
EndIf 



;this makes the player jump

If KeyHit (57)
 
h\y = h\y -200
			
		
			
EndIf 


;this makes the player come back down after jumping (maybe this is 
;gravity, I think). But, maybe I need to learn how to fix this part too, 
;for the player doesn't jump as eloquently as Mario on Super Mario 
;Brothers


If Not KeyHit (57)

 h\y = h\y + 10

EndIf 
		
		
		
;this makes the player (Henry) appear on screen		
		
DrawImage (HENRY,h\x,h\y,h\frame)

;this makes the platform appear on screen


DrawImage (PLATFORM,(b\x + p\x),p\y)


;here's one of the problems. This is what makes the MONSTER move backwards and forward. But when the player collides with the door,
;there's an error message that says "Object does not exist." This this is the part that I need help with.



m\x = m\x + MOVEMENT 

If m\x > 800 Then MOVEMENT = -MOVEMENT 
If m\x < 700 Then MOVEMENT = 1  


;this makes the Monster appear on screen

DrawImage (MONSTER,(b\x + m\x),m\y)	




;this is the door that the player collides with. Again, here is part of the problem. When the player collides with the door, to exit
;to exit the scene, I get an error message above in my MOVEMENT variable. The error message says "Object does not exist." Thus, 
;what do I do to fix this?


If (d <> Null) Then 

 				DrawImage (DOOR,(b\x + d\x),d\y)
	
					If ImagesCollide (HENRY,h\x,h\y,0,DOOR,(b\x + d\x),d\y,0) Then
		
							
							
						
							Delete d	
							
							Delete m
					
							b\image = BACKGROUND2
					

					
						
					
					EndIf 
		EndIf 	

	
		


Flip





Thanks


Sauer(Posted 2009) [#2]
When you collide with the door, you delete m. So, on the next iteration of the loop, the code comes around looking for m\x and it can't find it, as it was deleted. You need to either not delete M, or make a new instance of M after you delete it.


En929(Posted 2009) [#3]
Sauer, after I have added the line:


If (m <> Null) Then 

	DrawImage (MONSTER,(b\x + m\x),m\y)	 

If (d <> Null) Then  

 				DrawImage (DOOR,(b\x + d\x),d\y) 
	
					If ImagesCollide (HENRY,h\x,h\y,0,DOOR,(b\x + d\x),d\y,0) Then 
		
							
							
						
							Delete d	 
							Delete m
						
					
							b\image = BACKGROUND2 
					

					
						
					
					EndIf  
		EndIf  	

	
		
EndIf 



I still got the same message. As an experiment, I tried taking out the line where it says:


 


m\x = m\x + MOVEMENT 

If m\x > 800 Then MOVEMENT = -MOVEMENT 
If m\x < 700 Then MOVEMENT = 1  




And it worked. The monster and everything disappeared like I wanted it. But, when I added the movement back, I got that message. Thus, I wondering what do I do to stop the error message from occuring when I add movement?


Who was John Galt?(Posted 2009) [#4]
Like Sauer said, after you have deleted m, any reference to it is meaningless, including movement. Just stick your movement code in the block that has checkes m<>NULL.


Sauer(Posted 2009) [#5]
Yeah, you wrapped your code that has m\y in the IF check, but it's still coming to m\x later in the code and has no idea what it is.


En929(Posted 2009) [#6]
Thanks Sauer and John. In the bottom code block, I moved the MOVEMENT code to:


If (m <> Null) Then 


m\x = m\x + MOVEMENT 

If m\x > 800 Then MOVEMENT = -MOVEMENT 
If m\x < 700 Then MOVEMENT = 1  



	DrawImage (MONSTER,(b\x + m\x),m\y)	 

                      If (d <> Null) Then  

 				DrawImage (DOOR,(b\x + d\x),d\y) 
	
					If ImagesCollide (HENRY,h\x,h\y,0,DOOR,(b\x + d\x),d\y,0) Then 
		
							
									Delete m
						
									Delete d 
							
						
									b\image = BACKGROUND2
						
					
						
					
					EndIf  
		EndIf  	

	
		

	


Thus, it worked. Thanks again.


Yeshu777(Posted 2009) [#7]
Glad to hear it Henry, been following your progress for a few weeks now.

As a general rule, it's always best to check a 'type' exists or is valid before perfoming any checks or operations on it.

These things will become second nature the more you use Blitz.

Best Regards,


Matty(Posted 2009) [#8]
However, just to throw a spanner in the works - if you design it right from the beginning there will be no need to check if the type instance exists - just need to get the logic right..


Sauer(Posted 2009) [#9]
For example, it isn't always necessary to delete an instance of a type when it goes off the screen. Sometimes you just need to move it or not draw it, as to give the impression it went away. Then, when you want to use it again, your code still has all the data.

Just something to think about, keep working hard.


schilcote(Posted 2009) [#10]
Yeah, I bet older RPG games (like Earthbound) did that, that's why if you manage to get a screen away from your enemies they'll disappear and be replaced by another bunch of enemies (or none at all).


Caleb(Posted 2009) [#11]
Good job guys on answering the main question.

About your jumping problem: this might help make it a little more "eloquent"...

Think about when you jump in real life. You start at a certain velocity going upwards, but gravity slows you down, and then at the apex of your jump, you begin to speed towards the ground. If you're good at math, this is a quadratic function; what you're doing now is a linear function (none of that really matters, but I thought I might add it...).

So now for the code. First make a variable. Call it something like "jump" or something like that.

Second add another field to the "h" type and call it "yvel" or "yvelocity".

Then add the following code in place of your current jumping code.

If KeyHit(57)    ;If you hit spacebar

    If jump = 1   ;make sure jump = 1

        h\yvel = 10   ;NOTE: change this number to make him jump higher or lower

        jump = 0  ;this will prevent jumping again until he hits the ground

    EndIf

EndIf


Then at the beginning of the game loop, put the following:

h\y = h\y - h\vel ;this moves him up by whatever h\yvel is

h\yvel = h\yvel - 1  ;NOTE: change the "1" to something else to make him rise and fall faster or slower



Finally, change the ImagesCollide before the jumping code to match this:

If ImagesCollide(HENRY,h\x,h\y,0,PLATFORM,(b\x + p\x),p\y,0) Then 
	
	    h\y = 300

            h\yvel = 0  ;this keeps him from continually going down - ImagesCollide should take care of that, but use this just in case

            jump = 1 ;this allows jumping after he returns to the ground
			
EndIf



And that should do it. It should be much more like Super Mario Bros. Feel free to experiment with those 2 values in the code to make the jumping more realistic.


En929(Posted 2009) [#12]
Caleb, I added the things that you said. In that, the character HENRY 's Jump mechanism has improved. But, I'm still trouble with him jumping in thin air if the space bar happens to be pressed twice while he's in the air. There's probably something that I didn't put in right. Below I wrote out comments on what I did and how I added things. I started the comments with ;------ as for things that I've done:



Graphics 1640, 1000 



HENRY = LoadAnimImage ("Move2.png",400,370,0,13)

ENEMY = LoadImage ("MintMonster.png")

BACKGROUND = LoadImage ("DockII.jpg")

PLATFORM = LoadImage ("Big Platform2.png")


ENEMY = LoadImage ("MintMonster.png")






SetBuffer BackBuffer () 




Type HENRY
	Field x,y
	Field frame
	Field yvel
	Field vel 
End Type



Type ENEMY

	Field x,y


End Type 


 
Type BACKGROUND 

	Field x,y 
	
	Field image

End Type 


Type PLATFORM

	Field x,y
	
	Field image 
	
End Type 



;-------besides the h\x,h\y, and h\frame "h types," I added the h\yvel and h\vel (but am not sure if I did right when adding the h\vel)
	
h.HENRY = New HENRY

h\x = 500
h\y = 300
h\frame = 0
h\yvel = 0
h\vel =-5



e.ENEMY = New ENEMY

e\x = 800
e\y = 410






b.BACKGROUND = New BACKGROUND
b\x = 0
b\y = 700
b\image = BACKGROUND 


p.PLATFORM = New PLATFORM
p\x = 100 
p\y = 410
p\image = PLATFORM


;------here's the added Jump variable. I set it to "1"

Jump = 1

MOVEMENT = 1
EDMOVEMENT = 1





While Not KeyDown (1)

Cls 


TileImage (b\image,b\x,b\y)	


DrawImage (p\image, b\x + p\x,p\y)



DrawImage (HENRY, (b\x + h\x),h\y,h\frame)




;-----I added this section to the beginning of my game loop. Was this added correctly?


h\y = h\y - h\vel   ;this moves him up by whatever h\yvel is (---this is one of the recommended sections that I added)

h\yvel = h\yvel - 1  ;NOTE: change the "1" to something else to make him rise and fall faster or slower (----this is another recommended sections that I added)








If KeyDown(205)
	h\x = h\x + 3


	b\x = b\x - 3	h\frame = h\frame + 1
		
		If h\frame > 2 Then 
		h\frame = 0
		
		EndIf 

EndIf 
  





If KeyDown(203)

		
		
		h\x = h\x - 3
	
		b\x = b\x + 3 h\frame = h\frame + 1
		

		If h\frame > 12 Then
		
		 h\frame = 6


     	EndIf 

EndIf   




If KeyHit(57)    ;-If you hit spacebar 

h\y = h\y -200 ;-----Here's the part that I added. Maybe this part should be taken out. But, when I took it out he HENRY 
				;wouldn't jump. But, when this is added, if the space bar is pressed more than once, HENRY jumps in thin air. 
				;Thus, it seems that I should take this part out, but again HENRY won't jump at all If that was done.


    If jump = 1   ;----make sure jump = 1 (this section was recommended)

        h\yvel = 50  ;----NOTE: change this number to make him jump higher or lower (this section was also recommended)

        jump = 0  ;----this will prevent jumping again until he hits the ground (this line was recommended as well)

    EndIf

EndIf






If ImagesCollide(HENRY,h\x,h\y,0,PLATFORM,(b\x + p\x),p\y,0) Then  
	
	    h\y = 430

            h\yvel = 0  ;-----this keeps him from continually going down - ImagesCollide should take care of that, 
						;but use this just in Case

            jump = 1  ;----this allows jumping after he returns to the ground
			
EndIf






If (e <> Null) Then

      e\x = e\x + MOVEMENT 

			If e\x > 800 Then MOVEMENT = -MOVEMENT 
			If e\x < 700 Then MOVEMENT = 1  
					DrawImage (ENEMY,(b\x + e\x), e\y)

						

									
	


						EndIf 	
						
					
					

Flip 

Wend 




Caleb(Posted 2010) [#13]
Sorry for taking so long to respond... I need to bookmark this thread or something...

Well, for starters, I made a typo... That h\vel in the "h\y = h\y - h\vel" should be a h\yvel... Oops...

That typo of mine is one of the reasons why he won't jump. The second reason is that the "ImagesCollide" part should go before the "If KeyHit(57)". The problem with it at the moment is that once h\yvel is set for how high he's gonna go, ImagesCollide() negates it because the images are already colliding, and sets it back to 0, hence why he doesn't move.

The problem of doublejumping with your new "h\y = h\y - 200" (which you can get rid of now, btw) is that it raises him 200 pixels whether or not jump = 1 because it's not inside the If-then statement.

Also, another thing you can change is the "h\y = 430" (it was 300 originally, wasn't it?) to an If-then statement.

If h\y > 430 ;this is in case he falls below the platform temporarily
    h\y = 430 ;put him back on solid ground
EndIf


Originally, this was another thing that was causing problems with your ImagesCollide(). Once again, because the ImagesCollide() was after the Spacebar checker, it reset h\y before the changes could be made onscreen via the drawing section. Now, with this new part, he only gets reset if he falls below the platform (which can sometimes happen because of moving multiple pixels at a time). This happens so fast, though, it usually looks just like he's landing perfectly.

I know this kinda sounds like a lecture (believe me, I'm falling asleep while writing it, so I fixed your code and included it below. I tested it several times to make sure it is typo and error free... lol...



Hope this helps.


En929(Posted 2010) [#14]
Thanks Caleb. The code worked very well.





I know this kinda sounds like a lecture (believe me, I'm falling asleep while writing it, so I fixed your code and included it below.




Thanks a bunch. As a person who is starting out as a beginning programmer, I appreciate all lectures lol.