two projectiles

BlitzMax Forums/BlitzMax Beginners Area/two projectiles

hey everyone. Currently I have a ship able to fire one projectile at a time. Which is drawn, updated, deleted when neccessary and collision is checked for..

what I'd like to try is have two instances in the same projectile fire when you fire. So say the ship has missile launchers on the left wing and the right wing... when I fire i want two separate and distinct objects to come out. The problem is that so far how ive been trying it, ive been duplicating the previous code, except this time referencing the second instance, it just seems unneccessary and i was wonder what would be a better way to go about it.

so far when you fire, missle:tmissle = new tmissle is created. I went about putting in missle2:tmissle = new t missle would that be the right way to go about it?

no

What you would do would place all created missiles in a missile list and update all missiles on that list in the "update projectile" function you have.

currently i place all in a list. but if i only have one missle object instanced when I fire, i only have one missle.x and missle.y to work with, so i cant affect the launch location of the first missile, without affecting, the position of the second one... without a separate instance

in my update projectile function i am using

for missile = eachin tmissile.list

where the missile object is the actual reference to my missile object right now.

and the update function updates that specific missle, draws it moves it, by refering to each field that I need to like this, missle.x, missle.y, missle,frame, missile.age

etcetc... maybe there is a way to let it update each missles fields in that list, but its going over my head.. LOL

currently i place all in a list. but if i only have one missle object instanced when I fire, i only have one missle.x and missle.y to work with

Don't create with new then. Add a Create() function to your missile type, which takes the x and y coordinates as parameters and creates a new missile and adds it to the list. It's much neater that way.

in my update projectile function i am using

for missile = eachin tmissile.list

where the missile object is the actual reference to my missile object right now.

and the update function updates that specific missle, draws it moves it, by refering to each field that I need to like this, missle.x, missle.y, missle,frame, missile.age

etcetc... maybe there is a way to let it update each missles fields in that list, but its going over my head.. LOL

so how would I go about updating the new missiles values as stated above.. gotta change the update function some how, so it only doesnt refer to the first missile object only, but all the objects.

What?

for missile = eachin tmissile.list


where the missile object is the actual reference to my missile object right now.

No, that goes through every missile in the list. If it's not doing every missile, you're not putting them all in the list.

so how would I go about updating the new missiles values as stated above.. gotta change the update function some how, so it only doesnt refer to the first missile object only, but all the objects.

It *isn't* just doing the first missile object. That's why it's called EachIn and not FirstIn.

what if the update function is only refering to the first missiles fields?

For missile = EachIn missile.List

missile.age :+ 1
			
			If missile.age > 150 
				PlaySound(missileExplosionFx:TSound)
				RemoveLink missile.link
			End If
						
			SetRotation missile.angle
			SetBlend ALPHABLEND
			SetAlpha 1.0
			SetColor(255,255,255)
			MidHandleImage (missile.Image)
			SetScale 2,2
			DrawImage(missile.Image,missile.x,missile.y,missile.frame)
			missile.frame:+ 1
	
			If missile.frame = 30 Then missile.frame = 0
						
			missile.vx:+ Sin(missile.angle) * missile.speed
			missile.vy:+ Cos(missile.angle) * missile.speed
			
			missile.x :+ missile.vx 
			missile.y :- missile.vy 


Small Sample:

Type TCannon
	Global Missiles:TList = CreateList()
	
	Field Parent:TShip
	Field RelX:Int
	Field RelY:Int
	
	Function Create:TCannon(Parent:TShip , RelX:Int , RelY:Int)
		Local C:TCannon = New TCannon
		C.Parent = Parent
		C.RelX = RelX
		C.RelY = RelY
		Parent.AddCannon(C)
		Return C
	End Function
	
	Method Draw()
		SetColor 255,0,0
		DrawRect (Parent.X-5)-RelX, (Parent.Y-5) , 10 , 10
		SetColor 255 , 255 , 255
		
		
	End Method
	
	Method Shoot()
		Local M:TMissile = New TMissile
		M.X = (Parent.X - 5) - RelX
		M.Y = (Parent.Y - 5)
		Missiles.Addlast(M)
	End Method
		
End Type

Type TMissile
	Field X:Int
	Field Y:Float
End Type

Type TShip
	
	Field Cannons:Tlist = CreateList()
	Field X:Int
	Field Y:Int
	
	Function Create:TShip(X:Int , Y:Int)
		Local S:TShip = New Tship
		S.X = X
		S.Y = Y
		Return S
	End Function
	
	Method AddCannon(C:TCannon)
		Cannons.Addlast(C)
	End Method
	
	Method Draw()
		SetColor 0 , 255 , 0
		DrawRect x-5 , y-5 , 20 , 20
		SetColor 255 , 255 , 255
		For Local C:TCannon = EachIn Cannons
			C.Draw()
		Next
		
		DrawOval x,y,10,10

For Local M:TMissile = EachIn TCannon.Missiles
			M.Y:- .2
			DrawOval M.X , M.Y , 3 , 3
			If M.Y < 0 Then TCannon.Missiles.Remove(M)
		Next

	End Method
	
	Method Shoot()
		For Local C:TCannon = EachIn Cannons
			C.Shoot()
		Next
	End Method	
End Type

Graphics 800 , 600 , 0 , - 1

Local Ship:TShip = TShip.Create(400 , 300)
TCannon.Create(Ship , 10 , 10)
TCannon.Create(Ship , - 20 , 10)


While Not KeyHit(Key_Escape)
	Cls
	
	Ship.X = MouseX()
	Ship.Y = MouseY()
	If MouseHit(1) Then Ship.Shoot()
	Ship.Draw()
	Flip
Wend


Maybe it helps.

very nice klepto!