Object Arrays
Monkey Forums/Monkey Beginners/Object Arrays
| ||
| Hi, New to Monkey X I was playing around with the free one until I had to buy before I could ask questions. I don't know if this makes any difference but I'm still using the free one for now as I'm running out of time tonight to set it up. (is there not a code tag on this create topic screen?): Update: I found the tool tip after I posted :) I'm trying to create an object array but don't quite understand the syntax. I've been searching for quite a while and couldn't find a question to this problem: Here's my code in full
Import mojo
Function Main ()
New Game
End
Class SpawnAlien
Field x:Float
Field y:Float
Field sprite:Image
End
Class Game Extends App
Field backDrop:Image
Field player:Image
Field x:Float = 355
Field y:Float = 550
Field spawns:Int
Field alien:Object [6]
Method OnCreate ()
SetDeviceWindow (800, 600, 1)
backDrop = LoadImage ("space-intruders-bg.png")
player = LoadImage ("defender.png")
For spawns = 0 To 5
alien[spawns] = New SpawnAlien
alien[spawns].x = 50 * spawns
alien[spawns].y = 200
alien[spawns].sprite = LoadImage ("alien-type-1-1.png")
Next
SetUpdateRate 60
End
Method OnUpdate ()
If KeyDown (KEY_LEFT) And x > 0 Then x = x - 4
If KeyDown (KEY_RIGHT) And x < 708 Then x = x + 4
If KeyHit(KEY_ESCAPE) ' maps to the back button in Android
EndApp() ' or Error ""
End
End
Method OnRender ()
Cls 64, 96, 128
DrawImage backDrop, 0, 0
DrawImage player, x, y
For spawns = 0 To 5
DrawImage alien[spawns].sprite, alien[spawns].x, alien[spawns].y
End
End
End
The part I'm trying to get working is:
Field alien:Object [6]
'...
Method OnCreate ()
'...
For spawns = 0 To 5
alien[spawns] = New SpawnAlien
alien[spawns].x = 50 * spawns
alien[spawns].y = 200
alien[spawns].sprite = LoadImage ("alien-type-1-1.png")
Next
Method OnRender ()
'...
For spawns = 0 To 5
DrawImage alien[spawns].sprite, alien[spawns].x, alien[spawns].y
End
alien is an object array set to 6 elements which are loop instantiated using the call to the SpawnAlien class. The problem is that I get the following error upon compile: Error : Identifier 'x' not found. Can anyone tell me what I've missed with this alien[spawns].x Many thanks Dal1980 |
| ||
Object is a class just like your spawnAlien class. Object has been predefined by the language itself and is what every instance of a class you create is extended from. it's done automatic by the compiler. But the object class does not have any fields or components by itself. You can actually assign any class object created into it but you loose all usability of globals, fields, methods and functions from the original class. That is because the object class doesn't know or take possession of the components declared by the original class, in your case "SpawnAlien". to access components of the SpawnAlien class an instance of the SpawnAlien class must be created as an SpawnAlien not as an Object. After you create it and fill the components then you can assign it to the object class such as this:
For spawns = 0 To 5
Local an:SpawnAlien = New SpawnAlien '**********************
an.x = 50 * spawns
an.y = 200
an.sprite = LoadImage ("alien-type-1-1.png")
alien[spawns] = an '****************************
Next
the problem after assigning it to the Object class is that you can not access any components from the SpawnAlien class instance. to access the components you need to cast the object instance back to "SpawnAlien": For spawns = 0 To 5 Local an:SpawnAlien = SpawnAlien(alien[spawns]) '******************* DrawImage an.sprite, an.x, an.y End your code will work better if you do it like this:
Import mojo
Function Main ()
New Game
End
Class Alien '**************************
Field x:Float
Field y:Float
Field sprite:Image
End
Class Game Extends App
Field backDrop:Image
Field player:Image
Field x:Float = 355
Field y:Float = 550
Field spawns:Int
Field alien:Alien [6] '************************************
Method OnCreate ()
SetDeviceWindow (800, 600, 1)
backDrop = LoadImage ("space-intruders-bg.png")
player = LoadImage ("defender.png")
Local img:Image = LoadImage ("alien-type-1-1.png") '***********************
For spawns = 0 To 5
alien[spawns] = New Alien '*****************************
alien[spawns].x = 50 * spawns '********************
alien[spawns].y = 200 '***************
alien[spawns].sprite = img '**************
Next
SetUpdateRate 60
End
Method OnUpdate ()
If KeyDown (KEY_LEFT) And x > 0 Then x = x - 4
If KeyDown (KEY_RIGHT) And x < 708 Then x = x + 4
If KeyHit(KEY_ESCAPE) ' maps to the back button in Android
EndApp() ' or Error ""
End
End
Method OnRender ()
Cls 64, 96, 128
DrawImage backDrop, 0, 0
DrawImage player, x, y
For spawns = 0 To 5
DrawImage alien[spawns].sprite, alien[spawns].x, alien[spawns].y '**************
End
End
End
|
| ||
| Thanks Jesse It's a bit hard to read on my phone but looking forward to tonight when I can try your suggestion Many thanks for your reply, I appreciate it Cheers Dal |
| ||
Ah, so just changedField alien:object [6] to Field alien:SpawnAlien [6] Working :) ty |