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:
parent
4629ab4fcd
commit
f2d0a5b439
@ -1,5 +1,6 @@
|
|||||||
package;
|
package;
|
||||||
|
|
||||||
|
import flixel.math.FlxPoint;
|
||||||
import flixel.tweens.FlxEase;
|
import flixel.tweens.FlxEase;
|
||||||
import flixel.tweens.FlxTween;
|
import flixel.tweens.FlxTween;
|
||||||
import flixel.FlxG;
|
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
|
// 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
|
// 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 {
|
public function resizeText(xScale:Float, yScale:Float, xStaysCentered:Bool = true, yStaysCentered:Bool = false):Void {
|
||||||
var oldWidth:Float = this.width;
|
var oldMidpoint:FlxPoint = this.getMidpoint();
|
||||||
var oldHeight:Float = this.height;
|
|
||||||
//trace('old x before scaling: ' + this.x);
|
|
||||||
//trace('old midpoint before scaling: ' + this.getMidpoint().x);
|
|
||||||
reType(text, xScale, yScale);
|
reType(text, xScale, yScale);
|
||||||
//trace('old x after scaling: ' + this.x);
|
if(!(xStaysCentered && yStaysCentered)){
|
||||||
//trace('old midpoint after scaling: ' + this.getMidpoint().x);
|
if(xStaysCentered) {
|
||||||
//This works, commenting out all the tracing
|
//I can just use this juicy new function i made
|
||||||
if(xStaysCentered) {
|
moveTextToMidpoint(new FlxPoint(oldMidpoint.x, getMidpoint().y));
|
||||||
/*
|
}
|
||||||
If oldX is the old position, that is the same for both sizes of text, e.g. oldX = 50
|
if(yStaysCentered) {
|
||||||
And oldWidth is the old width of the text, e.g. oldWidth = 100
|
moveTextToMidpoint(new FlxPoint(getMidpoint().x, oldMidpoint.y));
|
||||||
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
|
} else {
|
||||||
and the newMidpoint, which is equal to midpoint, is newX + newWidth/2, then
|
moveTextToMidpoint(new FlxPoint(oldMidpoint.x, oldMidpoint.y));
|
||||||
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);
|
|
||||||
}
|
|
||||||
if(yStaysCentered) {
|
|
||||||
// Same logic applies here
|
|
||||||
this.y = this.y + oldHeight/2 - this.height/2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user