Print color of object not wokring

When i put local bombstartnew outside the function it does not print the correct color rgb vlue but when it is in the fucntion it does print the correct rgb value of the color

local bombstartnew local function bombTouched(event) if ( event.phase == "began" ) then print(unpack(bombstartnew.fillColor)) Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score + 1 -- add new score to current score scoreText.text = score end end local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombstartnew.enterFrame = offscreen Runtime:addEventListener( "enterFrame", bombstartnew ) bombstartnew:addEventListener( "touch", bombTouched ) end

i’ve made some changes to get a  non error code just to test, and works fine:

local physics = require( "physics" ) physics.start() local bombstartnew local function bombTouched(event) if ( event.phase == "began" ) then print(unpack(bombstartnew.fillColor)) end end local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) bombstartnew = display.newRect( startingpoint, 100, 50, 50) physics.addBody( bombstartnew, "dynamic", { density=1.6, friction=0.5, bounce=0.2 } ) local color1, color2, color3 = 0.5, 0.5, 1 bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombstartnew:addEventListener( "touch", bombTouched ) end addnewobject()

check your line where is “local color1, color2, color3 =ragdogLib.convertHexToRGB(randomcolor())”.

Change it to “local color1, color2, color3 = 0.5, 0.5, 1”. i don’t know if that function is returning the correct values since you didn’t provide it.

sorry but this is not i want ,convert to hex thing returns value in rgb formant and the function random color make generates diffrent colors in hex vlaue and the problem is when local bombstartnew in the function it prints correct object color but when it is outside it prints wrong vlaue

sorry but this is not i want ,convert to hex thing returns value in rgb formant and the function random color make generates diffrent colors in hex vlaue and the problem is when local bombstartnew in the function it prints correct object color but when it is outside it prints wrong vlaue

here is rest of code local ragdogLib = require “ragdogLib”;

function randomcolor()

local colors = { “#e67e22”,"#e74c3c" ,"#f1c40f","#1abc9c","#8e44ad" }

local finalcolor = colors[math.random(1, 5)]

return finalcolor

end

the code i sent you was just to prove that outside the function addnewobject you can retrieve the color of the object pressed. there are infinite ways to accomplish that. you should start with simple programs so you can understand the scope.

you can start with this kinda of code and evolve from that:

local var1=1 local function test() var1=2 local var2=3 print ("\_\_\_") print (var2) -- only inside is visible outside will print nil print ("\_\_\_") end print (var1, var2) -- print 1,nil test() -- will change var to 2 print (var1, var2) -- print 2, nil

What is it printing?

What do you expect it to print?

Rob

I want to print the current color of the object randomcolor makes random colors

What is it currently printing? (copy/paste please)

And when I asked what you expect, I meant are you expecting a number between 0…1, 0…255, hex codes?

Rob

Not sure exactly what you’re looking for… but you can print/access the fill color of a display object by doing this:

print(myObject.fill.r, myObject.fill.g, myObject.fill.b)

If you want that in RGB (0>255)

print(myObject.fill.r / 255, myObject.fill.g / 255, myObject.fill.b / 255)

I am expecting an rgb value here is full code

local ragdogLib = {}; ragdogLib.convertHexToRGB = function(hexCode) assert(#hexCode == 7, "The hex value must be passed in the form of #XXXXXX"); local hexCode = hexCode:gsub("#","") return tonumber("0x"..hexCode:sub(1,2))/255,tonumber("0x"..hexCode:sub(3,4))/255,tonumber("0x"..hexCode:sub(5,6))/255; end return ragdogLib;

local physics = require( "physics" ) physics.start() local ragdogLib = require "ragdogLib"; function randomcolor() local colors = { "#e67e22","#e74c3c" ,"#f1c40f","#1abc9c","#8e44ad" } local finalcolor = colors[math.random(1, 5)] return finalcolor end local function bombTouched(self,event) if ( event.phase == "began" ) then print(unpack(self.fillColor)) Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() self = nil score = score + 1 -- add new score to current score scoreText.text = score end end local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) local bombstartnew = display.newRect( startingpoint, -340, 50, 50) physics.addBody( bombstartnew ) local color1, color2, color3 = ragdogLib.convertHexToRGB(randomcolor()) bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombstartnew:addEventListener( "touch", bombstartnew ) Runtime:addEventListener( "enterFrame", bombstartnew ) end timer.performWithDelay(500, addnewobject,0)

The code in the OP will always print the colour of the most recently created one because you’re doing

print(unpack(bombstartnew.fillColor))

rather than

print(unpack(event.target.fillColor))

which would print the colour of the one you’d touched

i’ve made some changes to get a  non error code just to test, and works fine:

local physics = require( "physics" ) physics.start() local bombstartnew local function bombTouched(event) if ( event.phase == "began" ) then print(unpack(bombstartnew.fillColor)) end end local function addnewobject() local startingpoint = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) bombstartnew = display.newRect( startingpoint, 100, 50, 50) physics.addBody( bombstartnew, "dynamic", { density=1.6, friction=0.5, bounce=0.2 } ) local color1, color2, color3 = 0.5, 0.5, 1 bombstartnew:setFillColor(color1, color2, color3); bombstartnew.fillColor = {color1, color2, color3} bombstartnew:addEventListener( "touch", bombTouched ) end addnewobject()

check your line where is “local color1, color2, color3 =ragdogLib.convertHexToRGB(randomcolor())”.

Change it to “local color1, color2, color3 = 0.5, 0.5, 1”. i don’t know if that function is returning the correct values since you didn’t provide it.

sorry but this is not i want ,convert to hex thing returns value in rgb formant and the function random color make generates diffrent colors in hex vlaue and the problem is when local bombstartnew in the function it prints correct object color but when it is outside it prints wrong vlaue

sorry but this is not i want ,convert to hex thing returns value in rgb formant and the function random color make generates diffrent colors in hex vlaue and the problem is when local bombstartnew in the function it prints correct object color but when it is outside it prints wrong vlaue

here is rest of code local ragdogLib = require “ragdogLib”;

function randomcolor()

local colors = { “#e67e22”,"#e74c3c" ,"#f1c40f","#1abc9c","#8e44ad" }

local finalcolor = colors[math.random(1, 5)]

return finalcolor

end

the code i sent you was just to prove that outside the function addnewobject you can retrieve the color of the object pressed. there are infinite ways to accomplish that. you should start with simple programs so you can understand the scope.

you can start with this kinda of code and evolve from that:

local var1=1 local function test() var1=2 local var2=3 print ("\_\_\_") print (var2) -- only inside is visible outside will print nil print ("\_\_\_") end print (var1, var2) -- print 1,nil test() -- will change var to 2 print (var1, var2) -- print 2, nil

What is it printing?

What do you expect it to print?

Rob

I want to print the current color of the object randomcolor makes random colors

What is it currently printing? (copy/paste please)

And when I asked what you expect, I meant are you expecting a number between 0…1, 0…255, hex codes?

Rob