or i,v in ipairs(player.shots) do -- movement for shots
v.x = v.x + 5
if v.x > 800 then
table.insert(remShot, i) -- if shot is out of boundrys destroy them
end
Sorry for indenting problem
So I need each of these shots to move to where I click when I press the left mouse button
You can achieve this in a simple manner. Store the start position and the target location as properties in the bullet table.
Then use a simple logic shared by all bullets to move from their start location to their target location, like this:
-- Assuming each bullet has : x,y, target_x, target_y and speed properties
local function moveBullet(bullet,dt)
if bullet.x ~= bullet.target_x then
local dx = (bullet.target_x-bullet.x) * bullet.speed * dt
bullet.x = bullet.x + dx
end
-- same logic on y-axis
end
You may want to bring some slight changes to this, though, as it moves the bullet to the exact position of the target. Because you can't really control dx, dy (because of dt), you might have to add some tolerance parameter to stop updating the bullet position when it enters a certain range around the target.
SuperMeijin wrote:A few questions. What is a local table?
A local variable is one that is only accessible from the place it is defined. (Not sure how to explain it well. Maybe someone else can?)
This has many advantages, especially that you don't get pieces of code changing the meaning of things that happen somewhere completely different, which could have made reading, understanding and changing the code much harder.
SuperMeijin wrote:And what does ~= do?
It means "is not equal to". For example, 3 ~= 5 returns true and "same" ~= "same" returns false.
SuperMeijin wrote:A few questions. What is a local table?
A local variable is one that is only accessible from the place it is defined. (Not sure how to explain it well. Maybe someone else can?)
Yes, it is local to the current closure (scope), which can be:
- a file
- a function
- a do end block
- a for..end block
- a while...do...end block
So in this code:
-- myfile.lua
1 local a=1
1 do
12 local a=2
1 end
1
1 function test()
1 3 local a=3
1 3 for a=4,5 do
1 34
1 3 end
1 3 do
1 3 6 local a=6
1 3 end
1 end
there are 5 different instances of a variable named "a", and when you create one in local scope with the same name as one from the upper scope, then the one from the upper scope is not accessible within this local scope. Ie. You can access "file's" a(=1) only outside of function test(), because inside this function it is shadowed with its own local variable.