display multiple lines of text via for loop

Okay im embarrassed to even post this but Im n00b status at lua and am trying to understand for loops and pairs.

heres my code:

local myArray = {“one”, “two”, “three”}

for k,v in ipairs(myArray) do
myText1 = display.newText(v, 100, 100, native.systemFont, 46)
end

Of course, when i run this in the simulator, all the values in my array are stacked on top of each other in one text field.

My question is, how do i take my array and in the for loop, just display them in a cascading order?

My understanding is that the for loop will just loop through my array and execute whatever i specify until it reaches the end. correct?

i’ve tried writing:

myText.y = myText +10
myText.y = y +20
etc…

Thanks in advance Corona community!
[import]uid: 88004 topic_id: 24735 reply_id: 324735[/import]

Try changing the new text line to something like this;

[lua]myText1 = display.newText(v, 100, 100*k, native.systemFont, 46)[/lua]

What that will do is start at 100*1 as the Y, then 100*2, 100*3, etc.

Also please try to post your code within < lua > and < /lua > tags, makes it a little easier to read :wink:

Let me know if this helps you - and don’t be embarrassed, the only bad questions are the ones you don’t ask.

Peach :slight_smile: [import]uid: 52491 topic_id: 24735 reply_id: 100264[/import]

Remember that creating a new object doesn’t delete the old object even if you use the same name. In a loop like yours, though, you don’t want to use the same name like that.

Consider something like this:

[code]

local myArray = {“one”, “two”, “three”}
local myTexts = {}

for k,v in ipairs(myArray) do
myTexts[k] = display.newText(v, 100, 100, native.systemFont, 46)
myTexts[k]:translate( 0, (k-1) * 24 )
end
[/code] [import]uid: 44647 topic_id: 24735 reply_id: 100265[/import]

Oooh Toby has a good point, I may have misunderstood the question. Apologies if so! [import]uid: 52491 topic_id: 24735 reply_id: 100266[/import]

Thank you so much Peach and Toby2! I’ll post properly on the forums next time

Reading up on ‘translate’ right now as well. Gawd i should have been posting here a long time ago

cheers!
-kel [import]uid: 88004 topic_id: 24735 reply_id: 100400[/import]

Don’t worry, at least it wasn’t 200 lines outside tags - that gives me a headache :wink:

Good luck with your project, hopefully next time I’ll understand the question first time around!

Peach :slight_smile: [import]uid: 52491 topic_id: 24735 reply_id: 100504[/import]

@ peachpellen. I currently have a myText1 atleast 3 times myText2,myText3 which works. But I want to make seperate paragraphs for different steps. which text format is the best to make that happen? because I am trying with what I have and everything keeps overlapping each other. [import]uid: 69302 topic_id: 24735 reply_id: 122700[/import]

Can we see some plug and play code, please? I’m having a slightly hard time envisioning your setup and goal. Will do what I can to help if you can get back to me on that :slight_smile: [import]uid: 52491 topic_id: 24735 reply_id: 122737[/import]

[code]local myText1= display.newText(“How this app work!”, 0, 0, native.systemFont, 30)
screenCenterX = display.viewableContentWidth / 2
screenCenterY = display.viewableContentHeight /6

myText1:setTextColor(255, 0, 0)
group:insert(myText1)

[code]local myText2= display.newText("STEP 1. ", 0, 0, native.systemFont, 25)
–myText2.text = “Or you can change the text here”
myText2.x = display.contentWidth /2;
myText2.y = display.contentHeight /6;
myText2:setTextColor(255, 0, 0)
group:insert(myText2)

local myText3= display.newText(“whats your name”, 0, 0,200, 200, native.systemFont, 16)
myText3.x = display.contentWidth /2;
myText3.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText3:setTextColor(255, 255, 255)
group:insert(myText3)

local myText4= display.newText("STEP 2. ", 0, 0, native.systemFont, 25)
myText4.x = display.contentWidth /50;
myText4.y = display.contentHeight /10;
myText4:setTextColor(255, 0, 0)
group:insert(myText4)

local myText5= display.newText(“how old are you?”, 0, 0,200, 200, native.systemFont, 16)
myText5.x = display.contentWidth /2;
myText5.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText5:setTextColor(255, 255, 255)
group:insert(myText5)

local myText6= display.newText(“Step 3.”, 0, 0,200, 200, native.systemFont, 16)
myText6.x = display.contentWidth /2;
myText6.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText6:setTextColor(255, 255, 255)
group:insert(myText6)

local myText5= display.newText(“Where are you from?”, 0, 0,200, 200, native.systemFont, 16)
myText5.x = display.contentWidth /2;
myText5.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText5:setTextColor(255, 255, 255)
`` group:insert(myText5)

