Hi @Saerothir,
I don’t know what other advice I can give. I pulled out the angle-tracking code, placed it in a new Lua simulation, and it seems to work perfectly. So quite possibly the problem is somewhere in the StickLib module, or something is going wrong in Storyboard. I don’t actually use Storyboard so I won’t pretend I’m an expert on how it all works… I just know the basics of it, and I also know it’s extremely fussy about what things you place in which “phase” of it.
I did remove all of the instances where you changed the reference points of objects (the turret, even the player group itself. You should be very careful about where you use those commands, because they can really muck with the coordinate system, touch coordinates, scaling, etc.
I sense that you want to have the turret rotate by its “base”, not the center, which is why you adjusted its reference point. I actually suggest that you design your turret image as a larger square with its “base” in the center of the image boundaries and transparent space all around. So, instead of adjusting the reference point of it in Corona, you place its base in the center of a square image, and the Corona display object rotates around its center as is default.
Anyway, try this yourself in a NEW Corona simulation, outside of Storyboard. For me it works perfectly, and the turret always tracks the touch position.
Sorry I couldn’t find the problem in your level1.lua code. It must be something within StickLib or elsewhere, and could take awhile to track down. Maybe you can narrow down the problem by looking there, or pulling your app out of Storyboard for the time being.
Best of luck!
Brent
--adding new local group that will represent the tank
local player = display.newGroup()
--player.xReference = 25;
--player.yReference = 23;
--adding the tank that will be controlled by the player
local user\_tank = display.newImage("gameImages/user\_tank.png");
--adding the turret that will be controlled by the player
local user\_turret = display.newImage("gameImages/user\_turret.png")
--user\_turret:setReferencePoint(player.CenterReferencePoint);
user\_turret.x = 100;
user\_turret.y = 100;
--user\_turret.yReference = 18;
--following function is a daisy-chained series of transitions to simulate the turret moving
local function simulateTankMove()
transition.to( user\_turret, { time=6000, x=924 } )
transition.to( user\_turret, { delay=6000, time=6000, y=568 } )
transition.to( user\_turret, { delay=12000, time=3000, x=462, y=400 } )
transition.to( user\_turret, { delay=15000, time=3000, x=100, y=568 } )
transition.to( user\_turret, { delay=18000, time=6000, x=100, y=100, onComplete=simulateTankMove } )
end
simulateTankMove()
--===============================================================
--===============================================================
--===============================================================
--adds a rect to act as a base for touch event relating to the turret
local transRect = display.newRect(0,0, 1024, 768)
transRect:setFillColor(255,100,150,40)
--transRect.isVisible = false
transRect.isHitTestable = true
local math\_deg = math.deg --localize 'math.deg' for better performance
local math\_atan2 = math.atan2 --localize 'math.atan2' for better performance
--likewise, this function should reside outside and above the touch function
local function angleBetween( srcX, srcY, dstX, dstY )
local angle = ( math\_deg( math\_atan2( dstY-srcY, dstX-srcX ) )+90 ) --; return angle
if ( angle \< 0 ) then angle = angle + 360 end ; return angle % 360
end
local function touchRect( event )
local ang = angleBetween( user\_turret.x, user\_turret.y, event.x, event.y )
user\_turret.rotation = ang --rotate turret on ALL touch/drag conditions
local e = event.phase
if e.phase == "began" or e.phase == "moved" then
print( "BEGAN/MOVED phase touch" )
elseif e.phase == "ended" then
print( "FIRE a bullet now!" )
end
end
--a touch listener on the sensor rectangle is all you need. So, delete the next line...
--Runtime:addEventListener("touch", touchRect) --DELETE this!
transRect:addEventListener( "touch", touchRect )
--===============================================================
--===============================================================
--adding objects to local group 'player'
player:insert(user\_tank)
player:insert(user\_turret)
[import]uid: 9747 topic_id: 30680 reply_id: 123773[/import]