Skip to content

Instantly share code, notes, and snippets.

@Srushtika
Created June 9, 2020 20:38
Show Gist options
  • Save Srushtika/1665537d9c9f3eba6d10f26130550b4f to your computer and use it in GitHub Desktop.
Save Srushtika/1665537d9c9f3eba6d10f26130550b4f to your computer and use it in GitHub Desktop.
Code snippet 20 - For multiplayer space invaders article
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