Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / How to make something fire...

Author
Message
Bankzy10
15
Years of Service
User Offline
Joined: 29th Jan 2009
Location: Planet DarkBasic - Learning the tricks
Posted: 26th Apr 2009 23:38
Right, I have a spaceship and I would like to make it fire but what I don't understand is how to get it to shoot from the same place while the place it is shooting from constantly moves. Basically you control the spaceship, and you fire using the space bar. My question is how do you make the projectile fire from the same place - the spaceship even though it moves via the users inputted directions using the arrow keys?



Theres just the game part of the code - as you can see I am fairly new to programming. If someone can help it would be muchly appreciated.

Thanks, Bankzy =]
JLMoondog
Moderator
15
Years of Service
User Offline
Joined: 18th Jan 2009
Location: Paradox
Posted: 27th Apr 2009 09:57 Edited at: 27th Apr 2009 10:00
When you create the bullet put this in:

Edit: Blah, not paying attention. You can replace the Object Position with Sprite Position. And I believe there is a Sprite angle as well. You just need to place your bullet at the ship location and angle, then update it.

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 27th Apr 2009 16:53
It can be tricky to offset bullet and effect locations when rotating sprites, at least when your not looking for a direct mid point that is.

The newx and newyvalue commands can help a great deal - I would do 2 passes to get the location after an X and Y offets.

Say for example your enemy is an X-wing figher with 2 lazers at each side on the wing, calculating this bullet spawn position can be very tricky. So, if you take Xoff and Yoff as offset variables, working out the offsets you want on an non-rotated sprite is the first step. If the gun turrets on your X-wing are at 32 on X from the centre and 16 on Y from the centre, then you can work the resulting location like this, assuming ang# is the rotation angle of your X wing:

XP#=newxvalue(XP#,ang#,Yoff#)
YP#=newyvalue(YP#,ang#,Yoff#)
XP#=newxvalue(XP#,ang#+90.0,Xoff#)
YP#=newyvalue(YP#,ang#+90.0,Xoff#)

See it uses a double pass of new value, so that it works out the Y offset position first, then the X offset position, which is a case of working out the position at a 90 degree angle on the current rotation.

So if you keep note of the bullet start locations (hot spots these are often referred to) in your art package, you just need to feed these offsets in, with the sprite centre being taken into account of course.

Just thought I'd post that in case you do need to work out rotated stuff like that. The code there is untested but probably quite close to what you'd expect it to do.


Health, Ammo, and bacon and eggs!

Login to post a reply

Server time is: 2024-04-24 21:32:01
Your offset time is: 2024-04-24 21:32:01