Problem with my enemy
BlitzMax Forums/BlitzMax Beginners Area/Problem with my enemy
| ||
| Iīm currently trying to create an enemy for my game that is going to look like a flying saucer with a big gun under it, i create it like two objects, one body end one gun and then i add them both to a list for updating and rendering. My problem is: how do i get the gun object to follow along with the body object as long as the body is alive, when i kill the body the gun is going to freak out and have its own move and shoot methods but as long as the body is not dead it should be controlled (or whatever) by the body. And of cource if i could kill the gun first to make the body more or less defenseless. I donīt even know if itīs a good idea to make this enemy in two objects, maybe you have a better idea. And what happens when i have several ones of this type active at the same time, i guess the guns have to know wich body they belong to. Thanks for any help on this. |
| ||
I don't know if there is a better way, but maybe something like this:Type TEnemy Field Gun:TGun Field Body:Tbody '... Create Functions and stuff here Method Move(X:Int) If Body And Gun Then Body.x:+X Gun.X:+x ElseIf Body And Not Gun Body.x:+X ElseIf Not Body And Gun Gun.X:+1 EndIf End Method End Type Type TGun Field Life:Int Field x:Int Field Y:Int Field Image:TImage End Type Type TBody Field Life:Int Field X:Int Field Y:Int Field Image:TImage End Type |
| ||
I would do something like this:
type Tenemy
field x, y
method draw()
' ... and so on ...
endtype
type Tsaucer extends Tenemy
method update()
' Move the body
endmethod
endtype
type Tsaucer_gun extends Tenemy
field body:Tenemy
method update()
if body then
' follow body
x = body.x
y = body.y
else
' freak out
x = x - 10 + rand(0,20)
y = y - 3 + rand(0,6)
endif
endmethod
endtype
local ship:tsaucer = new tsaucer
local gun:tsaucer_gun = new tsaucer_gun
gun.body = ship
|
| ||
Type ASprite Abstract
Field xPos:Double , yPos:Double
Field image:TImage , frame:Int , maxFrame:Int
Method move( xSpeed:Double , ySpeed:Double )
xPos:+xSpeed
yPos:+ySpeed
EndMethod
Method draw()
DrawImage image , xPos, yPos , frame
frame:+1
frame:Mod maxFrame
EndMethod
'..whatever other methods you think are nessecary..
EndType
Type TFuselage Extends ISprite
Field myGun:ISprite
Method move( xSpeed:Double , ySpeed:Double )
Super.move( xSpeed:Double , ySpeed:Double )
myGun.move( xSpeed:Double , ySpeed:Double )
EndMethod
'..You get the idea..
EndType
Type TGun Extends ISprite
'..whatever fields you need for the gun
EndType
Local myShip:TFuselage = New TFuselage
myShip.image = LoadAnimImage("ufo.png" , 128 , 64 , 0 , 40)
myShip.maxFrame = 40
myShip.yPos = Rand( 0 , 400 )
myShip.myGun = New TGun
myShip.myGun.image = LoadAnimImage("gunpic.png" , 64 , 64 , 0 , 4)
myShip.myGun.maxFrame = 4
myShip.myGun.yPos = myShip.yPos + 64Or something to that effect. |