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

  • SEARCH
  • Home
  • 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 6620587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:07:15+00:00 2026-05-25T21:07:15+00:00

In CoronaSDK, I’m trying to make a certain code that creates a lot of

  • 0

In CoronaSDK, I’m trying to make a certain code that creates a lot of buttons and plays sounds for each of those buttons more “efficient” by first creating a table of strings and then creating the buttons and handlers by iterating over the table. I’m using the common “ui.lua” file to create buttons. However, I get (at least) two errors. The first is that the newButton fcn says it’s expecting a table value for “default.” Here’s the code:

--create the array.  Note that these names will be used for all the objects that refer to these things, so you must have a consistent naming convention
local instruments  = {"snare","piano1", "piano2","guitar1","guitar2","softPiano"}
--set constants to be able to set the x value of the buttons
local h = display.contentWidth/6-25;
local multiplier = 1
--loop through each item in the array to: (a) load the sound, (b) create a btn press event, and (c) create the button 
for k,v in pairs(instruments) do 
    --first, we use the value to make some reference strings
    img = "images/"..v.."_btn.png" 
    sound = "media/"..v..".wav"

    --now create the event listener
    local playThis = function (event)
        audio.play(sound)
    end
    --now create the button
    local thisInstrument = ui.newButton{
        default = img,
        onPress = playThis
    }
    thisInstrument.x = h*multiplier
    thisInstrument.y = display.contentHeight * .8
    multiplier = multiplier + 1
end

When I change the value of default to a straight up string, the buttons are at least created and displayed as expected across the screen.

 local thisInstrument = ui.newButton{
            default = "images/snareDrum_btn.png",
            onPress = playThis
        }

Of course, the sound still doesn’t play when I click a button. So, two questions: First, why won’t a simple reference to default = img work? Second, how do I get the listener to work for each new button?

  • 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-05-25T21:07:16+00:00Added an answer on May 25, 2026 at 9:07 pm

    Ok, so I found the answer. I made a couple of stupid rookie mistakes. The reason the local variable wasn’t working had nothing to do with my code. It was actually because I had misnamed a couple of the image files (doh!).

    Second, the button listener wasn’t working because (drumroll…) I had forgotten to load the audio files ahead of time (double doh!). I ended up deciding to place the playThis function outside the loop so that it wasn’t being recreated unnecessarily. Instead, I set a “sound” property on the created buttons and used the event argument to figure out which button was pushed and play the corresponding sound.

    Here’s the final code (I’ve modularized it to make it more re-usable). Hopefully, this’ll be a lesson for others in checking your entire environment before banging your head about your code.

    --init globals
    _H = display.contentHeight;
    _W = display.contentWidth;
    
    
    --test to see if I can more efficiently write code to do a repeated action by placing items inside an array and looping through the array
    --import the ui file to create buttons
    local ui = require("ui")
    
    --create the array.  Note that these names will be used for all the objects that refer to these things, so you must have a consistent naming convention
    local instruments  = {"snare","piano1", "piano2","guitar1","guitar2"}
    --set constants to be able to set the x value of the buttons
    
    local function doThis (event)
        audio.play(event.target.property)
    end
    
    --loop through each item in the array to: (a) load the sound, (b) create a btn press event, and (c) create the button (the event must be created before the listener, else it has nothing to listen for)
    local function makeBtns(btnList,btnImg,property,groupXPos,groupYPos)
        --first, let's place all the buttons inside a button group, so we can move them together
        local thisBtnGroup = display.newGroup();
        for index,value in ipairs(btnList) do 
            --first, we use the value to make some reference strings
            local img = "images/base_btn.png" 
            property = audio.loadSound("sounds/"..value..".wav")
            --now create the button
            local thisBtn = ui.newButton{
                default = img,
                onPress = doThis,
                text = value,
                size = 10
            }
            thisBtnGroup:insert(thisBtn)
            thisBtn.x = (index -1) * thisBtn.width
            thisBtn.property = property
        end
        thisBtnGroup.x = groupXPos; thisBtnGroup.y = groupYPos
        return thisBtnGroup
    end
    
    local myBand = makeBtns(instruments,"images/base_btn.png","sound",_W/3,_H-50)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a sprite dance according to the beat of a
I'm using Corona SDK to make a simple app that requires luasocket. Corona SDK
We're using objects that do not necessarily need to leverage the physics engine, but
I've scaled my an object by .99 every frame for a certain amount of
I installed the Corona SDK on MacOS. Now I'm trying to open a .lua
I have started my first game in corona SDK and have covered a lot
I'm trying to get a handle on how OOP is done in Lua, and
This is the code (corona SDK btw), it is calling a physicsdata (unimportant). r
I've been trying to write out a custom tick using the Corona SDK (using
I started learning about lua language to develop games,i percept that most people uses

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.