corona sdk - 场景保留旧物品

问题描述 投票:0回答:1

我正在使用导演,从一个场景移动到另一个场景。我有一个问题,当移动到任何场景时,来自intor的按钮和文本字段仍然在屏幕顶部。

如何在移动到下一个场景之前删除项目(文本字段,来自intro.lua屏幕的btns)?

    enter code here    
-- into.lua
module(..., package.seeall)
function new()
--
-- Project: NativeKeyboard2
--

local widget = require( "widget" )

require("hijacks")

local tHeight       -- forward reference

-------------------------------------------
-- General event handler for fields
-------------------------------------------

-- You could also assign different handlers for each textfield

local function fieldHandler( textField )
    return function( event )
        if ( "began" == event.phase ) then
            -- This is the "keyboard has appeared" event
            -- In some cases you may want to adjust the interface when the keyboard appears.

        elseif ( "ended" == event.phase ) then
            -- This event is called when the user stops editing a field: for example, when they touch a different field

        elseif ( "editing" == event.phase ) then

        elseif ( "submitted" == event.phase ) then
            -- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
            print( textField().text )

            -- Hide keyboard
            native.setKeyboardFocus( nil )
        end
    end
end

-- Predefine local objects for use later
local nameField, phoneField
local fields = display.newGroup()



-------------------------------------------
-- *** Create native input textfields ***
-------------------------------------------

-- Note: currently this feature works in device builds or Xcode simulator builds only (also works on Corona Mac Simulator)
local isAndroid = "Android" == system.getInfo("platformName")
local inputFontSize = 18
local inputFontHeight = 30
tHeight = 30

if isAndroid then
    -- Android text fields have more chrome. It's either make them bigger, or make the font smaller.
    -- We'll do both
    inputFontSize = 14
    inputFontHeight = 42
    tHeight = 40
end

nameField = native.newTextField( 40, 120, 200, tHeight )
nameField.font = native.newFont( native.systemFontBold, inputFontSize )
nameField:addEventListener( "userInput", fieldHandler( function() return nameField end ) ) 

phoneField = native.newTextField( 40, 160, 200, tHeight )
phoneField.font = native.newFont( native.systemFontBold, inputFontSize )
phoneField.inputType = "phone"
phoneField:addEventListener( "userInput", fieldHandler( function() return phoneField end ) ) 


-- Add fields to our new group
fields:insert(nameField)
fields:insert(phoneField)

-------------------------------------------
-- *** Add field labels ***
-------------------------------------------

local defaultLabel = display.newText( "الاسم", 250, 120, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )

local defaultLabel = display.newText( "رقم الجوال", 250, 160, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )

-- -------------------------------------------
-- -- Create a Background touch event
-- -------------------------------------------


local listener = function( event )
    -- Hide keyboard
    print("tap pressed")
    native.setKeyboardFocus( nil )

    return true
end

-- Determine if running on Corona Simulator
--
local isSimulator = "simulator" == system.getInfo("environment")
if system.getInfo( "platformName" ) == "Mac OS X" then isSimulator = false; end

-- Native Text Fields not supported on Simulator
--
if isSimulator then
msg = display.newText( "الرجاء ادخال اسمك ورقم جوالك", 0, 280, native.systemFontBold, 12 )
msg.x = display.contentWidth/2      -- center title
msg:setTextColor( 255,0,0 )
end

-- -- Add listener to background for user "tap"
-- bkgd:addEventListener( "tap", listener )
-- display.remove( obj )
-- obj = nil    

local introGroup = display.newGroup();

    local background = display.newImage("graphics/intro_background.png")        
    local begin = display.newImage("graphics/begin_button.png")
    begin.x = 160; 
    begin.y = 400;
    begin.scene = "menu";

    introGroup:insert(background);
    introGroup:insert(begin);

    begin:addEventListener("touch", changeScene)

return introGroup;

end
corona
1个回答
1
投票

Corona提供了一个非常好的功能,即“故事板”。我给你一个简短的解释,试试这个 -

  • 故事板 - 它是一个场景(例如“屏幕”或“视图”)管理库,为开发人员提供了在Corona SDK应用程序中创建和转换场景模块的简便方法。 语法 - 本地故事板=需要“故事板” 示例 - local scene1 = storyboard.newScene(“场景名称”) 以下是故事板中使用的不同事件 - 1-创建场景 - - 当场景视图不存在时调用: function scene:createScene(event)local group = self.view end 2-输入场景 - - 场景移动到屏幕后立即调用: function scene:enterScene(event)local group = self.view end 3-退出场景 - - 当场景即将移动到屏幕外时调用: function scene:exitScene(event)local group = self.view 结束 4 - 销毁场景 - - 在删除场景的“视图”(显示组)之前调用 function scene:destroyScene(event)local group = self.view end

它会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.