Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Button and changing Default action (function)
Started by GrapAp Oct 24 2018 02:48 AM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

GrapAp

[GLOBAL: userInfoPane.html]
GrapAp
  • Observer

  • 4 posts
  • Corona SDK

Hi Everyone,

 

I am new with Corona and programming with lua ;)

 

I have one little problem I have created a button like this

 

 

and used options = {... } with defautl event = handleEventButton action

 

and later I try to change this event

 

button1.onEvent = ActionNumberTwo

 

but it doesn't work (but works for properties, not methods)

 

I was wondering if I can change options (object or maybe in lua table for update these properties) ?

 

The reason why I want to do this is that I do have a few buttons with the same properties but only onEvent method is different, so I didn't want to repeat so many code



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 293 posts
  • Corona SDK

Hello and welcome!

In the future, try to be a little more descriptive. It is always better to have your problem written out as clearly as possible instead of trying to truncate it. I mean, you didn't even take the time to write out how you created that button or what the options were.  ;)

 

You can create buttons via a function to make things easier. Here's a simple example:

 

local widget = require( "widget" )

local function buttonSample(x,y,height,width,id,onReleaseFunction)
	local buttonVar = widget.newButton{
		width = width,
		height = height,
		x = x,
		y = y,
		id = id,
		shape = "rect",
		fillColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
		onRelease = onReleaseFunction,
	}
	return buttonVar
end

local button = buttonSample(x,y,height,width,id,onReleaseFunction)

I have created a buttonModule.lua that I use to create any and all widget.newButton objects for my projects, so that don't need to repeat any code. :P


  • roaminggamer likes this

[TOPIC: post.html]
#3

GrapAp

[GLOBAL: userInfoPane.html]
GrapAp
  • Observer

  • 4 posts
  • Corona SDK

Yes, but for my first project, this solution probably won't save many lines of code because I want to initialize all buttons on first site (only one) of the application so anyway I need to put parameters like x,y, height .. and so on. What I wanted to do is get all the same options in one table and put (pass) it to the function later. Only one of this parameters would be added separately and it would be a method --> onRelease, and of course this works except adding separately method or rewriting method later for a new one doesn't work (after initializing a button).

 

Anyway thanks for the reply I will use onRelease instead onEvent, it will be easier a little bit ;)



[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,860 posts
  • Corona Staff

The widget.newButton() API does not support changing the handler functions after the fact. The source for widgets is open source and you could download it from our GitHub repository and add those features yourself.

 

https://github.com/coronalabs/framework-widget

 

Most people would give their button an .id value (set in the constructor) that can be accessed inside the button event listener function and have a single handler function that uses an "if" statement to provide additional context.

 

Here is an example:

local function handleButtonEvent( event )
	print("button", event.target.id)
	if event.target.id == "home" then
		composer.gotoScene( "scenes.menu", { effect = "crossFade", time = 500 })
	elseif event.target.id == "replay" then
		composer.removeScene( "scenes.newgame" )
		composer.gotoScene( "scenes.newgame", { effect = "crossFade", time = 500 })
	elseif event.target.id == "play" then
		composer.removeScene( "scenes.campaignselect" )
		composer.gotoScene( "scenes.campaignselect", { effect = "crossFade", time = 500 })
	elseif event.target.id == "leaderboard" then
		gameNetworkingLeaderboards()
	end
	return true
end

-- example button
	local playButton = widget.newButton({
		width = 40,
		height = 36,
		defaultFile = "images/playButton_up.png",
		overFile = "images/playButton_down.png",
		id = "play",
		onRelease = handleButtonEvent
		})
	sceneGroup:insert( playButton )


[TOPIC: post.html]
#5

GrapAp

[GLOBAL: userInfoPane.html]
GrapAp
  • Observer

  • 4 posts
  • Corona SDK

I didn't know that ;)

I think this problem can be resolved now, I have checked that this id can change later after declaration and those changes are passed to eventhandler function, so now I will create all buttons in the same manner in for in loop and after that, I will change id for those buttons.

 

Thanks to your code I can have one eventHandler function and based on id different action can be performed ;)




[topic_controls]
[/topic_controls]