Skip to content

Instantly share code, notes, and snippets.

@hamaluik
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamaluik/35b14ac571ac0e36056e to your computer and use it in GitHub Desktop.
Save hamaluik/35b14ac571ac0e36056e to your computer and use it in GitHub Desktop.
This will keep sprites in the same position on the screen no matter the zoom. Demo: http://luxe.blazingmammothgames.com/screenspacezoom
// demoed at: http://luxe.blazingmammothgames.com/screenspacezoom
import luxe.Color;
import luxe.Input;
import luxe.Sprite;
import luxe.Vector;
import phoenix.Batcher;
import luxe.Camera;
class Main extends luxe.Game {
var worldSprite:Sprite;
var topLeftSprite:Sprite;
var bottomRightSprite:Sprite;
var hudBatcher:Batcher;
var hudCamera:Camera;
override function ready() {
// create the world normally
worldSprite = new Sprite({
pos: new Vector(0, 0),
size: new Vector(32, 32),
color: new Color(0, 1, 0, 1)
});
Luxe.camera.center = worldSprite.pos;
// now create the HUD
hudBatcher = new Batcher(Luxe.renderer, "HUD batcher");
hudCamera = new Camera();
hudBatcher.view = hudCamera.view;
hudBatcher.layer = 2;
Luxe.renderer.add_batch(hudBatcher);
topLeftSprite = new Sprite({
pos: new Vector(0, 0),
origin: new Vector(0, 0), // maps its position to the top left of itself
size: new Vector(64, 64),
color: new Color(1, 0, 0, 1),
batcher: hudBatcher
});
bottomRightSprite = new Sprite({
pos: new Vector(Luxe.screen.w, Luxe.screen.h),
origin: new Vector(64, 64), // map its position to the bottom right of itself
size: new Vector(64, 64),
color: new Color(0, 0, 1, 1),
batcher: hudBatcher
});
} //ready
override function onkeyup( e:KeyEvent ) {
if(e.keycode == Key.escape) {
Luxe.shutdown();
}
if(e.keycode == Key.key_1) {
hudCamera.zoom = 1;
onZoomChanged();
}
else if(e.keycode == Key.key_2) {
hudCamera.zoom = 2;
onZoomChanged();
}
else if(e.keycode == Key.key_3) {
hudCamera.zoom = 4;
onZoomChanged();
}
else if(e.keycode == Key.key_4) {
hudCamera.zoom = 0.5;
onZoomChanged();
}
else if(e.keycode == Key.key_5) {
hudCamera.zoom = 0.25;
onZoomChanged();
}
} //onkeyup
function onZoomChanged() {
topLeftSprite.pos = hudCamera.screen_point_to_world(new Vector(0, 0));
bottomRightSprite.pos = hudCamera.screen_point_to_world(new Vector(Luxe.screen.w , Luxe.screen.h));
}
} //Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment