What am I doing?
BlitzMax Forums/BlitzMax Beginners Area/What am I doing?
| ||
| 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. |
| ||
| 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 |
| ||
| 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. |
| ||
| That shouldn't be in the function in your type - create object p outside of the type declaration, before the Repeat Until loop. |
| ||
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) |
| ||
| Thanks Ryan. I'll try that. :) But then wouldnt that leave the one particle you created outside the loop non updateable? |
| ||
| 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. |
| ||
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
} |
| ||
| 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. |
| ||
| Thanks very much tonyg. I'm still confused by all this OOP. I'll give your edited version a go. :) |
| ||
| Thanks to Ryan aswell. :) |
| ||
| 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. |