N00b question #17 - array of various types?
Monkey Forums/Monkey Programming/N00b question #17 - array of various types?
| ||
Hey guys, Not sure how to do this, but I'm building a list of game assets, but instead of having multiple variable names like: Field bobX:Int, bobY:Int, bobSpeed:Float, bobImage:Image etc. I was hoping to do it in a tidier way by using an array. My idea was this: (in the on create): Field levelAssets:Image[][] (this currently gives me an 'expecting class member declaration error) Then you would have something like this in a function/method that is called when the level loads: levelAssets[0][0] = 200 ' x levelAssets[0][1] = 50 ' y levelAssets[0][2] = 1.0 ' speed levelAssets[0][3] = spritesheet.GrabImage(0, 0, 200, 200) ' image ... etc. The reason I wanted to do that is so I can use a for-next loop to iterate through the array and add the item to an assets list type: for local i:int = 0 to levelAssets.length addAsset(levelAssets[i][0], levelAssets[i][1], levelAssets[i][2], levelAssets[i][3]) next addAsset points to a method as below: Method addAsset:Void(x:Float, y:Float, speed:Float, img:Image) Local ass:Ship = New Ship ass.x = x ass.y = y ass.speed = speed ass.img = img assets.AddLast ass End How does one go about making an array of various types like the above? Or is there a better way to do it.. |
| ||
i don't see how that's tidier, but nonetheless, i don't think you can mix your multi-dimensional arrays. the easiest way IMO is: Class Ship field x:int, y:int, speed:float field img:Image Method New(x%,y%,speed#,img:Image) self.x=x; self.y=y; self.speed=speed; self.img=img end end Local levelAssets:Ship[] = New Ship[100] ''for 100 ships levelAssets[0] = New Ship(200,50,1.0,spritesheet.GrabImage(0, 0, 200, 200)) assets.AddLast levelAssets[0] |
| ||
Oh that's great Adam, many thanks. I've already done a similar class to yours above but wasn't sure of how to create arrays of it. It now makes more sense! |