Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Showing an alert from within a button handler.
Started by ammar71 Aug 13 2019 06:09 AM

4 replies to this topic
showalert button handler

Best Answer XeduR @Spyric , 13 August 2019 - 06:24 AM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 54 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

XeduR @Spyric

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

  • 934 posts
  • Corona SDK

  Best Answer

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.


  • ammar71 likes this

[TOPIC: post.html]
#3

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 54 posts
  • Corona SDK

Thanks @XeduR

 

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



[TOPIC: post.html]
#4

ammar71

[GLOBAL: userInfoPane.html]
ammar71
  • Enthusiast

  • 54 posts
  • Corona SDK

Found my answer here:

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

 

Thanks again.



[TOPIC: post.html]
#5

XeduR @Spyric

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

  • 934 posts
  • Corona SDK

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.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: showalert, button handler