Newbie Doubt - Animation

Hi everybody :slight_smile:

I’m starting using Corona and Lua to develope apps. Before that I used processing but it could only program apps for android and I wanted more than that, so I chose corona for this

Since I started I had some doubts on how corona organized their animated interface and searching the manuals I read:

“The basic drawing model involves a cycle of executing Lua code and
rendering objects in the display hierarchy. During this cycle, the
screen only updates when objects in the display change. These changes
occur by adding, removing, or changing properties of the child display
objects.” (http://docs.coronalabs.com/guide/media/displayObjects/index.html)

So I thought a code like this:

R  = math.random(0,100) G = math.random(0,100) B = math.random(0,100) display.newText("Test",50,50,"Arial",60):setFillColor(R,G,B)  

Should change the color every frame, and this doesn’t happens. Why? And how could do something similar ?

First off i would set up a small loop  around your math.random - then print out to terminal so you can see that the values are changing.

T.

Ok, but if I setup any loop (as for/while) the color on the text doesn’t change, only if I display.newText(“Test”,50,50,“Arial”,60):setFillColor(R,G,B) inside the loop…

What I’m trying to say is: on the text (http://docs.coronalabs.com/guide/media/displayObjects/index.html) says that if any variable that alter the draw has its value changed, the draw would be executed again for example:

local firstRun = true local R,G,B if firstRun then &nbsp;R,G,B = 0 firstRun = false else &nbsp;if R \< 100 then &nbsp;&nbsp; R,G,B = R,G,B+10 &nbsp;else &nbsp;&nbsp; R,G,B = 0 &nbsp;end end display.newText("Test",50,50,"Arial",60):setFillColor(R,G,B) &nbsp;

If the lua code was executed in cycles, my display text color would be changing each cycle. And this, doesn’t happens.

To follow up on what @ToeKnee said, the way your code works, is it generates 1 red, 1 green and 1 blue value.  Then it creates one display object and then once sets it’s color.

There is however an issue with your setting the color.  Assuming you’re using build 2000 or later, colors are in the range of 0 … 1.  You are generating 1 to 100, so it’s going to be white unless you divide the numbers by 100 each.

Now to change the colors continuously, you are going to have to do a loop like this…

local R, G, B local myText = display.newText("Test",50,50,"Arial",60) while true do &nbsp;&nbsp;&nbsp;&nbsp; R = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; G = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; B = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; myText:setFillColor(R, G, B) end

But there is a major problem with this solution (In fact it’s not a solution).  This loop will run WAY faster than the screen updates.  Your screen updates  every 1/30th of a second (or 1/60th).  That loop is going to run millions of times per second.  In fact it will be such a tight loop, your app wont’ do anything else.

The right way to do something like this to use what’s known as an “enterFrame” listener that triggers once each frame.  This is how I would code this:

local myText = display.newText( "Test", 50, 50, "Arial", 60 ) &nbsp; local function changeColor() &nbsp;&nbsp;&nbsp;&nbsp; local R = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; local G = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; local B = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; myText:setFillColor(R, G, B) end &nbsp; Runtime:addEventListener( "enterFrame", changeColor&nbsp; )

Rob

Thanks, got it now. If I want something to happens everyframe change I need to add an event.

(Deleted original since I was too slow and Rob hit Post first. However…)

Instead of:

local R = math.random(100) / 100

You should be able to use:

local R = math.random() 

…since math.random() without parameters returns a number between 0 and 1.

You know, so you can save on typing 9 extra characters. :slight_smile:

 Jay

First off i would set up a small loop  around your math.random - then print out to terminal so you can see that the values are changing.

T.

Ok, but if I setup any loop (as for/while) the color on the text doesn’t change, only if I display.newText(“Test”,50,50,“Arial”,60):setFillColor(R,G,B) inside the loop…

What I’m trying to say is: on the text (http://docs.coronalabs.com/guide/media/displayObjects/index.html) says that if any variable that alter the draw has its value changed, the draw would be executed again for example:

local firstRun = true local R,G,B if firstRun then &nbsp;R,G,B = 0 firstRun = false else &nbsp;if R \< 100 then &nbsp;&nbsp; R,G,B = R,G,B+10 &nbsp;else &nbsp;&nbsp; R,G,B = 0 &nbsp;end end display.newText("Test",50,50,"Arial",60):setFillColor(R,G,B) &nbsp;

If the lua code was executed in cycles, my display text color would be changing each cycle. And this, doesn’t happens.

To follow up on what @ToeKnee said, the way your code works, is it generates 1 red, 1 green and 1 blue value.  Then it creates one display object and then once sets it’s color.

There is however an issue with your setting the color.  Assuming you’re using build 2000 or later, colors are in the range of 0 … 1.  You are generating 1 to 100, so it’s going to be white unless you divide the numbers by 100 each.

Now to change the colors continuously, you are going to have to do a loop like this…

local R, G, B local myText = display.newText("Test",50,50,"Arial",60) while true do &nbsp;&nbsp;&nbsp;&nbsp; R = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; G = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; B = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; myText:setFillColor(R, G, B) end

But there is a major problem with this solution (In fact it’s not a solution).  This loop will run WAY faster than the screen updates.  Your screen updates  every 1/30th of a second (or 1/60th).  That loop is going to run millions of times per second.  In fact it will be such a tight loop, your app wont’ do anything else.

The right way to do something like this to use what’s known as an “enterFrame” listener that triggers once each frame.  This is how I would code this:

local myText = display.newText( "Test", 50, 50, "Arial", 60 ) &nbsp; local function changeColor() &nbsp;&nbsp;&nbsp;&nbsp; local R = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; local G = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; local B = math.random(100) / 100 &nbsp;&nbsp;&nbsp;&nbsp; myText:setFillColor(R, G, B) end &nbsp; Runtime:addEventListener( "enterFrame", changeColor&nbsp; )

Rob

Thanks, got it now. If I want something to happens everyframe change I need to add an event.

(Deleted original since I was too slow and Rob hit Post first. However…)

Instead of:

local R = math.random(100) / 100

You should be able to use:

local R = math.random() 

…since math.random() without parameters returns a number between 0 and 1.

You know, so you can save on typing 9 extra characters. :slight_smile:

 Jay