I need the object to move left to right, but in the code I have, it keeps right to left and goes back left to right:
function draw() {
var time = new Date().getTime() * 0.002;
var x = Math.sin(time * 0.5)*700
var y = 58;
The problem is in line 3 .
Math.sin values oscillate between -1 and 1, which is why you're seeing your object go from right to left then back to right again. It's cycling through the min and max of Math.sin.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin
Use this instead of Math.sin to achieve the desired effect: var x += time;
Why are you using Math.sin? just use this:
var x += time;
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.