\n Does this thing actually work

\n is supposed to make a line break.  I have never been able to make this work.

Does this still work in the present incarnation of Corona SDK or is this outdated?

Here’s my code if you need that.

[lua]

–set the path to the documents directory

pathDest = system.pathForFile( “ch13Write”, system.DocumentsDirectory)

– open/create the file

local myFile = io.open (pathDest, “a”)

myFile:write (“Hallelujah! I made a file! \n”)

myFile:flush()

io.close(myFile)

–check that the file was created

myFile = io.open ( pathDest, “r”)

if myFile then

–the file exists, read the data

local contents = myFile:read( “*a”)

local myOutput = “Contents of \n” … pathDest … “\n” … contents

io.close(myFile)

display.newText(myOutput, 50, 50, nil, 16)

end

[/lua]

i use it in some of my code and it works using build 2197

Here’s my version and if I’m reading it right, I’m on build 2189.  I’m using the free version.  Is this the culprit?

Version 2014.2189 (2014.3.6)

I’m not sure what you’re expecting to happen here.  The write, writes a single line with a newline at the end, which basically puts a single line in your text file.  The next time you run it, it appends the same line again.  Looking at the file in a text editor, it’s behaving exactly like you have it programmed.

However I suspect the real question is why is the text your displaying on the screen is just one line?  That’s because display.newText() operates in two modes:  single line mode and multi-line mode.  In single line mode, newlines are ignored.  It generates a single line of text.   In multi-line modes, newlines are honored.

What is the difference?

Consider:

textObject = display.newText(“initial string”, x, y, font, fontSize)

vs:

textObject = display.newText(“initial string”, x, y, width, height, font, fontSize)

The first version where no width and height were specified is “single line mode”, just as you have it programmed.  The second form, where a width and height are added, it will create a block “width” pixels wide and wrap the text when it fills up.  Newlines are processed and go to the beginning of the line.  If you specify a non-zero height, then text that is too long will be clipped at the height.  A height of 0 says make the text block big enough to hold all lines of text (up to the maxTextureHeight of the device).

Rob

Thanks, Rob.  I understand now.     Just by adding the width and height, the \n break works just fine.

i use it in some of my code and it works using build 2197

Here’s my version and if I’m reading it right, I’m on build 2189.  I’m using the free version.  Is this the culprit?

Version 2014.2189 (2014.3.6)

I’m not sure what you’re expecting to happen here.  The write, writes a single line with a newline at the end, which basically puts a single line in your text file.  The next time you run it, it appends the same line again.  Looking at the file in a text editor, it’s behaving exactly like you have it programmed.

However I suspect the real question is why is the text your displaying on the screen is just one line?  That’s because display.newText() operates in two modes:  single line mode and multi-line mode.  In single line mode, newlines are ignored.  It generates a single line of text.   In multi-line modes, newlines are honored.

What is the difference?

Consider:

textObject = display.newText(“initial string”, x, y, font, fontSize)

vs:

textObject = display.newText(“initial string”, x, y, width, height, font, fontSize)

The first version where no width and height were specified is “single line mode”, just as you have it programmed.  The second form, where a width and height are added, it will create a block “width” pixels wide and wrap the text when it fills up.  Newlines are processed and go to the beginning of the line.  If you specify a non-zero height, then text that is too long will be clipped at the height.  A height of 0 says make the text block big enough to hold all lines of text (up to the maxTextureHeight of the device).

Rob

Thanks, Rob.  I understand now.     Just by adding the width and height, the \n break works just fine.