Help with shooting cannonballs

I am coding some turrets that will shoot every couple seconds. The turrets show up and the cannonballs shoot but every time they shoot it is from the top left corner of the screen, like it doesn’t recognize the turrets. here is the code i have for the turrets. Any help would be greatly appreciated. Thank you

module(…, package.seeall)

local spawnedTurrets = 0
local turretSpawnNum = 2
local shootTime = 0
sTurretsFiring = false
local bulletNum = 1
local turretBullet = {}
local dir = 0
local turretX = {}
local turretY = {}
local turret = {}
local dir = {}
local turretDir = {}
local debudDir = true
local buletsFired = false
local boomText = display.newText("", 0,0, native.systemFont, 72)
require “sprite”
local sheet1 = sprite.newSpriteSheet(“TurretFire.png”,256,164)
local turretSet1 = sprite.newSpriteSet(sheet1,1,4)
local sheet2 = sprite.newSpriteSheet(“cannonball.png”,200,200)
local cannonSet1 = sprite.newSpriteSet(sheet2,1,3)
boomText:setTextColor(255, 0, 0)

local directionOfFire = 248

function spawnTurret(lolz, xSpawn, ySpawn, dirOfFire)
–local turretSet1 = sprite.newSpriteSet(sheet1,1,3)
spawnedTurrets = spawnedTurrets + 1 --detects the turrets and
sprite.add(turretSet1, “turret”, 1,4,600, 1)
turret[spawnedTurrets] = sprite.newSprite(turretSet1)
turretDir[spawnedTurrets]= dirOfFire
turret[spawnedTurrets].rotation = turretDir[spawnedTurrets]
turret[spawnedTurrets].x = xSpawn*display.contentWidth/320
turret[spawnedTurrets].y = ySpawn*display.contentHeight/320
turret[spawnedTurrets]:scale(_W*0.001, _W*0.001)
–turret[spawnedTurrets]:prepare(“turret”)
–turret[spawnedTurrets]:play()
if spawnedTurrets == 1 then
Runtime:addEventListener(“enterFrame”, updateTurret)
end
end

function updateTurret()
if shootTime > 100 then
for i = 1, spawnedTurrets do
sprite.add(cannonSet1, “cannon”, 1,3,1000,0)
turretBullet[i] = sprite.newSprite((cannonSet1),turret[i].x-directionOfFire/12,turret[i].y-directionOfFire/12)
turretBullet[i]:scale( _W*0.0009, _W*0.0009 ) – adjust size of bullet
physics.addBody( turretBullet[i], “static”, { friction=1 } )
turretBullet[i]:rotate(turretDir[i])
transition.to( turretBullet[i], { time=1700, x=turretBullet[i].x + math.cos((turretDir[i])*math.pi/180)*1500, y=turretBullet[i].y + math.sin((turretDir[i])*math.pi/180)*1500, onComplete=nil } )
–transition.to( boomText, { time=400, x=boomText.x, y=boomText.y-100, onComplete=nil } )
–bulletNum = bulletNum + 1
–bulletsFired = true
–print(“Turret Firing”)
turretBullet[i]:prepare(“cannon”)
turret[spawnedTurrets]:prepare(“turret”)
turretBullet[i]:play()
turret[spawnedTurrets]:play()
end
shootTime = math.random(-15,5)
else
shootTime = shootTime + 1
end
end [import]uid: 94237 topic_id: 16938 reply_id: 316938[/import]

after some more research it doesn’t seem to be recognizing the directionOfFire. But I have no idea why not. [import]uid: 94237 topic_id: 16938 reply_id: 63503[/import]

Try replacing this:

[blockcode]
turretBullet[i] = sprite.newSprite((cannonSet1),turret[i].x-directionOfFire/12,turret[i].y-directionOfFire/12)
turretBullet[i]:scale( _W*0.0009, _W*0.0009 ) – adjust size of bullet
[/blockcode]

With this:

[blockcode]
turretBullet[i] = sprite.newSprite(cannonSet1)
turretBullet[i].x = turret[i].x
turretBullet[i].y = turret[i].y
turretBullet[i]:scale( _W*0.0009, _W*0.0009 ) – adjust size of bullet
[/blockcode]

Changing the cannonball’s initial x and y coordinates to those of the turret firing it without any offset (which were likely the cause of it being offscreen in the first place) should fix it.

Also, I do have a question for you: why make the cannonballs static if they’re going to be moving?

[import]uid: 89724 topic_id: 16938 reply_id: 63508[/import]

thank you, that worked splendidly. To answer your question, I didn’t make the cannonballs static another, person on the project did. What would you recommend instead?
[import]uid: 94237 topic_id: 16938 reply_id: 63510[/import]

I’d recommend the default physics body type, dynamic, since it’ll be moving and colliding with other objects.

Just erase the “static” but keep a comma and space between the turretBullet[i] and the curly bracket. [import]uid: 89724 topic_id: 16938 reply_id: 63513[/import]