Why Does'nt this work. Its so simple :/

BlitzMax Forums/BlitzMax Beginners Area/Why Does'nt this work. Its so simple :/

Amon_old(Posted 2005) [#1]
Strict

Graphics 1024,768,16,0

Global particle_count:Int = 0



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

Global particle_list:TList = CreateList()

Type particle

	Field x:Int, y :Int

	Function create()

			p:particle = New particle
			p.x = MouseX()
			p.y = MouseY()
			ListAddLast particle_list,(p)
			
			Return p
	
	End Function
	
	Method draw()
	
		For p:particle = EachIn particle_list
		
			DrawImage particles,p.x,p.y,0
		
		Next
		
	End Method
	
End Type

		particle.create()

Repeat

	Cls
	
		For p:particle = EachIn particle_list
			
			p.draw()
			
		Next
		
	Flip
		
Until KeyHit(KEY_ESCAPE)


I dont understand why the function in the type doesnt return an error but when i try to use methods it gives one.


Amon_old(Posted 2005) [#2]
Is it because I'm not using local or globals and the identifiers cannot be found?


Ryan Moody(Posted 2005) [#3]
I think I already gave you the answer! You need something like this in Java:


Class Particle {

   int x, y;

   Particle() {  // Constructor. Must be the same
                 // name as the class in Java.

      x = MouseX()
      y = MouseY()

   }

   ... // Other methods

}

Particle particle = new Particle() // Creates one particle
                                   // using the constructor

particle.draw() // draws the particle



How you go about storing multiple particles dynamically is something you'll have to work out I'm afraid.

Ryan


Amon_old(Posted 2005) [#4]
Thanks Ryan. I got it to work. I was not assigning things as local or global which meant instances couldnt be found.

Also, your code seems easier for some reason.

Thanks :)


Ryan Moody(Posted 2005) [#5]
Ah, no prob!


PowerPC603(Posted 2005) [#6]
You also didn't specify that the function "create" should return a particle, it returns an Int.

Strict

Graphics 1024,768,16,0

Global particle_count:Int = 0



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

Global particle_list:TList = CreateList()

Type particle

	Field x:Int, y :Int

	Function create:particle()

			Local p:particle = New particle
			p.x = MouseX()
			p.y = MouseY()
			ListAddLast particle_list,(p)
			
			Return p
	
	End Function
	
	Method draw()
	
		For Local p:particle = EachIn particle_list
		
			DrawImage particles,p.x,p.y,0
		
		Next
		
	End Method
	
End Type

		particle.create()

Repeat

	Cls
	
		For Local p:particle = EachIn particle_list
			
			p.draw()
			
		Next
		
	Flip
		
Until KeyHit(KEY_ESCAPE)