As a simple test code, I have a “gun” that rotates clockwise and should fire a bullet every 500 Milliseconds. The bullet should fly into the direction where the gun (displayed as a simple arrow) points.
However, something weird happens here -the bullet directions do not fit to the gun rotation, but what really confuses me is why the bullets alternate their movement direction between backward and forward with every shot. The code below demonstrates the problem (just use any small image named “gfx.png”).
I don’t know why this happens. Could anyone give me a hint please what I am doing wrong here…?
Simple sample code:
[lua]local gunX = 100
local gunY = 200
local shotSpeed = 5
local lastShot = system.getTimer()
local Shots = {}
local numShots = 0
local Gun = display.newLine(0,0, 16,0)
Gun:setColor (255,255,255)
Gun:append (8,-24, 0,0)
Gun.x = gunX
Gun.y = gunY
Gun.xOrigin = gunX-8
Gun.xReference = 8
Gun.width = 1
Gun.rotation = 45
function CreateShots()
Gun.rotation = Gun.rotation + 1
– CREATE NEW SHOT
if (system.getTimer()-lastShot > 500) then
numShots = numShots + 1
Shots[numShots] = display.newImageRect(“gfx.png”,16,16) – SIMPLE SMALL BULLET IMAGE
Shots[numShots].x = Gun.x
Shots[numShots].y = Gun.y
Shots[numShots].xSpeed = math.cos(Gun.rotation) * shotSpeed
Shots[numShots].ySpeed = math.sin(Gun.rotation) * shotSpeed
lastShot = system.getTimer()
end
– MOVE SHOTS
for i = 1, numShots do
Shots[i].x = Shots[i].x + Shots[i].xSpeed
Shots[i].y = Shots[i].y + Shots[i].ySpeed
end
end
Runtime:addEventListener(“enterFrame”, CreateShots)[/lua]
[import]uid: 9644 topic_id: 2715 reply_id: 302715[/import]
Typing a lengthy answer only to find out that the my login session was over during that time and lose my post is not nice.
Could be an interupt on the machine also that caused it. But it seems they are close, so choose what you like the best. [import]uid: 5712 topic_id: 2715 reply_id: 8182[/import]