随机选择对象并更改所选对象的颜色

时间:2015-04-14 11:22:53

标签: list function lua corona

我有4个圈子,目前是黑色{'c1','c2','c3','c4'}。我正在设置它,以便选择其中一个圆圈,颜色从黑色变为红色。我已经设法让它在圆圈列表中打印出一个随机实体。但我不能让它改变随机选择的圆的颜色。对不起,我还是比较新的。

local composer = require( "composer" )
local scene = composer.newScene()

-- "scene:create()"
function scene:create( event )

    local sceneGroup = self.view

    local backgroundimg = display.newRect( 0,0,576,320 )
    backgroundimg.x = display.contentWidth*0.5
    backgroundimg.y = display.contentHeight*0.5
    sceneGroup:insert( backgroundimg )

    local pause = display.newImage( "image/pauseBTN.png" )
    pause.x = 508
    pause.y = 20
    sceneGroup:insert( pause )


    circle = {'c1','c2','c3','c4'}


    local function choice ( event )
        randCircle = circle[ math.random( #circle ) ]
        print(randCircle)
    end

    timer.performWithDelay( 3000, choice, 0 )

    local function tapListener( event )

    end

    c1 = display.newCircle( 75,100,75 )
    c1:setFillColor( 0,0,0 )
    sceneGroup:insert( c1 )
    c1:addEventListener( "tap", tapListener )

    c2 = display.newCircle( 300,100,75 )
    c2:setFillColor(0,0,0)
    sceneGroup:insert( c2 )
    c2:addEventListener( "tap", tapListener )

    c3 = display.newCircle( 187.5,225,75 )
    c3:setFillColor(0,0,0)
    sceneGroup:insert( c3 )
    c3:addEventListener( "tap", tapListener )

    c4 = display.newCircle( 412.5,225,75 )
    c4:setFillColor(0,0,0)
    sceneGroup:insert( c4 )
    c4:addEventListener( "tap", tapListener )

end


-- "scene:show()"
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then

    elseif ( phase == "did" ) then

    end
end


-- "scene:hide()"
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then

    elseif ( phase == "did" ) then

    end
end


-- "scene:destroy()"
function scene:destroy( event )

    local sceneGroup = self.view

end


-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

return scene

1 个答案:

答案 0 :(得分:2)

你需要实际的对象,而不是字符串:

circle = {c1, c2, c3 ,c4}

此行应在创建圆后执行。

在定义choice之前,请添加

local circle