So I have setup two files. Card.lua and Main.lua
What my question is, 1. Is this a good design? 2. How can I pass instance variables to the event listeners I have setup. As shown, I’m trying to read variables setup in Card.new hello, card_is_showing, clickListener and myImage. I want to read them as instance variables in the event_listeners. It did work for onObjectTouch, why i don’t know. For onObjectTap, it doesn’t find the variable, i tried to put both card_is_showing, event.card_is_showing and event.target.card_is_showing, but it doesn’t work. Any help is appreciated.
Card = {} function Card.new(sprite, x, y, dragListener) hello = false card\_is\_showing = true clickListener = true myImage = display.newImage( sprite ) myImage.x = x myImage.y = y -- Add the mouse event listener. if dragListener == true then myImage.touch = onObjectTouch myImage:addEventListener( "touch", myImage ) end if clickListener == true then myImage.tap = onObjectTap myImage:addEventListener( "tap", myImage ) end return myImage end function onObjectTouch( self, event ) print( "onObjectTouch" ) if event.phase == "ended" then event.target.hello = false elseif event.phase == "began" then event.target.hello = true elseif event.phase == "moved" then if event.target.hello == true then event.target.x = event.x event.target.y = event.y end end end function onObjectTap( self, event ) if event.card\_is\_showing == true then event.target.isVisible = false event.card\_is\_showing = false end end
And the main file is
local card = require "Card" local deck = card.new("sprites/cardBack\_red5.png", -350, 409, false) ...