Making a gun work

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
antn18
Posts: 10
Joined: Fri Feb 26, 2016 6:48 pm

Making a gun work

Post by antn18 » Fri Mar 04, 2016 1:32 pm

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

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Making a gun work

Post by Martin Koob » Fri Mar 04, 2016 3:56 pm

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

antn18
Posts: 10
Joined: Fri Feb 26, 2016 6:48 pm

Re: Making a gun work

Post by antn18 » Tue Mar 08, 2016 3:31 pm

This worked great thank you sir,

Ant

Post Reply

Return to “Games”