How can I change this to incorporate both players?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

How can I change this to incorporate both players?

Post by onedaysnotice »

Without rewriting everything for Player 2. I've tried so many things I've already lost count... Dx

Code: Select all

	if player.p1.state == "jumping" then
		player.p1.y = player.p1.y + player.p1.y_vel * dt
		player.p1.y_vel = player.p1.y_vel + gravity * dt
		if player.p1.y + player.p1.h > floor.y then
			player.p1.y = floor.y - player.p1.h
			player.p1.state = "idle"
			player.p1.y_vel = player.p1.y_vel_base
		end
	end
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How can I change this to incorporate both players?

Post by Jasoco »

Instead of "p1" use just indexes, i.e. define them as:

Code: Select all

player = {
  [1] = { stuff },
  [2] = { stuff }
}
Use a for loop from 1 to 2 to create each player the same if you need. Then change each players specifically different data, like location on screen if you need to. i.e.:

Code: Select all

player = {}
for i = 1, 2 do
  player[i] = { stuff }
end
Then you can just use a for loop to go through each player:

Code: Select all

for i = 1, #player do
if player[i].state == "jumping" then
      player[i].y = player[i].y + player[i].y_vel * dt
      player[i].y_vel = player[i].y_vel + gravity * dt
      if player[i].y + player[i].h > floor.y then
         player[i].y = floor.y - player[i].h
         player[i].state = "idle"
         player[i].y_vel = player[i].y_vel_base
      end
   end
end
A benefit is you can add more players easily since #player returns the index count of the player table's children.
onedaysnotice
Citizen
Posts: 63
Joined: Sun May 13, 2012 2:49 am

Re: How can I change this to incorporate both players?

Post by onedaysnotice »

Jasoco wrote:Instead of "p1" use just indexes, i.e. define them as:

Code: Select all

player = {
  [1] = { stuff },
  [2] = { stuff }
}
Use a for loop from 1 to 2 to create each player the same if you need. Then change each players specifically different data, like location on screen if you need to. i.e.:

Code: Select all

player = {}
for i = 1, 2 do
  player[i] = { stuff }
end
Then you can just use a for loop to go through each player:

Code: Select all

for i = 1, #player do
if player[i].state == "jumping" then
      player[i].y = player[i].y + player[i].y_vel * dt
      player[i].y_vel = player[i].y_vel + gravity * dt
      if player[i].y + player[i].h > floor.y then
         player[i].y = floor.y - player[i].h
         player[i].state = "idle"
         player[i].y_vel = player[i].y_vel_base
      end
   end
end
A benefit is you can add more players easily since #player returns the index count of the player table's children.
oh wow I was actually pretty close with some of my tries LOL I just didn't index the player when initialising them :S

Thanks so much! :)
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests