BMax does not support constructors with arguments (like C++ and all other OOP languages do) and though you have to implement workarounds by implementing constructors as a factory/create function for each type and find a way to share the init code of parenttypes using another function and remember for each new derived type to chain the init call.
So what I'm trying to do ISN'T crazy then.
But what I don't get is there's not just this one constructor which is an issue because of the type it returns. Other sprite functions TAKE sprites as parameters. For example: ThisSprite.Distance#(OtherSprite)
And still others use a Sprite() tpyecast within then to convert the value stored in the linked list of sprites to a sprite.
If I can't extend this sprite type because of these issues, then either there must be a way to work around it that is simple, or this whole extend thing is retarded.
[quite]All fruits are sprites. This does not mean that all sprites are fruits.[/quote]
That's true. So I should be able to do ThisFruit.Distance#(OtherFruit) even though the function takes a sprite as a parameter, right?
I asked for a sprite, and you give me a fruit, which is a type of sprite.
That doesn't explain why Fruit = Sprite doesn't work though. Conceptually it's a bit hard to wrap your head around, but programmatically, it would just take all the properties of sprite and stick them in the fruit, which makes perfect sense because fruit has all the necessessary variables being an extension of sprite.
On the other hand, the logical inverse of that is Sprite = Fruit. And that clearly will not work from a programming point of view, at least as far as not losing a bunch of data in the process.
ONE of these should logically exist, since "a fruit is a sprite", and since the first one makes the most sense, and doesn't result in a loss of data, that seems like the logical choice.
The only problem with Fruit = Fruit.Create() that I can see is that in the function it says New Sprite not New Fruit. I can't really see a way around that issue. It's not creating a new Fruit, so even if Fruit contained a link to the fields that were part of the sprite type now your fruit type would only have half the memory allocated that it needs.
I can accept that that doesn't work, but what I need to know is what will happen if I do this:
' -------------------------------------------------------------------------------------------------------------------------------------------------------
' This function creates a new sprite, and returns a pointer to it.
'
' Image : The image the sprite will use. May be null, if, for example, you wish to simply create an invisible parent for other sprites.
' Order : The order the sprite will be drawn in. Order can be positive or negative. Sprites with a lower order are drawn first.
' If two sprites have the same order, they will be drawn in the order in which they were created.
' Parent : The parent of this sprite. A sprite does not need to have a parent, but if it does, it will be positioned relative to it.
'
' The sprite's blending mode, scale, color, alpha, and viewport will be set according to the current values you have set with the Max2D functions.
' This is so that you can set the blending mode once for example, and all sprites you create from then on will use the direcred blending mode.
'
' Example:
'
' ThisSprite:Sprite = Sprite.Create(Image)
' -------------------------------------------------------------------------------------------------------------------------------------------------------
Function Create:Sprite(Image:TImage=Null, Order%=0, Parent:Sprite=Null)
Local NS:Sprite
NS = New Sprite
NS.SetParent(Parent)
NS.SetImage(Image)
NS.SetOrder(Order)
If Image <> Null Then NS.SetRadius(Min(ImageWidth(Image), ImageHeight(Image))/2)
Return NS
End Function
' -------------------------------------------------------------------------------------------------------------------------------------------------------
' This method is called when you create a sprite via SpriteName = New Sprite.
' It allows you to create types that extend the sprite type. Ie: Type Fruit Extends Sprite.
'
' If you create a sprite using New Sprite, then you don't need to call the Create() function. The sprite will however default to having no image, an
' order of 0, no parent, and a radius of 0. You will need to at least set the sprite's image after creating it if you want it to be seen.
' -------------------------------------------------------------------------------------------------------------------------------------------------------
Method New()
Local ScaleX#, ScaleY#
Local R%, G%, B%
Local VPx%, VPy%, VPw%, VPh%
' Add sprite to the end of the sprite list, and save a pointer to the link created.
Self._Link = SpriteList.AddLast(Self)
' Config sprite.
Self.SetBlend(.GetBlend())
.GetScale(ScaleX#, ScaleY#)
Self.SetScale(ScaleX#, ScaleY#)
.GetColor(R, G, B)
Self.SetColor(R, G, B)
Self.SetAlpha(.GetAlpha())
.GetViewport(VPx, VPy, VPw, VPh)
Self.SetViewport(VPx, VPy, VPw, VPh)
Self.SetMass(1)
Self.SetElasticity(1)
End Method
Now in theory with this setup, I should be able to go: Fruit = New Fruit and get a fruit right? The New method in my sprite type will be called, won't it?
But what happens with that list I have there? All the rest of my sprite functions like Sprite.Draw() assume that all of those objects in that list are sprites.
So I do: For ThisSprite = EachIn SpriteList
Will that work when the object that was inserted into the list was actaully a fruit, which is technically also a sprite, but it's type is fruit, and ThisSprite is of type Sprite?
I'm so confused.
And yes, it would be nice to be able to be able to avoid these Create functions by being able to pass parameters with New.