Escape Characters (newline, tab) in Text object?


(Willard Torres) #1

This is a weird problem I’ve been encountering. Until recently, I’ve found that using ‘\n’ and ‘t’ characters would work well in display Text. Now, it doesn’t work at all! The output in the log is correct (with proper spaces and indentations), but in the actual game, the text is all squeezed up. Any possible reasons for this?

If it helps, I load text from an Ogmo level, and replace all “\n” and “\t” with “\n” and “\t”, as I saw in a post here once.


(Willard Torres) #2

I just realized now that this might have happened due to me switching from a downloaded FP source to the GitHub repo.


(Ultima2876) #3

I’m not intimately familiar with FlashPunk’s Text class, but my immediate thought is that the ‘multiline’ properly of the text has somehow been set to false, so it is always drawing on a single line. Not sure if it’s a FlashPunk problem or something in your code though - sorry if that’s only of limited help!


(Jacob Albano) #4

I looked through one of my own projects and might have found your problem. Ogmo doesn’t store newlines as \n (or \r, as I had previously thought); it uses the XML sequences for carriage returns.




If you replace occurrences of that string with \n, you should be good to go.


(Alex Larioza) #5

I just encountered this issue myself. The latest code does seem to have wordWrap set to false as setting it to true makes “\n” work as expected again. I’m pretty certain wordWrap was enable by default before…what’s with the change?

EDIT: I take that back, setting wordWrap to true does not fix the issue. Not sure why the new line character isn’t working.


(Willard Torres) #6

Upon comparing the two versions of Text.as (one from the archive download and the other from the GitHub, repo), I saw that the only difference was this:

			// reassign text to force a recalc of TextLineMetrics and so be sure they report correct values
		_field.htmlText = _field.htmlText;

Anyone know what this? I myself would look more into this.


(azrafe7) #7

Tabs and newlines seem to work correctly for me (although I’m not loading text from ogmo).

And yeah… I know what that line does (see Questions about text). What happens if you comment it out?


(Alex Larioza) #8

I’m not loading text from Ogmo, I’m just straight up creating a new text object with the string “test\ntest” and it renders as “testtest”


(azrafe7) #9

This works for me with the current version of FlashPunk (swf):

package {
	import net.flashpunk.Engine;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Text;

	public class Main extends Engine
	{
		
		public function Main() {
			super(640, 480);
		}
		
		override public function init():void {
			super.init();
			
			FP.console.enable();
			var text:Text = new Text("test\ntest\ttab", 40, 40);
			FP.world.addGraphic(text);
		}		
		
		public static function main():void { 
			new Main(); 
		}
		
	}
}


(Ultima2876) #10

Could this be a Flex issue? I remember a while ago they changed some font settings that screwed some stuff up, so it could possibly be a new compiler nightmare?


(Alex Larioza) #11

Hmmm… I’m on 4.6.0 (23201).

Is anyone else having this issue or is it just me? It might be my FlashPunk repo as I’m using my own branch. However, I haven’t made any changes to the Text class and I recently merged with the master branch, so I wouldn’t expect that to be the issue…

@azrafe7 Here’s what your code produces on my system:

EDIT: I just tested @azrafe7 test code with the FlashPunk version directly from Github and received the same results above, so its definitely got to do with Flash, Flex, or something else.


(azrafe7) #12

I’m using your exact same Flex version and it’s working o.O. Does it report the correct string if you trace text.text?

Also… can you test this (at the end of previous init())?

	var fmt:TextFormat = new TextFormat(text.font, 16, 0xE08000);
	var text2:TextField = new TextField();
	text2.embedFonts = true;
	text2.defaultTextFormat = fmt;
	text2.setTextFormat(fmt);
	text2.wordWrap = true;
	text2.multiline = true;
	text2.autoSize = TextFieldAutoSize.LEFT;
	text2.text = "test\ntest\ttab";
	text2.x = 180;
	text2.y = 40;
	FP.engine.addChild(text2);

(Alex Larioza) #13

Tracing the string of the text object results in a correctly formatted string. Here’s the results of the new code:


(azrafe7) #14

Oh… so it does work with standard as3… this is odd (but I’m starting to think that you’re somehow inadvertently using a modified Text class)!

If you can post a full FD project (including source of FlashPunk you’re using) I will take a look.

Assuming you’re on Windows, which version of FlashDevelop do you have? Also… opening the linked swf I posted above shows correct or wrong behaviour?


(Alex Larioza) #15

I tested your code in a new project with the FP branch directly from Github and also my own branch. Both have the exact same problem.

I’m using version 4.5.2.5 of FlashDevelop.

Here’s the project I tested in: http://www.mediafire.com/download/3eivh7942h9c3g7/sandbox.zip


(azrafe7) #16

Oh shit!

Well it seems is actually a regression due to my fix for another issue (Merge pull request #123 from azrafe7/master), also mentioned by @Anheurystics above.

If you comment that single line of code then newlines should behave as expected. I’ll play with it a bit to see if a solution can be found to address both problems.

EDIT: This should solve both cases:

// reassign text to force a recalc of TextLineMetrics and so be sure they report correct values
_richText ? _field.htmlText = _field.htmlText : _field.text = _field.text;

But I’ll need confirmation.


(Alex Larioza) #17

Yup, this fixes the problem! :smile: