Page 1 of 1
How can I change this to incorporate both players?
Posted: Thu Jun 28, 2012 6:24 am
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
Re: How can I change this to incorporate both players?
Posted: Thu Jun 28, 2012 6:56 am
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.
Re: How can I change this to incorporate both players?
Posted: Thu Jun 28, 2012 7:24 am
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!