Going to and away from an Array

BlitzPlus Forums/BlitzPlus Programming/Going to and away from an Array

En929(Posted 2009) [#1]
I have another question. I was trying to find out a simple way of keeping an array (of apples) in one place, giving the appearance that the character is going towards or away from an object. For example, in Super Mario Bros, the coins always stay in one place until Mario collects those coins. That's what I'm trying to do with the apples below. In the apple sections in the code below, I explained what I did so far.

I tried having the array moving with other objects (like the platform, etc) when Key (205) – the Right key is pressed down, and Key (203) - the left key is pressed down, but I got an error message with that. I tried making them move with another object as in a p\x + a\x scenerio. However, I still got an error message with that too. The error messages in both instances said that the "object doesn’t exist.”
--------------------------------------------------------------------------



Graphics 1640, 1000
SetBuffer BackBuffer ()


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

;Load Items

HENRY = LoadAnimImage ("Move2.png",400,370,0,13)
BACKGROUND = LoadImage ("BackgroundTree.jpg")

PLATFORM = LoadImage ("Platform.png")

APPLE = LoadImage ("Apples.png")



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 APPLE

	Field x,y
	
	Field image
	
End Type 

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


;here are the apples. I'm trying to give the appearance of them 
;staying in one place, and the player having 
;to eventually get to these apples .



For z = 1 To 5 
a.APPLE = New APPLE
a\x = 450 + (50*z)
a\y = 30 
 
Next 


While Not KeyDown (1)

  Cls 
		



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



;here's what I'm working with now. I'm trying to figure out a 
;simple way to get the array of apples (a\x) to
;give the appearance of the apples staying in one place. But, 
;when I tried various loops and put them with
;the keydown element, I kept getting error messages. It's probably 
;something that I'm not doing right

		
For a.APPLE = Each APPLE


	DrawImage(APPLE, (b\x + a\x) ,a\y) 

	 
        If ImagesCollide(HENRY,h\x,h\y,0,APPLE, (b\x + a\x) ,a\y,0) Then  
 			
		
	      Delete a  
		 
          score = score + 1  
		
	      PlaySound (COLLECTIONSOUND) 

        EndIf  
		
Next  



;this line makes the player go to the right. In this, the 
; platform, etc. are used to give a smoother 
;appearance of going to and away from things; but again, I'm 
;trying to make the array of 5 APPLES do the same.
 

If KeyDown(205)
		
		
		p\x = p\x - 3
		
		
		b\x = b\x - 3
		h\frame = h\frame + 1
		
		If h\frame > 2 Then 
		h\frame = 0
		
		EndIf 

EndIf 

;this line makes the player go to the left. Again, this line 
; gives the appearance of going to and away from things (like the 
;platform), but, I'm trying to do the same with the array of 5 
;APPLE


 
If KeyDown(203)
	
		p\x = p\x + 3
	
	
		b\x = b\x + 3
		h\frame = h\frame + 1
		

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


       EndIf
EndIf 

If KeyDown (57)
 
h\y = h\y -80
			
		
			
EndIf 



If Not KeyHit (57)

 h\y = h\y + 10

EndIf 


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 



		

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

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



Flip

Wend 




Sauer(Posted 2009) [#2]
Off the top of my head, I think this should work:

If KeyDown(205)
		For a.APPLE=Each APPLE  ;for every apple
                            a\x=a\x-3   ;move left 3
	            Next

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

EndIf 



En929(Posted 2009) [#3]
Thanks again Sauer. It worked.