Discord
Login
Community
DARK THEME

Buttons

Does anyone know how to fix this?

  cookies = 0
  initUI
end

update = function()
  updateUI
end

draw = function()
  screen.drawMap("map", x+0, y+0, 400,200)
  screen.drawSprite( "cookie", x-100, y-10, 200, 200)
  screen.drawSprite("chocolatechip", x-170, y+80, 20,20)
  screen.drawSprite("shopimage", x+100, y+85,75,75)
  screen.drawText("Cookies: " + cookies, -75, +10, 30, "#FFF"
  drawUI()
end

I wouldn't know exactly since the code you provided doesn't have much context, but from what I can see:

  1. UpdateUI I assume is a function, so you must include its parentheses if you want to call it, UpdateUI(). Same with initUI()
  2. screen.drawText("Cookies: " + cookies, -75, +10, 30, "#FFF" should have a ) at the end

It's for a remake of cookie clicker. But I don't know why I can't see the button.

Here's the UI code:

  buttons = []
  buttons.push(
    object
      x = -90
      y = -50
      width = 80
      height = 25
      text = "cookie!"
      action = function () score += 1 end
      scale = 1
      alpha = 4
    end
)
end
updateUI = function()
  for button in buttons
    //check hover
    button.scale = 1
    if checkRoundMouseHover(button.x, button.y,
      button.width, button.height) then
      button.alpha = 1
      if mouse.press then
        button.action()
      end
      if mouse.pressed then
        button.scale = 1.1
      end
    else
      button.alpha = 0.7
      end
    end
end

drawUI = function()
  for button in buttons
    screen.setDrawScale(button.scale, button.scale)
    screen.setAlpha(button.alpha)
    screen.fillRoundRect(button.x-100, button.y-10,
      button.width+50, button.height+50,"rgb(255,255,255)")
    screen.drawSprite("button", -100, -10, 90, 90)
    screen.drawText(button.text, button.x-100, button.y-10)
    screen.setDrawScale(1, 1)
    screen.setAlpha(1)
  end
end
    

Well, all that's left is to add initUI = function() at the beginning of the entire text you sent me just now But other than that, everything works fine after I made the changes I mentioned at the beginning, I can see the button, ignore that it looks ugly

I can see the button now but the score doesn't go up. Can you explain why? Because I'm super confused.

Here's the project. https://microstudio.io/i/P1X3L_2012/cookieclicker2/

Well, that's because in the button actions action increases the variable chips and not cookies, which is what is used for points, so in initUI, you must change chips to global.cookies in the line that I indicate (And set it to increase points, that is, += 1)

initUI = function()
  buttons = []
  buttons.push(
    object
      x = -90
      y = -50
      width = 80
      height = 25
      text = "cookie!"
      action = function () chips +=0 end <-- (HERE, Also put += 1)
...

Something like that:

initUI = function()
  buttons = []
  buttons.push(
    object
      x = -90
      y = -50
      width = 80
      height = 25
      text = "cookie!"
      action = function () global.cookies +=1 end
      scale = 1
      alpha = 1
    end
)
end

This is because in the line where it draws the number of clicks or cookies, it uses the variable cookies

draw = function()
  screen.clear()
  screen.drawMap("map", x+0, y+0, 400,200)
  screen.drawSprite("basicshopbutton", x+100, y+35, 200,125)
  screen.drawSprite("basicshopbutton", x+100, y-20, 200,125)
  screen.drawSprite("basicshopbutton", x+100, y-75, 200,125)
  screen.drawSprite("chocolatechip", x-150, y+80, 25,25)
  screen.drawSprite("shopimage", x+100, y+85,75,75)
  screen.drawText("Chips: " + cookies, -75, +80, 30, "#FFFFFF") <-- (HERE)
...

Another solution is to change the cookies to chips and set it to change global.chips, since if you don't set it to global, it won't modify it

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community