How to get an object to fire in a certain direction.

Hi guys, newbie here. I’m trying to find a platform to move to, and I came across corona – seems pretty awesome and I already started “falling for it” but, I am finding it rather difficult to execute firing from my “player” object in the direction it is flying… sort of like asteroids ship that rotates, and flies around trying to fire. Would someone be able to direct me to;

  1. make a ship fire using a button code?

  2. auto-fire toggle on/off code?

Can anyone help me with the above? I appreciate it.

Thanks,
Peter [import]uid: 10379 topic_id: 3059 reply_id: 303059[/import]

Hi Peter,

Not sure there is any pre-written code for this, but here are some tips:

  1. Buttons are easy. There is an example of this in the “Corona in 5 Minutes” document, found on this site’s Resources>Docs page. The button is associated with a “listener” which simply “hears” when it is pressed. If yes, some code is exectued, i.e. firing bullets.

  2. Auto-fire toggle would be accomplished with a simple boolean variable. Just change it upon toggle (toggle happens with some other button I assume).

  3. Any object’s rotation is determined simply by “object.rotation”. You can use that to set or read it’s current rotation, and then assign that to the bullets. Corona doesn’t have a built-in angular force function yet, so you’ll have to use basic math and trigonometry to determine the “X” and “Y” force values of the bullets according to the ship angle. This website is helpful for that: http://www.csgnetwork.com/righttricalc.html. With the proper math, you’d then use Corona’s body:setLinearVelocity function to apply linear force to the bullets.

Hope this helps! Corona can seem confusing at first, especially if you’re coming from a certain “unspeakable” platform. :slight_smile: Trust me, it gets much easier within a week, and the power you’ll receive for your learning efforts will blow your mind.

Brent
[import]uid: 9747 topic_id: 3059 reply_id: 8932[/import]

Brent,

Thanks for the reply. Yup, confused at first, but it is starting to make more sense by the minute… the “unspeakable” platform shall remain nameless :wink:

Peter [import]uid: 10379 topic_id: 3059 reply_id: 8935[/import]