In a function we have stumbled across, it determines the angle by a bunch of equations and such to fire towards that point however because we start the character in the middle of a 2560 * 720 dimensioned game so 1280 when firing he will only fire left. this is basically what happens
(1280, 360)
| \ |
| < o |
| / |
however when we move the player to the left most point of the map such as this point
(400, 360)
| \ ^ / |
| < o > |
| / v \ |
| = edge of screen
\ or / = Directional shooting
< = left shooting
> = right shooting
^ = shooting up
v = shooting down
I’ve suspected it is because the function bases it’s point to be shot at from the point it is created in the game which is (0,0) causing it to completely work in the second example
<lua>
-----------SHOOTING------------------
– local canShoot=true
function angleOfPoint( pt )
local x, y = pt.x, pt.y
local radian = math.atan2(y,x)
local angle = radian*180/math.pi
if angle < 0 then angle = 360 + angle end
return angle
end
function angleBetweenPoints( a, b )
local x, y = b.x - a.x, b.y - a.y
return angleOfPoint( { x=x, y=y } )
end
function rotateTo( point, degrees )
local x, y = point.x, point.y
local theta = math.rad(degrees)
local pt = {
x = x * math.cos(theta) - y * math.sin(theta),
y = x * math.sin(theta) + y * math.cos(theta)
}
return pt
end
function fireBullet(source, angle,x,y)
local bullet = display.newRect( 0,0 , 10,10 )
bullet.x, bullet.y, bullet.rotation = source.x, source.y, angle
bullet:setFillColor(0,0,0)
bullet.isType=“bullet”
physics.addBody(bullet,“dynamic”,bulletBodyElement)
group:insert(bullet)
– canShoot=true
local direction = rotateTo( {x=10,y=0}, angle )
bullet:applyForce( direction.x, direction.y, bullet.x, bullet.y )
end
function canHeShoot()
canShoot=true
end
– timer.performWithDelay(3000,canHeShoot,-1)
function touch( e )
– if isBuilding==false then
local angle = angleBetweenPoints( guy, e )
guy.rotation = angle+90
– print(e.x,e.y)
if (e.phase == “began”) then
elseif (e.phase == “moved”) then
else
– if canShoot then
fireBullet(guy, angle,e.x,e.y)
– end
end
return true
– end
end
Runtime:addEventListener(“touch”,touch)
</lua>