Yes
Can you post your current code. Please click on the blue <> button and paste the code into that windows please.
Thanks
Rob
local physics = require( "physics" ) physics.start() display.setDefault("background", 1, 0, 0) function randomcolor() colorlist = { "#27ae60", "#e74c3c", "#e74c3c", "#34495e", "#7f8c8d" } choosecolor = colorlist[math.random(1,5)] end function convHexColor(hex) local r = hex:sub(1, 2) local g = hex:sub(3, 4) local b = hex:sub(5, 6) return tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) end local bombstartnew = display.newRect( 150, -340, 50, 50) physics.addBody( bombstartnew ) bombstartnew.enterFrame = offscreen bombstartnew:setFillColor( convHexColor( randomcolor )) Runtime:addEventListener( "enterFrame", bombstartnew ) end timer.performWithDelay( 500, addnewobject, 0 )
The way you have this:
bombstartnew:setFillColor( convHexColor( randomcolor ))
you are passing the address of the function randomcolor() to convHexColor. The address of a function is a number (memory address) not a string of hex codes.
Perhaps you meant to do:
bombstartnew:setFillColor( convHexColor( randomcolor() ))
where you actually call the function randomcolor() with the hopes that it would return you a value, but that still won’t work because the function randomcolor() sets a variable but doesn’t actually return anything.
A rewrite of randomcolor() to be:
function randomcolor() local colorlist = { "#27ae60", "#e74c3c", "#e74c3c", "#34495e", "#7f8c8d" } local choosecolor = colorlist[math.random(1,5)] return choosecolor end
would solve that and then:
bombstartnew:setFillColor( convHexColor( randomcolor() ) )
would likely do what you need it to do.
Rob
Still get the same error and same line
here is code
local physics = require( "physics" ) physics.start() display.setDefault("background", 1, 0, 0) function randomcolor() local colorlist = { "#27ae60", "#e74c3c", "#e74c3c", "#34495e", "#7f8c8d" } local choosecolor = colorlist[math.random(1,5)] return choosecolor end function convHexColor(hex) local r = hex:sub(1, 2) local g = hex:sub(3, 4) local b = hex:sub(5, 6) return tonumber(r, 16), tonumber(g, 16), tonumber(b, 16) end function addnewobject() local bombstartnew = display.newRect( 150, -340, 50, 50) physics.addBody( bombstartnew ) bombstartnew.enterFrame = offscreen bombstartnew:setFillColor( convHexColor( randomcolor ) ) Runtime:addEventListener( "enterFrame", bombstartnew ) end timer.performWithDelay( 500, addnewobject , 0 )
ok it now works without error but the object is blue and when i put print(randomcolor) i get function: 0x7faac32f2fb0 instead of a hex value
Because you are still passing the address of the function, not the return value. Please change this line to read:
bombstartnew:setFillColor( convHexColor( randomcolor() ) ) --<— see the added parenthesis?