Error trying to create an extended type
BlitzMax Forums/BlitzMax Beginners Area/Error trying to create an extended type
| ||
| Greetings Puppies, I'm new to this oop stuff and need a little guidance, and couldn't find the answer elsewhere. Please see code below: Strict
Graphics 800,600,0
Type ParentType
Field id
Field x#,y#
Field xdir#, ydir#
Field name$
Function Create:ParentType(newname$)
Local temp:ParentType = New ParentType
temp.x = Rnd(800)
temp.y = Rnd(600)
temp.name$ = newname$
Local n
n = Rand(2)
If n=1 Then
temp.xdir = 1
Else
temp.xdir = -1
End If
n = Rand(2)
If n=1 Then
temp.ydir = 1
Else
temp.ydir = -1
End If
Return temp
End Function
Method Move()
x:+xdir
y:+ydir
If x<0 Or x>800 Then xdir:*-1
If y<0 Or y>600 Then ydir:*-1
End Method
End Type
Type YAxis Extends ParentType
Method Move()
y:+ydir
If y<0 Or y>600 Then ydir:*-1
End Method
End Type
Local y_axis:YAxis = YAxis.Create("Y Only : " + Rand(100))
Local myinstance:ParentType = ParentType.Create("X and Y : " + Rand(100))
While Not KeyDown(key_escape)
Cls
myinstance.move
DrawText myinstance.name, myinstance.x, myinstance.y
Flip
Wend
End
On compilation it tells me it is 'unable to convert from parenttype to Yaxis'. I understand in one respect, ie, because 'Create()' is trying to return an object of parenttype not YAxis, but I thought the point of inheritance was that it would allow this sort of behaviour. I can't even seem to cast it. Any suggestions? Peace, Jes |
| ||
you are trying to assign a ParentType instance to a YAxis type, you can either cast it as a YAxis, or make a new Create function for the child type:
Strict
Graphics 800,600,0
Type ParentType
Field id
Field x#,y#
Field xdir#, ydir#
Field name$
Function Create:ParentType(newname$)
Local temp:ParentType = New ParentType
temp.x = Rnd(800)
temp.y = Rnd(600)
temp.name$ = newname$
Local n
n = Rand(2)
If n=1 Then
temp.xdir = 1
Else
temp.xdir = -1
End If
n = Rand(2)
If n=1 Then
temp.ydir = 1
Else
temp.ydir = -1
End If
Return temp
End Function
Method Move()
x:+xdir
y:+ydir
If x<0 Or x>800 Then xdir:*-1
If y<0 Or y>600 Then ydir:*-1
End Method
End Type
Type YAxis Extends ParentType
Method Move()
y:+ydir
If y<0 Or y>600 Then ydir:*-1
End Method
Function Create:YAxis(newname$)
Return YAxis(Super.Create(newname$))
End Function
End Type
Local y_axis:YAxis = YAxis.Create("Y Only : " + Rand(100))
Local myinstance:ParentType = ParentType.Create("X and Y : " + Rand(100))
While Not KeyDown(key_escape)
Cls
myinstance.move
DrawText myinstance.name, myinstance.x, myinstance.y
Flip
Wend
End
|
| ||
| Greetings Puppies, Thanks, oh pert one :0) I was hoping to avoid the necessity of the second Create() command, but if that's the way, then that's the way. Thanks. Jes |
| ||
| np oh soggy one :) |
| ||
| Greetings Puppies, Hmmm, it doesn't seem to work. Please see updated code below. Strict
Graphics 800,600,0
Type ParentType
Field id
Field x#,y#
Field xdir#, ydir#
Field name$
Function Create:ParentType(newname$, i)
Print "Arrived at Create"
Local temp:ParentType = New ParentType
temp.id = i
temp.x = Rnd(800)
temp.y = Rnd(600)
temp.name$ = newname$
Local n
n = Rand(2)
If n=1 Then
temp.xdir = 1
Else
temp.xdir = -1
End If
n = Rand(2)
If n=1 Then
temp.ydir = 1
Else
temp.ydir = -1
End If
Return temp
End Function
Method Move()
x:+xdir
y:+ydir
If x<0 Or x>800 Then xdir:*-1
If y<0 Or y>600 Then ydir:*-1
End Method
Method Show()
SetAlpha(Float id/100)
DrawText name, x, y
End Method
End Type
Type YAxis Extends ParentType
Function Create:YAxis(newname$,i)
Local temp:YAxis
temp = YAxis(Super.Create(newname$,i))
If temp = Null Then
Print "Temp not created!!"
DebugStop
End If
Return temp
End Function
Method Move()
y:+ydir
If y<0 Or y>600 Then ydir:*-1
End Method
Method Show()
SetAlpha(Float id/100)
DrawText name, x, y
End Method
End Type
Local parents:TList
Local i
parents = CreateList()
Local myinstance:ParentType
Local y_axis:YAxis
For i = 1 To 100
myinstance:ParentType = ParentType.Create("X and Y : " + i, i)
parents.addlast(myinstance)
Next
For i = 1 To 100
y_axis:YAxis = YAxis.create("Y Only : " + i,i)
If y_axis = Null Then
Print "Y Axis Not Created!!"
DebugStop
End If
parents.addlast(y_axis)
Next
SetBlend(alphablend)
While Not KeyDown(key_escape)
Cls
For myinstance = EachIn parents
myinstance.move
myinstance.show
Next
For y_axis = EachIn parents
y_axis.move
y_axis.show
Next
FlushMem ; Flip
Wend
EndAny suggestions? Peace, Jes |
| ||
| First of all, you can't use "Super" from a static context - the compiler should have thrown an error! Secondly, I'm not sure you're allowed to downcast like that. Instead try something like this: |
| ||
| Greetings Puppies, Thanks Mikkel, I'd just done exactly that right down to the function called init :0) I just came back to update to say 'Problem Solved' Ta anyway, Peace, Jes |
| ||
| One thing not mentioned: You can assign YAxis to ParentType but not vice versa. Why? Because YAxis is an extended of Parent Type so it contains the full ParentType which can be assigned to a parentType Structure. But ParentType does not contain the full YAxis which is the reason this assignement is illegal. |
| ||
Using Soggy's original code and changing only the YAxis line...
Strict
Graphics 800,600,0
Type ParentType
Field id
Field x#,y#
Field xdir#, ydir#
Field name$
Function Create:ParentType(newname$)
Local temp:ParentType = New ParentType
temp.x = Rnd(800)
temp.y = Rnd(600)
temp.name$ = newname$
Local n
n = Rand(2)
If n=1 Then
temp.xdir = 1
Else
temp.xdir = -1
End If
n = Rand(2)
If n=1 Then
temp.ydir = 1
Else
temp.ydir = -1
End If
Return temp
End Function
Method Move()
x:+xdir
y:+ydir
If x<0 Or x>800 Then xdir:*-1
If y<0 Or y>600 Then ydir:*-1
End Method
End Type
Type YAxis Extends ParentType
Method Move()
y:+ydir
If y<0 Or y>600 Then ydir:*-1
End Method
End Type
Local y_axis:YAxis = YAxis (ParentType.Create("Y Only : " + Rand(100)))
Local myinstance:ParentType = ParentType.Create("X and Y : " + Rand(100))
While Not KeyDown(key_escape)
Cls
myinstance.move
DrawText myinstance.name, myinstance.x, myinstance.y
Flip
Wend
End
|
| ||
| Greetings Puppies, Excellent, ta James. That actually makes sense, doesn't it ;0) Peace, Jes |