Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Created May 2, 2021 01:10
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 KinoAR/9b025ba483264e8935384fb91a9211ca to your computer and use it in GitHub Desktop.
Save KinoAR/9b025ba483264e8935384fb91a9211ca to your computer and use it in GitHub Desktop.
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 = FlxG.camera;
}
if (obj.pixelPerfectPosition) {
point.floor();
}
//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