Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This stumped me till today. The main problem is that you're making newdice a Corona display.newText object and then reassigning it to be a dice object. All the Corona objects act like ordinary tables, but they're actually special objects. So you have two options:</p> <p>A. Don't use classes and OOP. As your code is now, there's no reason to have dice be a class. This is the option I'd go with unless you have some compelling reason to make dice a class. Here's how you would implement this option</p> <pre><code>--dice not a class-- local dice = {} local function taps(event) event.target.b = false print("works") end function dice.new( posx, posy) -- constructor local a=math.random(1,6) --local b= true local newdice = {} newdice = display.newText(a, display.contentWidth*posx, display.contentHeight*posy, nil, 60) newdice:addEventListener("tap", taps) newdice.b = true return newdice end </code></pre> <p>or B. Use a "has a" relationship instead of an "is a" relationship for the display object. Since you can't make them both a dice object and a display object, your dice object could contain a display object. Here's how that would look.</p> <pre><code>--dice class-- local dice = {} local dice_mt = { __index = dice } -- metatable local function taps(event) event.target.b = false print("works") end function dice.new( posx, posy) -- constructor local a=math.random(1,6) --local b= true local newdice = {} newdice.image = display.newText(a, display.contentWidth*posx, display.contentHeight*posy, nil, 60) newdice.image:addEventListener("tap", taps) newdice.b = true return setmetatable( newdice, dice_mt ) end </code></pre> <p>There were some other problems as well. In your taps function event handler you have to use event.target.b instead of self.b. Also, in dice.new b is a local variable so it's not a member of your dice class.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload