简体   繁体   中英

moving DOM object with style.left

I'm trying to create a simple game in javascript, sort of a Pong game.

When I move the opponent paddle to the right, the movement isn't as smooth as it is when I move the paddle to the left.

There isn't really much to the updating of position

if(opponentDir === 'right'){  
counter += 1; 

if(counter === 80)
  opponentDir = 'left';
 }
else
{
 counter -= 1; 

 if(counter === 0)
   opponentDir = 'right';
}

and then the same drawing function applies, Game.draw. Am I missing something with the use of style.left?

http://codepen.io/xonium/pen/qLDmj

So far i've only tested it in Chrome.

The problem was my drawing method.

Updating the velocity to reflect the movement direction of the paddle got me the disired result.

left = actualPixels + this.velocity * interpolation; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM