hello
I want to make coloring book. I get up with idea to make pictures in corona by makeing lt of rectangles. I got coordinates and values of each picture’s pixel. I draw it by my function
for j=1, hight do
for i=1, width do
d=(j*width)-295
local pixelColor = table[i+d]
local pixel = display.newRect(0, 0, valuex, valuey)
pixel:setFillColor(pixelColor)
pixel.x = i
pixel.y = j
end
so this algorithm made me a lot of rectangles which i want to fill up with colors. I have ready drawn picture in Corona. Anyone know how i could now fill them up ? What i need to use?
put those rectangles in a table declared outside the first for cicle, right now they are local you can’t access them anywhere else. add touch event to the rectangles, when you touch then you can change the color for whatever you want…saying this…corona is not the best language to build projects involving pixels and color.
could you show me how i can put it on the table ?
-- painting tool example -- this code creates rectangles with random colors in the screen. You can touch them to change the color to black -- after 5 seconds the color of the rectangles will be turned grey, after that you still can change the color to black -- touch function to change color local function onObjectTouch( self, event ) if ( event.phase == "moved" ) then print( "object touched: " .. self.id ) -- change the color of the rectangles to black self:setFillColor(0,0,0,1) end return true end -- end touch event local pixel={} -- table where all display.newRect will be stored will be a bidimensional table local height=300 local width=300 local rand=math.random -- always make a local variable to call math functions -- size of the rectangles to be filled with color local valuex=5 local valuey=5 -- to help store the elements in a sequencial order in the bidimensional table created to store them local counterX=1 local counterY=1 -- magic working for j=1, height, valuey do pixel[counterY]={} -- adding second dimension -- reset counterX to start from 1 again when you finish adding a new line of rectangles counterX=1 for i=1, width, valuex do -- random rgb values to fill the rectangle local r=rand() local g=rand() local b=rand() -- create the rectangule pixel[counterY][counterX] = display.newRect(0, 0, valuex, valuey) pixel[counterY][counterX].anchorX=0 pixel[counterY][counterX].anchorY=0 -- give a random color to it pixel[counterY][counterX]:setFillColor(r,g,b,1) -- position it in the screen pixel[counterY][counterX].x = i pixel[counterY][counterX].y = j -- giving a name to the rectangle just to check what object i'm touching pixel[counterY][counterX].id = counterX..", "..counterY pixel[counterY][counterX].touch = onObjectTouch -- adding touch event to change the color pixel[counterY][counterX]:addEventListener( "touch", pixel[counterY][counterX] ) -- adding the value to prepare to store next element counterX=counterX+1 end -- adding the value to prepare to store next element counterY=counterY+1 end local function changeColor() for j=1, #pixel do for i=1, #pixel[j] do pixel[j][i]:setFillColor(0.4,0.4,0.4,1) -- pixel[j][i]:removeSelf() -- pixel[j][i]=nil end end end -- after 5 seconds all rectangles will be gray but the touch event will still be present so you can change them to black again timer.performWithDelay(5000,changeColor,1)
I’ve created this code so you can learn from it. i suggest learn more basic from corona tutorials also because this is really basic stuff.
hope this helps a little.
Regards, Carlos.
put those rectangles in a table declared outside the first for cicle, right now they are local you can’t access them anywhere else. add touch event to the rectangles, when you touch then you can change the color for whatever you want…saying this…corona is not the best language to build projects involving pixels and color.
could you show me how i can put it on the table ?
-- painting tool example -- this code creates rectangles with random colors in the screen. You can touch them to change the color to black -- after 5 seconds the color of the rectangles will be turned grey, after that you still can change the color to black -- touch function to change color local function onObjectTouch( self, event ) if ( event.phase == "moved" ) then print( "object touched: " .. self.id ) -- change the color of the rectangles to black self:setFillColor(0,0,0,1) end return true end -- end touch event local pixel={} -- table where all display.newRect will be stored will be a bidimensional table local height=300 local width=300 local rand=math.random -- always make a local variable to call math functions -- size of the rectangles to be filled with color local valuex=5 local valuey=5 -- to help store the elements in a sequencial order in the bidimensional table created to store them local counterX=1 local counterY=1 -- magic working for j=1, height, valuey do pixel[counterY]={} -- adding second dimension -- reset counterX to start from 1 again when you finish adding a new line of rectangles counterX=1 for i=1, width, valuex do -- random rgb values to fill the rectangle local r=rand() local g=rand() local b=rand() -- create the rectangule pixel[counterY][counterX] = display.newRect(0, 0, valuex, valuey) pixel[counterY][counterX].anchorX=0 pixel[counterY][counterX].anchorY=0 -- give a random color to it pixel[counterY][counterX]:setFillColor(r,g,b,1) -- position it in the screen pixel[counterY][counterX].x = i pixel[counterY][counterX].y = j -- giving a name to the rectangle just to check what object i'm touching pixel[counterY][counterX].id = counterX..", "..counterY pixel[counterY][counterX].touch = onObjectTouch -- adding touch event to change the color pixel[counterY][counterX]:addEventListener( "touch", pixel[counterY][counterX] ) -- adding the value to prepare to store next element counterX=counterX+1 end -- adding the value to prepare to store next element counterY=counterY+1 end local function changeColor() for j=1, #pixel do for i=1, #pixel[j] do pixel[j][i]:setFillColor(0.4,0.4,0.4,1) -- pixel[j][i]:removeSelf() -- pixel[j][i]=nil end end end -- after 5 seconds all rectangles will be gray but the touch event will still be present so you can change them to black again timer.performWithDelay(5000,changeColor,1)
I’ve created this code so you can learn from it. i suggest learn more basic from corona tutorials also because this is really basic stuff.
hope this helps a little.
Regards, Carlos.