draw method in parent type
BlitzMax Forums/BlitzMax Programming/draw method in parent type
| ||
I have a parent type called spaceobject there are 2 types that extend from it: ship and asteroid if I have a global image in ship-type and asteroid-type how do I draw them from the parent type 'spaceobject' it says 'not found' when I try to do it. it only works if I put the image in a field, but that means loading a new image for each type instance |
| ||
Make your draw function abstract in the space object, and the asteroid and ship types have their own draw functions that draw their object. Or, put the two graphics in the spaceobject type. |
| ||
Being globals, them can be drawed 'intance-free'. So you could do something like: DrawImage(TMyType.MyGlobalImage,x,y) from anywhere, including the spaceobject class. |
| ||
what youre looking for isType Tobj Field x,y Method draw() Plot(x,y) EndMethod EndType Type Tship Extends Tobj Field img:Timage=yourGlobalShipImage Method draw() DrawImage(img,x,y) EndMethod EndType Type Trock Extends Tobj Field img:Timage=yourGlobalRockImage Method draw() DrawImage(img,x,y) EndMethod EndType Global myObjs:Tlist=New Tlist While Not AppTerminate() For Local o:Tobj=EachIn myObjs o.draw() Next Wend --edit to add: you put your Tship and Trocks in the list and it will auto use the right draw() in case that wasnt obvious |
| ||
it only works if I put the image in a field, but that means loading a new image for each type instance Whats wrong with reusing the same reference to the image? |
| ||
Are you saying that if you load the same image over and over, it is actually only loaded once and every time after that only gets a reference to the one loaded image? Is this true, confirmed? |
| ||
No it's not. He's saying just load the image once and have more than one reference pointing to the same image. What actually happens when you re-declare a field in an extended type which exists in the base type? I would usually avoid this type of thing. |
| ||
What actually happens when you re-declare a field in an extended type which exists in the base type? I would usually avoid this type of thing. From other topics people have said the new field exists along with the old one, and can be confusing as to which one you're accessing. |
| ||
I see, that is how I normally do it. Thanks. |
| ||
What actually happens when you re-declare a field in an extended type which exists in the base type? I would usually avoid this type of thing. My code will work but you are right the semantics are wacky and not the clearest way to handle it. Having an img field in Tobj makes a lot more sense. Set that img to whatever in a constructor is a more proper way to do it. |
| ||
I would usually avoid this type of thing. There is no reason to avoid this kind of thing. It is a powerful tool called polymorphism. http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming The only place I would avoid it is if youre using multiple inheritance which BMax does not support. multiple inheritance + polymorphism = oo spaghetti code. |
| ||
There is no reason to avoid this kind of thing. It is a powerful tool called polymorphism. Sure there is. Blitzmax doesn't support re-declaring a field of the base type, it creates a new field instead. |
| ||
why is that bad? isn't that how i can call Super.whatever() and it works? |
| ||
No, thats a method. A field is like a variable in a type. |
| ||
wasn't that what we were talking about ? draw method in parent type in my code it's the draw() method that's polyed |
| ||
The person you quoted asked what would happen if a field gets re-declared, followed by what you quoted I would usually avoid this type of thing. I took it they were refering to re-declaring a field. |