• Create Account

### #Actualj-locke

Posted 19 December 2012 - 10:49 PM

You are correct that you're canceling out your motion.

(example assuming right is true, left is false)
In the second method posted (iupd()), you're canceling out, for example, by looking at r and setting the player's x-direction speed and then looking at l and setting that very same player's x-direction speed to 0. Note that just as a fluke of the way you have ordered the if-else statements, left will cancel out right's motion but right won't cancel out left's motion (because you have the if self.i.r and then after that you have if self.i.l).
You have the same situation going with your y direction speed.

One way to address that problem is to make your method more like:
self.i.getkeys()
self.player.vx = 0
self.player.vy = 0
if self.i.r:
self.player.vx += self.player.speed
if self.i.l:
self.player.vx -= self.player.speed
if self.i.d:
self.player.vy += self.player.speed
if self.i.u:
self.player.vy -= self.player.speed

With this approach is that if you press left and right simultaneously, vx will add the variable speed and then subtract that same value, balancing out at 0 x-direction speed, which seems reasonable for someone trying to go both left and right at the same time.

Another idea would be to do this:
if self.i.r:
self.player.vx = self.player.speed
else:
if self.i.l:
self.player.vx = -self.player.speed
else:
self.player.vx = 0

It brings in the side effect that if someone were pressing right and left at the same time, the player would go right (because the first else wouldn't get hit to check for left being true).

### #1j-locke

Posted 19 December 2012 - 10:47 PM

You are correct that you're canceling out your motion.

(example assuming right is true, left is false)
In the second method posted (iupd()), you're canceling out, for example, by looking at r and setting the player's x-direction speed and then looking at l and setting that very same player's x-direction speed to 0. Note that just as a fluke of the way you have ordered the if-else statements, left will cancel out right's motion but right won't cancel out left's motion (because you have the if self.i.r and then after that you have if self.i.l).
You have the same situation going with your y direction speed.

One way to address that problem is to make your method more like:

[source lang="python"]self.i.getkeys()self.player.vx = 0self.player.vy = 0if self.i.r: self.player.vx += self.player.speedif self.i.l: self.player.vx -= self.player.speedif self.i.d: self.player.vy += self.player.speedif self.i.u: self.player.vy -= self.player.speed[/source]

With this approach is that if you press left and right simultaneously, vx will add the variable speed and then subtract that same value, balancing out at 0 x-direction speed, which seems reasonable for someone trying to go both left and right at the same time.

Another idea would be to do this:

[source lang="python"]if self.i.r: self.player.vx = self.player.speedelse: if self.i.l: self.player.vx = -self.player.speed else: self.player.vx = 0[/source]

It brings in the side effect that if someone were pressing right and left at the same time, the player would go right (because the first else wouldn't get hit to check for left being true).

PARTNERS