Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example extension for translation of a screen position into a world position for FlxObjects.
import flixel.FlxCamera;
import flixel.FlxObject;
//Sets the screen position of a FlxObject using setPosition(which is in world coordinates)
function setScreenPosition(obj:FlxObject, point:FlxPoint, ?camera:FlxCamera) {
if (camera == null) {
camera =;
if (obj.pixelPerfectPosition) {
//scrollFactor if set to 0 would result in the object moving to the screen position in world space
//So, if the screen was 500 x 500 and the player is at (600, 20) and you use this with a scroll factor of 0
//the on screen element would move to 0, 0 and not be visible on screen with a width of 500x500.
obj.setPosition((camera.scroll.x * obj.scrollFactor.x) + point.x,
(camera.scroll.y * obj.scrollFactor.y) + point.y);
return obj;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment