Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
3 buttons using classes not working — Gideros Forum

3 buttons using classes not working

HarrisonHarrison Member
edited April 2014 in General questions
Button = Core.class(Sprite)
 
function Button:onMouseUp(event)
if self:hitTestPoint(event.x,event.y) then
print("you clicked me!!")
end
end
function Button:init(sprite)
print("initializing button")
self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
self:addChild(sprite) 
 
end
I make 3 buttons in main.lua using
local bitmap = Bitmap.new(Texture.new("abuttonimage",true))
x=Button.new(bitmap) 
x:setPosition(0,0)
stage:addChild(x)
repeat with y and z, but change position.
now, only z is visible and running. The others initialise the button, but they do not appear or respond.
it works with 1 button, but not 3.
any ideas on why/how to fix it?
“ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill

Comments

  • you should have to do like this
    local bitmap = Bitmap.new(Texture.new("abuttonimage",true))
    x=Button.new(bitmap) 
    x:setPosition(0,0)
    stage:addChild(x)
     
    local bitmap = Bitmap.new(Texture.new("abuttonimage",true))
    y=Button.new(bitmap) 
    y:setPosition(0,0)
    stage:addChild(y)
     
    local bitmap = Bitmap.new(Texture.new("abuttonimage",true))
    z=Button.new(bitmap) 
    z:setPosition(0,0)
    stage:addChild(z)
    as every bitmap can have only one parent and if you try to add them again in different paren then it will first remove from previous parent and then added in new parent and hence visible only one time

    :)
    +1 -1 (+2 / -0 )Share on Facebook
  • @hgvyas123,
    but if you set the position of all the buttons to (0,0) they will all sit on top of each other.

    @Harrison,
    you are passing the function a child that is moved from parent to parent (same instance) as @hgvyas explained. you can try this code as
     function Button:init(theSpriteName)
      local bitmap = Bitmap.new(Texture.new(theSpriteName, true))
      self:addChild(sprite)
      self:addEventListener(... the rest of the code ...)
     end
    and then call it easily as
     x = Button.new("abuttonimage.png")
     y = Button.new("bbuttonimage.png")
     z = Button.new("cbuttonimage.png")
     
     stage:addChild( add the x, the y and the z)
    Or you can as I suggested in my book create a more generic code that looks like
     function Button:init(theSpriteName, xPos, yPos, parent)
      local parent = parent or stage
      local bitmap = Bitmap.new(Texture.new(theSpriteName, true))
      local xPos, yPos = xPos or 0, yPos or 0
      self:addChild(sprite)
      self:addEventListener(... the rest of the code ...)
      self:setPosition(xPos, yPos)
      parent:addChild(self)
     end
    so you can call it all in one go as
     x = Button.new("abuttonimage.png", 0, 0)
     y = Button.new("bbuttonimage.png", 100, 100)
     z = Button.new("cbuttonimage.png", 0, 200)
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+3 / -0 )Share on Facebook
  • ohhh yes Copy Paste error :D

    :)
  • HarrisonHarrison Member
    edited April 2014
    @OZApps
    i am trying to make it work, but when i do
    local start = Button.new("images/button.png", 10, 0)
    in main.lua
    and
    self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    in button.lua it dosent seem to work.. what am i doing wrong?
    “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
  • OZAppsOZApps Guru
    edited April 2014
    @harrison,
    it would help to post your actual code, because there might be something in it that is the issue.

    Secondly, there is no function called self.onMouseUp, so you will have to pass it Button.onMouseUp instead. The rest should be all the same.

    Likes: Harrison

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
  • It works now, i misspelled some variables and got mixed up. thanks!
    “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
  • Currently attempting to make unique code run based on the
    function Button:init(sprite, xPos, yPos, parent) by adding a new variable "name".
    the problem is.. i am not sure how to get the name from init to onmousedown without using global variables.
    my new onmousedown code would look something like :
    unction Button:onMouseDown(event)
    --check if button is on click coordinates
    if self:hitTestPoint(event.x,event.y) then
    if name == "startbutton" then
    --display pressed button
    elseif name == "exit" then 
    --display different pressed button
     end
    end

    My current code:
    --make a sprite class "Button"
    Button = Core.class(Sprite)
     
    --check if button is pressed
    function Button:onMouseDown(event)
    --check if button is on click coordinates
    if self:hitTestPoint(event.x,event.y) then
    print("hi!")
     
    end
    end
     
    --check if button is released
    function Button:onMouseUp(event)
    --check if the button is on click coordinates
    if self:hitTestPoint(event.x,event.y) then
    --debug print
    print("you clicked me!!")
    --make a click noise
    local click= Sound.new("sounds/sfx/clickbutton.wav")
    click:play()
    end
    end
     
    function Button:init(sprite, xPos, yPos, parent)
      local parent = parent or stage
      --load the bitmap
      local bitmap = Bitmap.new(Texture.new(sprite, true))
      --load x any y position or set it to 0
      local xPos, yPos = xPos or 0, yPos or 0
      self:addChild(bitmap)
      self:addEventListener(Event.MOUSE_UP, Button.onMouseUp , self)
      self:addEventListener(Event.MOUSE_DOWN, Button.onMouseDown,self)
      --put the button at the specified x and y
      self:setPosition(xPos, yPos)
      parent:addChild(self)
     end
    “ The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time. ” - Tom Cargill
  • OZAppsOZApps Guru
    edited April 2014
    @harrison,
    have you tried the
    self.name
    x = Button.new("exitImage.png", 10,10)
    x.name = "exit"
    and then in your code of
    onMouseDown
    just check for
    self.name == "exit"
    Hope that helps,

    Likes: Harrison

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.