Showing an alert from within a button handler.

Hi all,

I was wondering if it was a good idea to show an alert from within a button handler.

Basically, I have a “play level” button. In the button handler I check if the player has enough coins to play this level. If yes then I deduct the coins and goto the level.

But if the user doen’t have enough coins, which of the following is better?

local function handleButtonEvent( event ) if ( "ended" == event.phase ) then if (event.target.id == "play\_level\_btn") then if coins \> 0 then coins = coins - 1 composer.gotoScene( "play\_level",scene\_options) else native.showAlert("Warning", "You don't have enough coins!", { "OK", alertListener}) end end end return true end

Or  (only line number 8 is changed)

local function handleButtonEvent( event ) if ( "ended" == event.phase ) then if (event.target.id == "play\_level\_btn") then if coins \> 0 then coins = coins - 1 composer.gotoScene( "play\_level",scene\_options) else timer.performWithDelay( 100, showCoinsAlert) end end end return true end

Thanks for your help.

Instant feedback is better, so I wouldn’t use a delay there, but I would steer clear of using native alerts when notifying about things like that.

Native alerts, at least to me, seem like they don’t really belong to the game, it’s like if the phone or its OS is trying to intervene and tell me something. If the coins are a big part of your game, I’d create a custom made popup using normal display objects for it.

Thanks @XeduR

How would I go about doing a popup? Would I use an overlay scene or is there a better (easier) way?

Found my answer here:

https://forums.coronalabs.com/topic/57265-adding-a-mini-popup-window-with-buttons-on-it/

Thanks again.

No problem.

The easiest I can think of would be to create a simple function that adds one image or even just a simple rectangle, 256 by 128 pixels in size, for instance, and place it to the middle of the screen. Then just add some text on top of it, again centered, and add a simple “OK” button to the bottom centre of the image/rect that will close the popup.

On top of this, I usually add a faded black rectangle behind all of this that covers the rest of the screen. I also add a touch & tap listener to it in order to highlight the popup window and to block any touches through it.

Instant feedback is better, so I wouldn’t use a delay there, but I would steer clear of using native alerts when notifying about things like that.

Native alerts, at least to me, seem like they don’t really belong to the game, it’s like if the phone or its OS is trying to intervene and tell me something. If the coins are a big part of your game, I’d create a custom made popup using normal display objects for it.

Thanks @XeduR

How would I go about doing a popup? Would I use an overlay scene or is there a better (easier) way?

Found my answer here:

https://forums.coronalabs.com/topic/57265-adding-a-mini-popup-window-with-buttons-on-it/

Thanks again.

No problem.

The easiest I can think of would be to create a simple function that adds one image or even just a simple rectangle, 256 by 128 pixels in size, for instance, and place it to the middle of the screen. Then just add some text on top of it, again centered, and add a simple “OK” button to the bottom centre of the image/rect that will close the popup.

On top of this, I usually add a faded black rectangle behind all of this that covers the rest of the screen. I also add a touch & tap listener to it in order to highlight the popup window and to block any touches through it.