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
Likes: ar2rsawseen, Harrison
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
Likes: ar2rsawseen, hgvyas123, Harrison
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
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?
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
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
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 :
My current code:
have you tried the
Likes: Harrison
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