Created
June 9, 2020 20:38
-
-
Save Srushtika/1665537d9c9f3eba6d10f26130550b4f to your computer and use it in GitHub Desktop.
Code snippet 20 - For multiplayer space invaders article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function startMovingPhysicsWorld() { | |
let p2WorldInterval = setInterval(function () { | |
if (!gameOn) { | |
clearInterval(p2WorldInterval); | |
} else { | |
// updates velocity every 5 seconds | |
if (++shipVelocityTimer >= 80) { | |
shipVelocityTimer = 0; | |
shipBody.velocity[0] = calcRandomVelocity(); | |
} | |
world.step(P2_WORLD_TIME_STEP); | |
if (shipBody.position[0] > 1400 && shipBody.velocity[0] > 0) { | |
shipBody.position[0] = 0; | |
} else if (shipBody.position[0] < 0 && shipBody.velocity[0] < 0) { | |
shipBody.position[0] = 1400; | |
} | |
} | |
}, 1000 * P2_WORLD_TIME_STEP); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment