Midpoint-Based Movement Functionality Added

Added a function called moveTextToMidpoint() which changes the position of the text based on what midpoint you want it to have.
Extremely handy for keeping text centered. Also, changed resizeText() using this new function, making resizeText() much simpler.
This commit is contained in:
Xela10001 2021-08-08 18:05:49 +01:00 committed by GitHub
parent 4629ab4fcd
commit f2d0a5b439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
package;
import flixel.math.FlxPoint;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxTween;
import flixel.FlxG;
@ -285,32 +286,31 @@ class Alphabet extends FlxSpriteGroup
// ThatGuy: Ooga booga function for resizing text, with the option of wanting it to have the same midPoint
// Side note: Do not, EVER, do updateHitbox() unless you are retyping the whole thing. Don't know why, but the position gets retarded if you do that
public function resizeText(xScale:Float, yScale:Float, xStaysCentered:Bool = true, yStaysCentered:Bool = false):Void {
var oldWidth:Float = this.width;
var oldHeight:Float = this.height;
//trace('old x before scaling: ' + this.x);
//trace('old midpoint before scaling: ' + this.getMidpoint().x);
var oldMidpoint:FlxPoint = this.getMidpoint();
reType(text, xScale, yScale);
//trace('old x after scaling: ' + this.x);
//trace('old midpoint after scaling: ' + this.getMidpoint().x);
//This works, commenting out all the tracing
if(!(xStaysCentered && yStaysCentered)){
if(xStaysCentered) {
/*
If oldX is the old position, that is the same for both sizes of text, e.g. oldX = 50
And oldWidth is the old width of the text, e.g. oldWidth = 100
And newWidth is the current width of the text, e.g. newWidth= 150
And the midpoint, is always the same, e.g. midpoint = oldX + oldWidth/2 = 50 + 100/2 = 100
and the newMidpoint, which is equal to midpoint, is newX + newWidth/2, then
newX = midpoint - newWidth/2 <=> newX = oldX + oldWidth/2 - newWidth/2, e.g., <=> newX = 50 + 100/2 - 150/2 <=> newX = 25
*/
//Since this.x doesnt change with text scaling, in this equation, this.x can be used as both the old and the new x
this.x = this.x + oldWidth/2 - this.width/2;
//trace('new x after scaling: ' + this.x);
//trace('new midpoint after scaling: ' + this.getMidpoint().x);
//I can just use this juicy new function i made
moveTextToMidpoint(new FlxPoint(oldMidpoint.x, getMidpoint().y));
}
if(yStaysCentered) {
// Same logic applies here
this.y = this.y + oldHeight/2 - this.height/2;
moveTextToMidpoint(new FlxPoint(getMidpoint().x, oldMidpoint.y));
}
} else {
moveTextToMidpoint(new FlxPoint(oldMidpoint.x, oldMidpoint.y));
}
}
// ThatGuy: Function used to keep text centered on one point instead of manually having to come up with offsets for each sentence
public function moveTextToMidpoint(midpoint:FlxPoint):Void {
/*
e.g. You want your midpoint at (100, 100)
and your text is 200 wide, 50 tall
then, x = 100 - 200/2, y = 100 - 50/2
*/
this.x = midpoint.x - this.width / 2;
this.y = midpoint.y - this.height / 2;
}
}