What am I doing?

BlitzMax Forums/BlitzMax Beginners Area/What am I doing?

Amon_old(Posted 2005) [#1]


As you can see from the above I have no clue as to what I'm doing.

Dont worry about the update() method it just has a simple thing in it. All I'm trying to do now is get it to work but it doesnt. I still have not grasped OOP.


Ryan Moody(Posted 2005) [#2]
Hi Amon, just visiting!

I don't know the BlitzMax language, but I'm guessing it won't run because it appears you haven't instantiated the object called particle (you need something like Particle particle = new Particle() in Java). What do others think?

Ryan


Amon_old(Posted 2005) [#3]
I dont know how to fix it. I get an eroor saying "Identifier p not found".

You see the p:particle = new particle is in a function in my type. Now I'm trying to call that function to create the particles but it doesnt want to know.


Ryan Moody(Posted 2005) [#4]
That shouldn't be in the function in your type - create object p outside of the type declaration, before the Repeat Until loop.


Ryan Moody(Posted 2005) [#5]
A Java solution:

Class Particle{

   int X, Y;

   Particle(){  // Constructor
      X = 400;
      Y = 300;
   }

   Draw()...
   Update() ...

}

Particle p = new Particle() // Creates one particle

Repeat

   p.Draw()    // Calls the Draw / Update methods on object p
   p.Update()

Until keydown(1)



Amon_old(Posted 2005) [#6]
Thanks Ryan. I'll try that. :) But then wouldnt that leave the one particle you created outside the loop non updateable?


Ryan Moody(Posted 2005) [#7]
Absolutely not. As long as the object is being referred to by a pointer (in this case, 'p'), you can call any of the methods you've defined onto it.


Ryan Moody(Posted 2005) [#8]
Well, I suppose what you want is this:

Particle[] particles = new Particle[100] //Create 100 particle references.

For count = 1 to 100
   particles [count] = new Particle() // Create 100 particles
next 

Repeat

   UpdateAllParticles()

Until Keydown(1)

UpdateAllParticles(){

   for count = 1 to 100
       particles[count].update() // update each particle
   next
}



tonyg(Posted 2005) [#9]
Amon, lots of stuff to change but I'm having loads of trouble getting my head around it as well.
This code 'runs' but doesn't do much...
Strict

Graphics 1024,768,16,0

Global particle_count:Int = 0


SetMaskColor 255,0,255
 
Global particles:TImage = LoadImage("particle.png",MASKEDIMAGE)

Type particle
Global particle_list:TList
	Field x:Int, y:Int, s:Int
	
	Function create()
	         If particle_list = Null particle_list = CreateList()

	
		While particle_count = 0
		
			For Local i:Int = 0 To 100
		
				Local p:particle = New particle
				p.x = MouseX()
				p.y = MouseY()
				p.s = Rand(3,10)
				
				particle_list.addlast(p)
				
				 If i:Int >=100
					
					particle_count = 1
					
				 EndIf
				
			Next
			
		Wend
		
		
		
	End Function
	
	
	Method destroy(p:particle)
	
		For Local p:particle = EachIn particle_list
				
			If p.x >1026 Or p.x < -2	
			
				p:particle = Null
				
			EndIf
			
			If p.y > 770 Or p.y < -2
			
				p:particle = Null
				
			EndIf
			
		Next
		
'		Return p
		
	End Method	
	
	
	Method update()
			x:+3
	End Method
	
	Method draw()
	DrawImage particles,x,y,0
		
	End Method
	
	
End Type


			
Repeat

	Cls
	
	
		particle.create()
	If particle.particle_list
		For Local p:particle = EachIn particle.particle_list
		Print p.x
          p.draw()
         Next
    EndIf
Rem
	If particle.particle_list
		For Local p:particle = EachIn particle.particle_list
          p.update()
         Next
    EndIf
End Rem


	
	Flip

Until KeyHit(KEY_ESCAPE)


Changes...
Global particle_list:TList moved into the Particle type definition.
If particle_list = Null particle_list = CreateList() added to the create function.
list.addlast particle,partical_list changed to particle_list.addlast(p) (and partical changed to particle)
p.particle = Null in destroy method changed to
p:particle = Null
Return from destroy method commented out (couldn't get it to work).
p.create() changed to particle.create() as the function belongs to the type and not the instances.
Following used to call draw() and update()
	If particle.particle_list
		For Local p:particle = EachIn particle.particle_list
		Print p.x
          p.draw()
         Next
    EndIf
	If particle.particle_list
		For Local p:particle = EachIn particle.particle_list
          p.update()
         Next
    EndIf

Draw and update changed to remove the for loops and the unnecessary ID...
	Method update()
	    Print "Update called"
		
         x:+3
			
		
	End Method
	
	Method draw()
	    Print "draw called"
	
			DrawImage particles,x,y,0
			
		
	End Method

Hope it helps.
<edit> ...and I reserve the right to have got this very wrong and missed a few changes I made. I think you used p.particle in some places when it should be p:particle.


Amon_old(Posted 2005) [#10]
Thanks very much tonyg. I'm still confused by all this OOP. I'll give your edited version a go.

:)


Amon_old(Posted 2005) [#11]
Thanks to Ryan aswell. :)


tonyg(Posted 2005) [#12]

I'm still confused by all this OOP


so am I. A lot of stuff I do is trial and error. It's slowly sinking. OOP *is* good as, once the set-up of types/functions/methods etc is done, it's quite easy to maintain. However, I find it too 'precise' to be much fun.