Discussion:
How to make a character shoot in a game.
ER
2016-08-23 03:45:47 UTC
Permalink
Hi,

For a game I've been trying to figure how to make it shoot but haven't
been able to wrap my mind around it

I want it were I press a button, i.e command button, and the weapon
should fly off in what ever
direction the character is facing. Also I would like it so that if
the button is held down it doesn't keep firing,
it needs to be pressed again.

Right now I have this
(Weapon is an instance of WeaponClass. )


UpdatePlayer(g as graphics)
.
.
.

if Keyboard.AsyncCommandKey then

AddWeapon

Weapon.shoot

g.DrawPicture(Weapon.GetImage(Weapon.MyDirection),Weapon.x,Weapon.y)

end if

AddWeapon()
Weapon = New WeaponClass

Weapon.images.append knife_up
Weapon.images.append knife_down
Weapon.images.append knife_left
Weapon.images.append knife_right

// set weapon x/y to hero's x/y
Weapon.x = HeroObject.x
Weapon.y = HeroObject.y

//set Weapon direction to whatever direction hero is facing.
Weapon.MyDirection = HeroObject.MyDirection



Weapon
shoot()
Select case MyDirection
case "left"
x = (x+12)
case "right"

X = (x -12)
case "up"
y = (y -12)
case "down"
y = (y + 12)
end Select



_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jim Wagner
2016-08-23 04:02:10 UTC
Permalink
How is the state “shoot” displayed to the user? Flash of light? Line of flight of a projectile? Sound?

Jim


James Wagner
Oregon Research Electronics
https://sites.google.com/site/oregonresearchelectronics/home
Hi,
For a game I've been trying to figure how to make it shoot but haven't been able to wrap my mind around it
I want it were I press a button, i.e command button, and the weapon should fly off in what ever
direction the character is facing. Also I would like it so that if the button is held down it doesn't keep firing,
it needs to be pressed again.
Right now I have this
(Weapon is an instance of WeaponClass. )
UpdatePlayer(g as graphics)
.
.
.
if Keyboard.AsyncCommandKey then
AddWeapon
Weapon.shoot
g.DrawPicture(Weapon.GetImage(Weapon.MyDirection),Weapon.x,Weapon.y)
end if
AddWeapon()
Weapon = New WeaponClass
Weapon.images.append knife_up
Weapon.images.append knife_down
Weapon.images.append knife_left
Weapon.images.append knife_right
// set weapon x/y to hero's x/y
Weapon.x = HeroObject.x
Weapon.y = HeroObject.y
//set Weapon direction to whatever direction hero is facing.
Weapon.MyDirection = HeroObject.MyDirection
Weapon
shoot()
Select case MyDirection
case "left"
x = (x+12)
case "right"
X = (x -12)
case "up"
y = (y -12)
case "down"
y = (y + 12)
end Select
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <listhe
ER
2016-08-23 04:57:52 UTC
Permalink
Right now its just an of a knife.

I'm kind of looking do something along the lines Gauntlet 2 for the
shooting. (valkyrie/elf)
Post by Jim Wagner
How is the state “shoot” displayed to the user? Flash of light? Line
of flight of a projectile? Sound?
Jim
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Trochoid
2016-08-24 07:13:40 UTC
Permalink
It looks like you're using Weapon.shoot to move it. But that's only getting
called and the image drawn when you actually fire.

Instead use AsyncCommandKey just to add the weapon, then separately, if
there is a weapon move and draw it.

if Keyboard.AsyncCommandKey then
AddWeapon
end

if Weapon <> nil then
Weapon.shoot
g.DrawPicture(Weapon.GetImage(Weapon.MyDirection),Weapon.x,Weapon.y)
end if

Also it might help to fold the drawing of the weapon into it's class, so it
looks like...

if Weapon <> nil then
Weapon.shoot
Weapon.draw(g)
end if


To keep the weapon from constantly firing you'll need to store and maintain
a 'countdown' property (or countup). Some value you increment each frame
and only fire the weapon if it's counted down to 0 (or up to some limit).
The best place to add this property and when to reset it depends on your
setup, but it might look like...


Property weaponReadyCountDown As integer


Sub UpdatePlayer(g as graphics)
...

if weaponReadyCountDown > 0 then
weaponReadyCountDown = weaponReadyCountDown - 1
end

if Keyboard.AsyncCommandKey and weaponReadyCountDown = 0 then
AddWeapon
end

...
end sub


Sub AddWeapon()

weaponReadyCountDown = 30 //number of frames until firing again

...
end sub


And elsewhere when the weapon has hit something and disappears you may want
to reset it

weaponReadyCountDown = 0
Post by ER
Right now its just an of a knife.
I'm kind of looking do something along the lines Gauntlet 2 for the
shooting. (valkyrie/elf)
How is the state “shoot” displayed to the user? Flash of light? Line of
Post by Jim Wagner
flight of a projectile? Sound?
Jim
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>
ER
2016-08-24 16:38:58 UTC
Permalink
Sweet Thanks!

Awesome its working!

Before I added a weapon countdown, I had what you suggested and if I
held the key down to fire the
weapon would stay with the character until I released the button.
Which is kind of neat.

I added a weapon count down and now if I hold the key down the weapon
fires off, a slight delay and fires again.

Both ways are pretty cool, not sure which one I'll go with, I like
them both. :-)


Ok when I fire a shot (or throw in this case) and fire again the
first or previous shot disappears.

So now I'm thinking, make a weapon array and append to it each time it
"shoots" and do all the shooting
stuff with the elements in the array (moving drawing etc..).

Or is there a better way to do that ?
Post by Trochoid
It looks like you're using Weapon.shoot to move it. But that's only getting
called and the image drawn when you actually fire.
Instead use AsyncCommandKey just to add the weapon, then separately, if
there is a weapon move and draw it.
if Keyboard.AsyncCommandKey then
AddWeapon
end
if Weapon <> nil then
Weapon.shoot
g.DrawPicture(Weapon.GetImage(Weapon.MyDirection),Weapon.x,Weapon.y)
end if
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>

Loading...