Jump to content

GUI Question


Parker

Recommended Posts

-- Вызывается при подключении к игре
function OnPlayerSpawned(player)
    -- Определяем размер панельки
    local panelWidth = 200
    local panelHeight = 100
    local button = ISButton:new(0, 0, 100, 30, "Открыть", nil, onButtonClick)
    local panel = ISPanel:new(0, 0, panelWidth, panelHeight)
    panel:addChild(button)
    
    -- Функция-обработчик нажатия кнопки
    function onButtonClick(button)
      -- Создаем и отображаем окно со списком кнопок
      local menu = ISContextMenu:new(0, 0, 0, 0)
      menu:addOption("Статистика", nil) -- TODO: Добавить обработчик нажатия
      menu:addOption("Опции", nil) -- TODO: Добавить обработчик нажатия
      menu:addOption("Выход", nil) -- TODO: Добавить обработчик нажатия
      menu:setX(getCore():getScreenWidth() - menu:getWidth())
      menu:setY(getCore():getScreenHeight() - menu:getHeight())
      menu:addToUIManager()
    end
    
    -- Добавим панель на интерфейс игрока
    local player = getPlayer()
    if player then
      player:getInventory():addChild(panel)
      panel:setX(100)
      panel:setY(getCore():getScreenHeight() - panelHeight - 1)
    end

  end
  
  -- Вызывается при нажатии на кнопку

  
  -- Вызываем функцию при подключении к игре
  Events.OnPlayerSpawned.Add(OnPlayerSpawned)


 

Why doesn't the code create a button that I did wrong?There are no errors in the console

Link to comment
Share on other sites

It seems that the issue is with the placement of the `onButtonClick` function. It should be defined before it is used in the `ISButton:new()` function. Also, you need to make sure that the `ISButton` and `ISPanel` classes are properly imported. Here's the corrected code:

```lua
-- Make sure to import ISButton and ISPanel classes
require "ISUI/ISButton"
require "ISUI/ISPanel"

-- Вызывается при подключении к игре
function OnPlayerSpawned(player)
    -- Функция-обработчик нажатия кнопки
    local function onButtonClick(button)
        -- Создаем и отображаем окно со списком кнопок
        local menu = ISContextMenu:new(0, 0, 0, 0)
        menu:addOption("Статистика", nil) -- TODO: Добавить обработчик нажатия
        menu:addOption("Опции", nil) -- TODO: Добавить обработчик нажатия
        menu:addOption("Выход", nil) -- TODO: Добавить обработчик нажатия
        menu:set

X(getCore():getScreenWidth() - menu:getWidth())
        menu:setY(getCore():getScreenHeight() - menu:getHeight())
        menu:addToUIManager()
    end

    -- Определяем размер панельки
    local panelWidth = 200
    local panelHeight = 100
    local button = ISButton:new(0, 0, 100, 30, "Открыть", nil, onButtonClick)
    local panel = ISPanel:new(0, 0, panelWidth, panelHeight)
    panel:addChild(button)

    -- Добавим панель на интерфейс игрока
    local player = getPlayer()
    if player then
        player:getInventory():addChild(panel)
        panel:setX(100)
        panel:setY(getCore():getScreenHeight() - panelHeight - 1)
    end
end

-- Вызываем функцию при подключении к игре
Events.OnPlayerSpawned.Add(OnPlayerSpawned)
```

I have moved the `onButtonClick` function definition to the beginning of the `OnPlayerSpawned` function and added the `require` statements for the `ISButton` and `ISPanel`classes. This should create the button as expected. Make sure that the `ISButton` and `ISPanel` classes are available in your project.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...