Created
May 2, 2021 01:10
-
-
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.
This file contains hidden or 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
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