inheritance problem

BlitzMax Forums/BlitzMax Beginners Area/inheritance problem

I'm having a little problem with inheritance. The way i see it, my enemy2 type over rides the updatePosition method, but when i have an object of type enemy and enemy2 there is some strange behavior. Can you see what i'm doing wrong

Cheers
Charlie


Strict

Import "test2.bmx"

Type enemy
	
	Global enemyX:Float
	Global enemyY:Float
	Global enemyDirection:Float
	Global enemyAngle:Float
	Global enemyRadius:Float
	Global enemyVelocity:Float
	
	Method getX:Float()
		Return enemyX
	End Method
	
	Method setX(inX:Float)
		enemyX=inX
	End Method
	
	Method getY:Float()
		Return enemyY
	End Method     
	
	Method setY:Float(inY:Float)
		enemyY=inY
	End Method
	
	Method getDirection:Float()
		Return enemyDirection
	End Method
	
	Method getAngle:Float()
		Return enemyAngle
	End Method  
	
	Method setDirection(inDirection:Float)   
	    enemyDirection=inDirection
	End Method
	
	Method getRadius:Float()
		Return enemyRadius
	End Method
	
	Method getVelocity:Float()
		Return enemyVelocity
	End Method
	
	Method setAngle(inX:Float,inY:Float)
		enemyAngle=calculateAngle(enemyX,enemyY,inX,inY)
	End Method
	
	Method setVelocity(inVel:Float)
		enemyVelocity=inVel
	End Method
	  
	Method updatePosition(inX:Float,inY:Float) 
		enemyDirection=calculateAngle(enemyX,enemyY,inX,inY)
		enemyRadius=enemyVelocity
		enemyX=enemyRadius*Cos(enemyDirection)+enemyX
		enemyY=enemyRadius*Sin(enemyDirection)+enemyY
	End Method    
	
	Method setup(inX:Float, inY:Float, inVelocity:Float, inRadius:Float)
		enemyX=inX
		enemyY=inY
	    enemyVelocity=inVelocity
	    enemyRadius=inRadius
	End Method
	
	Method paint()
		SetColor(255,255,255)
		DrawRect(enemyX,enemyY,5,5)
	End Method
	
	Function Create:enemy()
		Return New enemy
	End Function
	
	
End Type
  

Type enemy2 Extends enemy
	
	Method updatePosition(inX:Float,inY:Float) 
		enemyDirection:+enemyVelocity
		enemyX=enemyRadius*Cos(enemyDirection)+inX
		enemyY=enemyRadius*Sin(enemyDirection)+inY
	End Method
		
End Type 

Graphics 640,480,0

Local en:enemy=New enemy
Local en2:enemy2=New enemy2 

Local i:Int 

en.setup(100.0,100.0,1.1,100.0)
en2.setup(100.0,100.0,5,100.0) 

For i=0 To 100 

	'en.updatePosition(MouseX(),MouseY())
	'en.paint()
	en2.updatePosition(320,240) 
	en2.paint()
	
	Flip
	Cls
	
Next

Function calculateAngle(x1:Float, y1:Float, x2:Float, y2:Float)
	
	Local x:Float=0
	Local y:Float=0
	Local angle:Float=0
	
	x=x1-x2
	y=y1-y2
	
	angle=-ATan2(x,y )-90
	
	Return angle
	
End Function
 


rename global to field.

Otherwise all instances of enemy and enemy2 only have 1 single version of each of those.

ok, thanks. i was using global because i didn't want to be able to access the attributes outside of the type. can i still do this with field? i can't seem to make them private

Cheers
Charlie

you can't make fields and methods private/public as far as I know, I'd like to be wrong on this.

Nope, sadly you are right GA.

all information are public. If you don't want to make them public, then check the faq section, the first thread in there shows how to do it.

BUT: This will it make impossible to extend your types ... (as the implementation is the private part and thus can't be extended)

thanks Dreamora. It would be useful to have private just so users of my Types don't try calling internal methods...

You can try renaming all your private stuff with an underscore at the beginning. Works as a good reminder.

Thanks, I guess the same could be applied to methods too.

That or simply not documenting it. There is no IDE so far that is able to parse the .i files to show undocumented stuff on a precompiled library. (and even if, you could still remove it from there, if it is not used from outside anyway)

I haven't got as far as documentation ... :-/