Performance impact: widget button vs image button

When I read Rob’s post about a single listener, this is what I understand it to mean.

(Following written using SSK: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/easy_interfaces/#button-factories))

local onPush( event ) local target = event.target print( "Pressed button with ID: ", target.id ) end local tmp = ssk.easyIFC:presetPush( nil, "default", 100, 100, 80, 40, "Button 1", onPush ) tmp.id = 1 local tmp = ssk.easyIFC:presetPush( nil, "default", 100, 150, 80, 40, "Button 2", onPush ) tmp.id = 2 local tmp = ssk.easyIFC:presetPush( nil, "default", 100, 200, 80, 40, "Button 3", onPush ) tmp.id = 3 local tmp = ssk.easyIFC:presetPush( nil, "default", 100, 250, 80, 40, "Button 4", onPush ) tmp.id = 4

I will reply soon, I’m not in front of my computer right now. Thanks!

Sorry for the late reply.
What I would like to do is be able to create the 36 buttons in a function, loop or table, I think it could help me with the App performance and help improve code.
Here is an image. This is a simple 6 button example, mi App have the same 3 buttons columns but with 12 rows = 36 different buttons. All buttons are inside scrollview, I’m using “display.newImageRect” with a touch listener. No over file, just a simple image.

Hi.  What is the question for this last post?

Are you saying you want someone to show you how to make a button builder function, where you can simply pass in details like:

  • image
  • position <x,y>
  • listener to call when touched
  • (optionally) size of button <width, height>

I’m sure I have something like that already in my free stuff.  I’ll see if something pops out and post back if I see it, but you may have to dig through my archive:

https://github.com/roaminggamer/RG_FreeStuff/

Dude.  :huh:  You need to look at the answers I give you more closely.

I already gave you code for this in another answer:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2018/05/dodi.zip

Look at the code again and pay particular attention to easyButton.lua

Hi roaminggamer, I’m trying to understand the way you code but to be honest you are in a very high level for my current knowledge. I have no experience with code, just Corona api, I’m reading lua.org trying to understand it in a better way …thanks again I will look and keep trying to understand. What I am looking for from you is a basic explanation that can guide me on how to do things.

I see.  This is a two part answer…

One

I think you’d well served reading the tutorials and guides here.

In this context, these specifically:

Two

Let me give you some examples that build up into a solution.

The Basic Touch Listener

-- -- This listener assumes you want to call a function when lifting your finger. -- It doesn't handle highlighting the button or any other visual effects. -- It also doesn't handle sliding your finger off the button and lifting your finger. -- It assumes the object has a function called 'onRelease' -- It is very basic and not good for usability, but it will do the basics. -- local function onTouch( self, event ) if( event.phase == "ended" ) then self.onRelease() end return false end 

A Button Builder That Uses The Listener

local function createPushButton( x, y, width, height, imgPath, onRelease ) local button = display.newRect( imgPath, width, height ) button.x = x button.y = y button.onRelease = onRelease button.touch = onTouch button:addEventListener("touch") return button end

Making A Module

Now, lets take all of the code above an put it  a module to make it portable and easy to use.

-- simpleButton.lua local m = {} local function onTouch( self, event ) if( event.phase == "ended" ) then self.onRelease( self ) end return false end function m.createPushButton( x, y, width, height, imgPath, onRelease ) local button = display.newRect( imgPath, width, height ) button.x = x button.y = y button.onRelease = onRelease button.touch = onTouch button:addEventListener("touch") return button end return m

Using The Module

local simpleButton = require "simpleButton" local function myListener( self ) print( "Just pressed button ID: ", self.id ) end local tmp = simpleButton( 100, 100, 100, 30, "image1.png", myListener ) tmp.id = 1 local tmp = simpleButton( 100, 150, 100, 30, "image2.png", myListener ) tmp.id = 2 local tmp = simpleButton( 100, 200, 100, 30, "image3.png", myListener ) tmp.id = 3

Pre-emptive post…

If anyone sees this:

self.onRelease( self )

and feels I should have showed him this:

self:onRelease()

Feel free to explain why.  I wanted to avoid the dot versus colon discussion to keep the response as easy as possible to understand.

@roaminggamer Thank you very much. You have given me a super answer. I will put everything on my part to understand, practice and apply in my App.