Page 1 of 1

Making a gun work

Posted: Fri Mar 04, 2016 1:32 pm
by antn18
I am currently creating a game as part of a sixth form task. I have the basic functions like movements and collisions completed but my game has a requirement for a gun. I have images of the gun and its bullet but i need a way to make the bullet be fired towards the enemy. The cross hair already follows whereever the mouse goes and so i want to be to able to shoot the enemies using that.

Thanks, Ant

Re: Making a gun work

Posted: Fri Mar 04, 2016 3:56 pm
by Martin Koob
Hi I have not made games so not sure if this is the best way to do the move, I am sure there are more smooth ways to animate.

Anyway here is one way to approach it.

If you had three images "gun", "bullet" and "target1" you could use the following shootTarget handler with two parameters, pTarget and pTime

With this you could have one gun shoot several different targets by creating images "target2", "target3" etc.

pTarget is the name of the target image you are aiming at and pTime is the time in 60th of a second that it takes to travel. One problem with this is that bullets going right across the screen would move faster than bullets travelling shorter distances. To have bullets travel at the same speed you would have to do some math using the distance and the time.

If you wanted to shoot from more than one object say you had different ships firing at other ships you would need a third parameter pGun that would be substituted for the image "gun" in the handler.

Code: Select all

on mouseup
   shootTarget "target1", 10
end mouseUp

on shootTarget pTarget, pTime
   show image "bullet"
   move image "bullet" from the loc of Image "gun" to the loc of image pTarget in pTime ticks
   hide image "bullet"
end shootTarget
Again I have never made a game or used the game loop so not sure if this is the best way to handle it but for a simple game it may do.

Hope that helps.

Martin

Re: Making a gun work

Posted: Tue Mar 08, 2016 3:31 pm
by antn18
This worked great thank you sir,

Ant