Created
March 29, 2015 07:09
-
-
Save kenhoff/56dfbbbe0f918ab437eb to your computer and use it in GitHub Desktop.
Field of parallaxed stars - Phaser
This file contains 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
var game = new Phaser.Game("100", "100", Phaser.AUTO, '', { preload: preload, create: create, update: update }); | |
var random = new Phaser.RandomDataGenerator() | |
function preload() { | |
game.load.image("star", "star1.png") | |
} | |
function create() { | |
game.world.setBounds(-1500, -1500, 3000, 3000) | |
stars = game.add.group() | |
for (var i = 0; i < 1000; i++) { | |
star = game.add.sprite(0, 0, "star") | |
star.depth = random.realInRange(1,10) | |
star.worldPos = {x: random.realInRange(-1500 * star.depth, 1500 * star.depth), y: random.realInRange(-1500 * star.depth, 1500 * star.depth)} | |
scale = 1 / star.depth | |
star.scale.set(scale, scale) | |
stars.add(star) | |
star.position.x = star.worldPos.x | |
star.position.y = star.worldPos.y | |
} | |
} | |
function update() { | |
stars.forEach(function(star) { | |
star.position.x = (star.worldPos.x - (game.camera.x + (game.camera.width / 2))) / star.depth | |
star.position.y = (star.worldPos.y - (game.camera.y + (game.camera.height / 2))) / star.depth | |
}) | |
game.camera.setPosition(game.input.x - (game.camera.width), game.input.y - (game.camera.height)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment