Ok, this is the last of these topics as I really don't want to spam, but it's a key ingredient into what I'm investigating and the concept I'm coming up with.
I'm trying to work out how to approach the idea of having multiple selectable characters that I can simply toggle through to be playable.
So, say you have 4 characters on the screen at once in the same room, but can only control one at any one time, however you can switch between them with the press of a button or maybe using the 1-4 keys. How might I go about this in the script?
Cheers.
Creating multiple selectable characters
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 9
- Joined: Wed Feb 09, 2011 4:40 pm
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Creating multiple selectable characters
If you have a table called playables you could do somthing like:
Code: Select all
player = next(playables, player) or next(playables)
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Creating multiple selectable characters
I, personally, would have the four characters separately defined in a table (containing all game characters, or something). I would have a variable which references one of those tables. And I would make the controls handler act on that variable (NOT directly referencing the characters). To switch characters, point the variable at a different character. In other words:
Code: Select all
characters = {
pc1={x=10,y=10, speed=50},
pc2={x=20,y=20, speed=50},
pc3={x=30,y=30, speed=20},
pc4={x=40,y=40, speed=100},
}
player = characters.pc1
function love.keypressed(k)
if k == 1 then player = characters.pc1
elseif k == 2 then player = characters.pc2
elseif k == 3 then player = characters.pc3
elseif k == 4 then player = characters.pc4
end
end
function love.update(dt)
if love.keyboard.isDown("w") then player.y = player.y - player.speed * dt
-- et cetera
end
end
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Creating multiple selectable characters
My switching code is shorter!
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Creating multiple selectable characters
Yeah, but it's a little less easy to understand. I was just trying make my example as readable as possible.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- Jasoco
- Inner party member
- Posts: 3726
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: need to flag this might come in handy
Please don't do that. There is both a Subscribe and Bookmark link at the bottom of every page.pactace wrote:.
Re: Creating multiple selectable characters
oh thanks didnt know that
Very new programmer dont judge
Re: Creating multiple selectable characters
Here a hypothisis but maybe this would work
thats just my guess
Code: Select all
characters = {
player1
player2
player3
player4
}
player1 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player2 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player3 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player4 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
characters = Player
end
function love.keypressed(k)
if k == 1 then player = player1
elseif k == 2 then player = player2
elseif k == 3 then player = player3
elseif k == 4 then player = player4
end
end
Very new programmer dont judge
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Creating multiple selectable characters
This doesn't just not work, it makes no sense.pactace wrote:Here a hypothisis but maybe this would work
What are you trying to do here?
Help us help you: attach a .love.
Re: Creating multiple selectable characters
I hadn't tested it before it made sense to me but this might make sense no promises
Code: Select all
player = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player2 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player3 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player4 = {
x = 256,
y = 256,
x_vel = 0,
y_vel = 0,
jump_vel = -4000,
speed = 400,
flySpeed = 859,
state = "",
h = 55,
w = 45,
standing = false,
}
player = player
function love.keypressed(k)
if k == 1 then player = player1
elseif k == 2 then player = player
elseif k == 3 then player = player3
elseif k == 4 then player = player4
end
end
Very new programmer dont judge
Who is online
Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 4 guests