我认为这很简单但我已经在网上试图解决它好几天了。
我的目标是有一个按钮,弹出一个小弹出窗口,然后可以用来为原始按钮选择一个新图像。我希望这样做的方法是将原始按钮精灵框架设置为composer.getVariable(“buttonState”),但我不知道如何传回命令来更新原始精灵。
目前在main.lua我正在使用
local composer = require( "composer" )
composer.setVariable("buttonState", 1 )
composer.gotoScene( "scene_game")
然后在scene_game.lua中设置精灵:
local composer = require( "composer" )
--calling up sprite.lua library
local sprite = require( "sprite" )
--setting up sprite sheet
local sheet1 = sprite.newSpriteSheet( "itemSlot.png", 72, 72 )
---spriteSheet, startFrame, frameCount
local spriteSet1 = sprite.newSpriteSet(sheet1, button1state, 4)
--Slot 1
--spriteSet, sequenceName, startFrame, frameCount, time, [loopParam]
sprite.add( spriteSet1, "green", composer.getVariable("buttonState" ), 4)
local slot1 = sprite.newSprite( spriteSet1 )
slot1.x = 72*6 --button location left/right
slot1.y = 72*5 --button location up/down
--Bring up Item slot Selection window
slot1.dropDown=function(event)
if(event.phase == "ended") then
composer.showOverlay( "scene_dropDown", options )
end
end
slot1:addEventListener("touch",slot1.dropDown)
在scene_dropDown.lua
中local composer = require( "composer" )
local scene_game = require( "scene_game" )
local sprite = require( "sprite" )
--setting up sprite sheet
local sheet1 = sprite.newSpriteSheet( "itemSlot.png", 72, 72 )
---spriteSheet, startFrame, frameCount
local spriteSet2 = sprite.newSpriteSet(sheet1, 1, 4)
--Item option 1
--spriteSet, sequenceName, startFrame, frameCount, time, [loopParam]
sprite.add( spriteSet2, "green", 1,4)
local item1 = sprite.newSprite( spriteSet2 )
item1.x = display.contentCenterX+36 --button location left/right
item1.y = display.contentCenterY-36 --button location up/down
item1:setFrame( 2 )
--Item option 1 functions
function state1Cb( event )
display.remove(item1)
composer.setVariable("buttonState", 2 )
item1:removeEventListener( "tap", state1Cb )
timer.performWithDelay( 1, addListenerA ) -- Do this instead
return true
end
--Item option 1 listener
function addListener1()
item1:addEventListener( "tap", state1Cb )
end
--Finishing up the listeners
addListener1()
return scene
这是代码的缩减版本,只有一个选项。
答案 0 :(得分:0)
在showOverlay()中,事件表上应该有一个父节点对象的参数。如果你向场景对象/表添加函数或变量,那么在showOverlay()的create或show函数中你可以这样做:
local parentScene = event.parent
parentScene.doSomething()
parentScene.someValue = 10
等