Hi, I would like to have variables for a "prevx" and "prevy". Which would be the last x/y variables before collision, so that once the player collides with something I could just set player.x/y to prevx/prevy. I just really don't know how to go about programming it. I can think of a very long and inefficient way of doing it that would just +/- x/y depending on the direction of the player, but I'm assuming there is a better/nicer way.
Any Ideas?
Store previous x/y value?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Store previous x/y value?
@rynesaur
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Store previous x/y value?
Well, you could just create two more properties where you store your last x and y values, but that does seem like a rather crude solution. A more elegant approach would be to check for collisions before you move your character.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Store previous x/y value?
Crude, but pretty effective. To store a self.prevX all you have to is rewrite it in update(). By contrast, calculating collision before it happens is a ton of code that would be all over the place.
Ryne, to see a working example, look at the source for the "companion" actor in Kurosuke. It is actually 8 frames behind the player, but the same execution applies.
Ryne, to see a working example, look at the source for the "companion" actor in Kurosuke. It is actually 8 frames behind the player, but the same execution applies.
Kurosuke needs beta testers
Re: Store previous x/y value?
I don't quite understand iteration, and I also don't use middleclass, would you know a similar way excluding middleclass and would you mind explaining it? I would really appreciate it, if you have the time.tentus wrote:Crude, but pretty effective. To store a self.prevX all you have to is rewrite it in update(). By contrast, calculating collision before it happens is a ton of code that would be all over the place.
Ryne, to see a working example, look at the source for the "companion" actor in Kurosuke. It is actually 8 frames behind the player, but the same execution applies.
@rynesaur
- nevon
- Commander of the Circuloids
- Posts: 938
- Joined: Thu Feb 14, 2008 8:25 pm
- Location: Stockholm, Sweden
- Contact:
Re: Store previous x/y value?
What you're asking for is basically this:Ryne wrote:would you mind explaining it? I would really appreciate it, if you have the time.
Code: Select all
function love.load()
--This is where we make your character. My syntax may be fucked up from having used Python recently.
player = {
x = 100,
y = 100,
prevx = 100,
prevy = 100,
}
end
function love.update(dt)
if playeriscollidingwithstuff() then
player.x = player.prevx
player.y = player.prevy
end
player.prevx = player.x
player.prevy = player.y
end
Re: Store previous x/y value?
I see, I didn't think it would work that way, but I guess it does. Thanks nevon!nevon wrote:What you're asking for is basically this:Ryne wrote:would you mind explaining it? I would really appreciate it, if you have the time.Code: Select all
function love.load() --This is where we make your character. My syntax may be fucked up from having used Python recently. player = { x = 100, y = 100, prevx = 100, prevy = 100, } end function love.update(dt) if playeriscollidingwithstuff() then player.x = player.prevx player.y = player.prevy end player.prevx = player.x player.prevy = player.y end
@rynesaur
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Store previous x/y value?
What about storing them in local variables, before updating?
Code: Select all
player = { x = 100, y = 100 }
function love.update()
local prevX, prevY = player.x, player.y
doStuffThatMovesThePlayer()
if playerCollidesWithAnything() then
player.x, player.y = prevX, prevY --restore with prevX, prevY
end
end
When I write def I mean function.
Who is online
Users browsing this forum: No registered users and 56 guests