So lines 1-16 works fine. I only added the other text so that you can see what I am trying to do. So I know its not right because I just quickly copied and paste so you can get the visual. But doing it right it just kept overlapping, so Im thinking there is a better way to get it to look how I want.
[import]uid: 69302 topic_id: 24735 reply_id: 122740[/import]

@ peachpellen. I currently have a myText1 atleast 3 times myText2,myText3 which works. But I want to make seperate paragraphs for different steps. which text format is the best to make that happen? because I am trying with what I have and everything keeps overlapping each other. [import]uid: 69302 topic_id: 24735 reply_id: 122700[/import]

Can we see some plug and play code, please? I’m having a slightly hard time envisioning your setup and goal. Will do what I can to help if you can get back to me on that :slight_smile: [import]uid: 52491 topic_id: 24735 reply_id: 122737[/import]

[code]local myText1= display.newText(“How this app work!”, 0, 0, native.systemFont, 30)
screenCenterX = display.viewableContentWidth / 2
screenCenterY = display.viewableContentHeight /6

myText1:setTextColor(255, 0, 0)
group:insert(myText1)

[code]local myText2= display.newText("STEP 1. ", 0, 0, native.systemFont, 25)
–myText2.text = “Or you can change the text here”
myText2.x = display.contentWidth /2;
myText2.y = display.contentHeight /6;
myText2:setTextColor(255, 0, 0)
group:insert(myText2)

local myText3= display.newText(“whats your name”, 0, 0,200, 200, native.systemFont, 16)
myText3.x = display.contentWidth /2;
myText3.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText3:setTextColor(255, 255, 255)
group:insert(myText3)

local myText4= display.newText("STEP 2. ", 0, 0, native.systemFont, 25)
myText4.x = display.contentWidth /50;
myText4.y = display.contentHeight /10;
myText4:setTextColor(255, 0, 0)
group:insert(myText4)

local myText5= display.newText(“how old are you?”, 0, 0,200, 200, native.systemFont, 16)
myText5.x = display.contentWidth /2;
myText5.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText5:setTextColor(255, 255, 255)
group:insert(myText5)

local myText6= display.newText(“Step 3.”, 0, 0,200, 200, native.systemFont, 16)
myText6.x = display.contentWidth /2;
myText6.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText6:setTextColor(255, 255, 255)
group:insert(myText6)

local myText5= display.newText(“Where are you from?”, 0, 0,200, 200, native.systemFont, 16)
myText5.x = display.contentWidth /2;
myText5.y = myText2.y + myText2.contentHeight /2 + myText3.contentHeight /2;
myText5:setTextColor(255, 255, 255)
`` group:insert(myText5)

So lines 1-16 works fine. I only added the other text so that you can see what I am trying to do. So I know its not right because I just quickly copied and paste so you can get the visual. But doing it right it just kept overlapping, so Im thinking there is a better way to get it to look how I want.
[import]uid: 69302 topic_id: 24735 reply_id: 122740[/import]

If you are trying to display texts one after one from a text array then try to maintain a table for the text views (display.newText). I am just modifying your code here.

[lua]_w = display.contentWidth;
_h = display.contentHeight;
local texts = {“How this app work”, "STEP 1. ", “Whats your Name”, “STEP 2”, “How old are you”, “STEP 3”, “Where are you from”};
local fontSize = {30, 25, 16, 25, 16, 16, 16};
local initY = _h * 0.3;
local displayTexts = {};

for i=1,#texts do
local text = display.newText(texts[i], _w/2, initY + (i-1) * 35, native.systemFont, fontSize[i]);
if(fontSize[i] == 16)then
text:setTextColor(255,255,255);
else
text:setTextColor(255,0,0);
displayTexts[i] = text;
group:insert(text);
end[/lua]

I think this procedure will help you. Here Text gaps are 35 pixels.
[import]uid: 104777 topic_id: 24735 reply_id: 124424[/import]

If you are trying to display texts one after one from a text array then try to maintain a table for the text views (display.newText). I am just modifying your code here.

[lua]_w = display.contentWidth;
_h = display.contentHeight;
local texts = {“How this app work”, "STEP 1. ", “Whats your Name”, “STEP 2”, “How old are you”, “STEP 3”, “Where are you from”};
local fontSize = {30, 25, 16, 25, 16, 16, 16};
local initY = _h * 0.3;
local displayTexts = {};

for i=1,#texts do
local text = display.newText(texts[i], _w/2, initY + (i-1) * 35, native.systemFont, fontSize[i]);
if(fontSize[i] == 16)then
text:setTextColor(255,255,255);
else
text:setTextColor(255,0,0);
displayTexts[i] = text;
group:insert(text);
end[/lua]

I think this procedure will help you. Here Text gaps are 35 pixels.
[import]uid: 104777 topic_id: 24735 reply_id: 124424[/import]