All,
I am new to programming with Xojo, so apologies if this is an ignorant question. I am attempting to simulate a farm field in which new tree seedlings establish and grow. I’d like to run a timer with units of perhaps one month, and have all the tree seedlings grow a little bit each month. But it may involve dozens to hundreds of seedlings. The seedlings will each be a small jpg cartoon. I assume I’ll use an array to handle them, but should I use one or many Canvases? If one, then should each tree seedling be an object?
Thanks, Chris Peterson
Use a single canvas. I would create a ‘tree seedling’ class and have it manage what it needs to display and where. And yes there would be an array of hold all of the seedlings.
something like
For Each plant As Plant In Plants
if plant.IsVisible() then
plant.Draw(g)
endif
plant.Grow(timespan)
this Plant class have all properties and methods you need
you could also use one canvas each field.
i would subclass a canvas and add functionality there too
Ah, I see. Use a single Canvas, and DrawPicture the matching growing picture at its location on the screen.
Thank you all for valuable feedback. I much appreciate it!
Ahhhhh…another problem has arisen. I want to use Timer to iterate across months. It’s unclear to me how to code the Timer so that the Canvas gets refreshed each time the Timer fires. I can put simple commands like a message box in the timer and they work fine, but not sure how to write out the invalidate or refresh for Canvas. I am in Xojo 2021, so I guess old invalidate has been replaced by the new refresh?
you would have one method with time span input to let plants grow.
the .refresh you can call anytime you like at 60 fps or each second.
usually physics simulations do the math thousands times a second but for the eye 30 to 60 fps is enough.
The old Canvas still has Invalidate and Refresh methods.
The new DesktopCanvas has Refresh with an optional Imediate as Boolean = False
parameter. Refresh() is the old invalidate. Refresh(True) is the old Refresh.