Having trouble with a cannon firing...

Hi. I’m eleven years old and a novice at Corona. This may be relevant to the current topic (having balls shoot in the correct direction).

I created a cannonBall and a cannon and made them a single group and put them in the same place. I created a working method of rotating the cannon via touch:

local function rotateTo (event)
local deg = math.atan2( event.y - cannonGroup.y, event.x - cannonGroup.x )
cannonGroup.rotation = math.deg(deg)
end

However, after creating a shootBall function (using the code given kindly by indiegamepod) and applying it and the rotateTo function to a turnAndShoot function:

local function turnAndShoot (event)
if ( event.phase == “began” ) then
timer.performWithDelay( 1, rotateTo, 0 )
end
if ( event.phase == “ended” ) then
timer.performWithDelay( 1, shootBall )
end
end

the cannon will rotate with the touch perfectly, but the shootBall function will not call. Why is this? [import]uid: 82408 topic_id: 15607 reply_id: 315607[/import]

you need to use the phase “moved” as well to get events from when the touch is moved [import]uid: 5354 topic_id: 15607 reply_id: 57642[/import]

it looks like you have it set to shoot the ball when the touch ends now [import]uid: 5354 topic_id: 15607 reply_id: 57649[/import]

I changed the code again:

[code]
local function turnAndFire (event)
local deg = math.atan2( event.y - cannonGroup.y, event.x - cannonGroup.x )
cannonGroup.rotation = math.deg(deg) + 90
– The +90 is because the rotation was originally built for an image with the cannon pointing
– right, not an image pointing upwards
if ( event.phase == “ended” ) then
cannonBall:setLinearVelocity(math.sin(math.rad(cannon.rotation))*200, math.cos(cannon.rotation))*-200)
end
end [import]uid: 82408 topic_id: 15607 reply_id: 57647[/import]

email me the code, images etc… so I can see it run support@coronaremote.com [import]uid: 5354 topic_id: 15607 reply_id: 57658[/import]

Oh by the way, I finished it, and am adding improvements to it. The cannon fires and rotates perfectly, and the cannon ball can interact with the other objects. Glad I figured this out. Cya :wink: [import]uid: 82408 topic_id: 15607 reply_id: 57791[/import]

Hello,

I’m new to Corona SDK and LUA and I could use some help, I can’t solve this problem for a couple of days. I want to code this:

  1. one stick to be rotating permanently at certain speed - I figured that out

  2. and to fire some bullet on tap event - I figured that out to

This is the code:

[lua]centerX = display.contentWidth * .5
centerY = display.contentHeight * .5

– Physics
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )

local rotationSpeed = 5
local stick
local bullet
local halfStick

local bg = display.newImageRect(“images/bg.png”, 570, 360)
bg.x = centerX
bg.y = centerY

stick = display.newImageRect ( “images/stick.png”, 25, 150 )
stick.x = centerX
stick.y = centerY
stick:setReferencePoint(display.CenterReferencePoint);

local stickkRotation = function ()
stick.rotation = stick.rotation + rotationSpeed
end

local function shootBullets(event)
bullet = display.newImageRect (“images/bullet.png”, 25, 27)
bullet.x = stick.x
bullet.y = stick.y
bullet.rotation = stick.rotation
physics.addBody(listic, “dynamic”, {density=1.0, friction=0.5, bounce=0.1})
local speed = 300
local velocity = {}
velocity.x = math.cos( math.rad( bullet.rotation - 90)) * speed
velocity.y = math.sin( math.rad( bullet.rotation - 90)) * speed
bullet:applyForce(velocity.x, velocity.y, stick.x, stick.y)

end

–Listeners
Runtime:addEventListener(“enterFrame”, stickRotation)
Runtime:addEventListener(“tap”, shootBullets);

[/lua]

The problem is that I don’t know how to set bullet to start from top of the stick (now it starts moving from center of the stick). I really don’t know how to solve that and I can’t lose any more time on that. So, I need a formula which calculates position of top of the stick and shoots bullet from there in the direction in which is the stick.

Could someone please help me with this?

Hello,

I’m new to Corona SDK and LUA and I could use some help, I can’t solve this problem for a couple of days. I want to code this:

  1. one stick to be rotating permanently at certain speed - I figured that out

  2. and to fire some bullet on tap event - I figured that out to

This is the code:

[lua]centerX = display.contentWidth * .5
centerY = display.contentHeight * .5

– Physics
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )

local rotationSpeed = 5
local stick
local bullet
local halfStick

local bg = display.newImageRect(“images/bg.png”, 570, 360)
bg.x = centerX
bg.y = centerY

stick = display.newImageRect ( “images/stick.png”, 25, 150 )
stick.x = centerX
stick.y = centerY
stick:setReferencePoint(display.CenterReferencePoint);

local stickkRotation = function ()
stick.rotation = stick.rotation + rotationSpeed
end

local function shootBullets(event)
bullet = display.newImageRect (“images/bullet.png”, 25, 27)
bullet.x = stick.x
bullet.y = stick.y
bullet.rotation = stick.rotation
physics.addBody(listic, “dynamic”, {density=1.0, friction=0.5, bounce=0.1})
local speed = 300
local velocity = {}
velocity.x = math.cos( math.rad( bullet.rotation - 90)) * speed
velocity.y = math.sin( math.rad( bullet.rotation - 90)) * speed
bullet:applyForce(velocity.x, velocity.y, stick.x, stick.y)

end

–Listeners
Runtime:addEventListener(“enterFrame”, stickRotation)
Runtime:addEventListener(“tap”, shootBullets);

[/lua]

The problem is that I don’t know how to set bullet to start from top of the stick (now it starts moving from center of the stick). I really don’t know how to solve that and I can’t lose any more time on that. So, I need a formula which calculates position of top of the stick and shoots bullet from there in the direction in which is the stick.

Could someone please help me with this?