Data Structures?
Posted: Tue Dec 20, 2011 5:22 pm
What kind of data structures do people use in Livecode? I've got a concept of how things might work, but wanted to run things by you all and see what comments came up. Note that I posted this here (in "Complete Beginners") on purpose. If you're coming along later and reading this, use a *large* grain of salt before believing what I type below.
Also, I *love* hearing "RTFM" if you'll just tell me which "FM" to read (extra points for links to relevant articles).
In building a game, I'd like to encapsulate a bunch of items together to form a sprite. (Things like the location, hot point, frame number, and a list of images that comprise the sprite, perhaps alpha, xAccel, yAccel, dxAccel, dyAccel, movement handler, and lots of other things).
It seems that true OOP isn't available, so as a substitute, I was going to make an array of items to be processed.
Each time through the event loop I can iterate once through this array, handling each item at a time.
Each item in the array is itself an array containing the elements I'd like to bundle:
type
loc
hotpointloc
curframe
alpha
xAccel
yAccel
handler
...
Loading each such "sprite" will be a pain I suppose:
put img "sprite_baddie1_fr1" into images[1]
put img "sprite_baddie1_fr2" into images[2]
put img "sprite_baddie1_fr3" into images[3]
put img "sprite_baddie1_fr4" into images[4]
put images into spriteframes["frames"]
put "baddie1" into item["type"]
put "1" into item["curframe"]
...
Finally, I put this object into the object array which is iterated over during frame draw time:
put item into objectArray[index]
set index = index+1
But looping to handle it should be "relatively" straightforward, yes?
So, to draw the next iteration of a sprite (in rough pseudo code at this point)
-- call movement handler for sprite at index 3
call objectArray[3]["handler"],3
-- check for collisions, and react (including changing this sprite into another type).
collide 3
-- do the drawing of the sprite, if still viable (draw the frames based on the sprite "type")
drawsprite 3
My thinking is that the index of the sprite upon which to act is sent to the different functions, which do the right thing.
Oh, and I was thinking that a sprites' type can be changed on the fly, so that the sprite could be "ship", with its frames, and if it gets hit by a bullet could be changed into a "fireball" type sprite, without having to load/change the velocity, movements, or anything.
Is there a better way to do this sort of thing in LiveCode? Am I over-engineering here?
Thanks,
-Ken
Also, I *love* hearing "RTFM" if you'll just tell me which "FM" to read (extra points for links to relevant articles).
In building a game, I'd like to encapsulate a bunch of items together to form a sprite. (Things like the location, hot point, frame number, and a list of images that comprise the sprite, perhaps alpha, xAccel, yAccel, dxAccel, dyAccel, movement handler, and lots of other things).
It seems that true OOP isn't available, so as a substitute, I was going to make an array of items to be processed.
Each time through the event loop I can iterate once through this array, handling each item at a time.
Each item in the array is itself an array containing the elements I'd like to bundle:
type
loc
hotpointloc
curframe
alpha
xAccel
yAccel
handler
...
Loading each such "sprite" will be a pain I suppose:
put img "sprite_baddie1_fr1" into images[1]
put img "sprite_baddie1_fr2" into images[2]
put img "sprite_baddie1_fr3" into images[3]
put img "sprite_baddie1_fr4" into images[4]
put images into spriteframes["frames"]
put "baddie1" into item["type"]
put "1" into item["curframe"]
...
Finally, I put this object into the object array which is iterated over during frame draw time:
put item into objectArray[index]
set index = index+1
But looping to handle it should be "relatively" straightforward, yes?
So, to draw the next iteration of a sprite (in rough pseudo code at this point)
-- call movement handler for sprite at index 3
call objectArray[3]["handler"],3
-- check for collisions, and react (including changing this sprite into another type).
collide 3
-- do the drawing of the sprite, if still viable (draw the frames based on the sprite "type")
drawsprite 3
My thinking is that the index of the sprite upon which to act is sent to the different functions, which do the right thing.
Oh, and I was thinking that a sprites' type can be changed on the fly, so that the sprite could be "ship", with its frames, and if it gets hit by a bullet could be changed into a "fireball" type sprite, without having to load/change the velocity, movements, or anything.
Is there a better way to do this sort of thing in LiveCode? Am I over-engineering here?
Thanks,
-Ken