How to make a object move to multiple position?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

How to make a object move to multiple position?

Post by SuperMeijin »

Ive seen this thread and it works viewtopic.php?f=4&t=10822. But want to do this for multiple objects. Heres my code

Code: Select all

function love.keypressed(key)
    if (key == " ") and playerDead == false then
	    shoot()
	end
	if (key == "j") then
	    spawnEnemies()
    end
end

Code: Select all

function shoot() -- function for shooting
    local shot = {}
	shot.x = player.x + 50
	shot.y = player.y + 40
	table.insert(player.shots, shot)
end

Code: Select all

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
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: How to make a object move to multiple position?

Post by Roland_Yonaba »

Hi,

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:

Code: Select all

 -- 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
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: How to make a object move to multiple position?

Post by SuperMeijin »

A few questions. What is a local table? And what does ~= do?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to make a object move to multiple position?

Post by Robin »

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.
Help us help you: attach a .love.
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: How to make a object move to multiple position?

Post by SuperMeijin »

I understand you code but im not sure where to put it?
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: How to make a object move to multiple position?

Post by miko »

Robin wrote:
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:

Code: Select all

-- 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.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: How to make a object move to multiple position?

Post by substitute541 »

SuperMeijin wrote:I understand you code but im not sure where to put it?
It's a logic operator. It's used for while loops, if, if-elseif, etc. statements. For example, "if (same ~= same) then dosomething end".
Currently designing themes for WordPress.

Sometimes lurks around the forum.
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: How to make a object move to multiple position?

Post by SuperMeijin »

Ive only got this far and theyre still moving as one

Code: Select all

function love.mousepressed(x, y, button)
    if button == "l" then
		startX = player.x + 80
		startY = player.y + 60
		destX = x
		destY = y
		angle = (math.atan2(destY - player.y, destX - player.x))
		bulletDy = shotspeed * math.sin(angle)
		bulletDx = shotspeed * math.cos(angle)
		shoot()
	end
end

Code: Select all

function shoot() -- function for shooting
    shot = {}
	shot.x = startX
	shot.y = startY
	dx = bulletDx
	dy = bulletDy
	table.insert(player.shots, shot)
end

Code: Select all

	for i,v in ipairs(player.shots) do -- movement for shots
		v.x = v.x + (dx*dt)
		v.y = v.y + (dy*dt)
		if v.x > 800 then
SuperMeijin
Prole
Posts: 15
Joined: Wed Oct 03, 2012 3:37 pm

Re: How to make a object move to multiple position?

Post by SuperMeijin »

I have solved the problem now thanks for all the help.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest