Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:46:15+00:00 2026-06-09T14:46:15+00:00

The main creates a simple 2d array. Now i want to create a addeventlistener

  • 0

The main creates a simple 2d array. Now i want to create a addeventlistener for each object in the table. I presume i do this in the class? Although i have created a taps function and then defined addeventlistener but i ma getting errors.

--main.lua--
grid={}
for i =1,5 do
grid[i]=  {}
for j =1,5 do

        grid[i][j]=diceClass.new( ((i+2)/10),((j+2)/10))
    end
end
--dice class--
local dice = {}
local dice_mt = { __index = dice } -- metatable


function dice.new( posx, posy) -- constructor
local a=math.random(1,6)
local b= true
local newdice = display.newText(a, display.contentWidth*posx,
    display.contentHeight*posy, nil, 60)
--newdice:addEventListener("tap", taps(event))

return setmetatable( newdice, dice_mt )
end


function dice:taps(event)
self.b = false
print("works")
end
function dice:addEventListener("tap", taps(event))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:46:16+00:00Added an answer on June 9, 2026 at 2:46 pm

    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:

    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

    --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
    

    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.

    --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
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create simple app able to edit images. Main view of app
I've got m2m relationship like this: #main table CREATE TABLE products_product ( id integer
I have my main class PayUnit where I create my main object references, such
hi i have create one simple media player.... now its working fine. i retrieve
I created a simple login system using sql It has 4 main components index
I have created a very simple GUI project in Qt as follows: main: #include
I am writing multi-threaded python application. The main thread create a Thread pool of
Often I create Child threads within the main() as Thread thread = new Thread(new
I have created an array Man: public main blah blah{ man = man[10]; }
I'm new to cakePHP.I just tried to create a simple form with a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.