Alert

Features demonstrated:

Native alert dialog.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
-- Handler that gets notified when the alert closes
local function onComplete( event )
        print( "index => ".. event.index .. "    action => " .. event.action )
 
        local action = event.action
        if "clicked" == event.action then
                if 2 == event.index then
                        -- Open url if "Learn More" was clicked by the user
                        system.openURL( "http://developer.anscamobile.com" )
                end
        elseif "cancelled" == event.action then
                -- our cancelAlert timer function dismissed the alert so do nothing
        end
end
 
-- Show alert
local alert = native.showAlert( "Corona", "Dream. Build. Ship.", { "OK", "Learn More" }, onComplete )
 
-- Dismisses alert after 10 seconds
local function cancelAlert()
        native.cancelAlert( alert )
end
 
timer.performWithDelay( 10000, cancelAlert )
 
 
--------------------------------------------------------------------------------
-- Below is a simplified version of the Fishies sample code.
-- It's here just to show you that animation continues 
-- when the native alert is displayed.
--------------------------------------------------------------------------------
 
 
-- Seed randomizer
local seed = os.time();
math.randomseed( seed )
 
-- Background
local halfW = display.contentWidth / 2
local halfH = display.contentHeight / 2
 
local backgroundPortrait = display.newImage( "aquariumbackgroundIPhone.jpg", 0, 0 )
 
-- Fishies
local numFish = 10
local file1 = "fish.small.red.png"
local file2 = "fish.small.blue.png"
 
-- Define touch listener for fish so that fish can behave like buttons.
-- The listener will receive an 'event' argument containing a "target" property
-- corresponding to the object that was the target of the interaction.
-- This eliminates closure overhead (i.e. the need to reference non-local variables )
local buttonListener = function( event )
        local group = event.target
 
        -- tap only triggers change from original to different color
        local topObject = group[1]
 
        if ( topObject.isVisible ) then
                local bottomObject = group[2]
 
                -- Dissolve to bottomObject (different color)
                transition.dissolve( topObject, bottomObject, 500 )
 
                -- Restore after some random delay
                transition.dissolve( bottomObject, topObject, 500, math.random( 3000, 10000 ) )
        end
 
        -- we handled it so return true to stop propagation
        return true
end
 
 
-- Create a table to store all the fish and register this table as the 
-- "enterFrame" listener to animate all the fish.
local bounceAnimation = {
        container = display.getCurrentStage(),
        reflectX = true,
}
 
-- Add fish to the screen
for i=1,numFish do
        -- create group which will represent our fish, storing both images (file1 and file2)
        local group = display.newGroup()
 
        local fishOriginal = display.newImage( file1 )
        group:insert( fishOriginal, true )  -- accessed in buttonListener as group[1]
 
        local fishDifferent = display.newImage( file2 )
        group:insert( fishDifferent, true ) -- accessed in buttonListener as group[2]
        fishDifferent.isVisible = false -- make file2 invisible
 
        -- move to random position in a 200x200 region in the middle of the screen
        group:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) )
 
        -- connect buttonListener. touching the fish will cause it to change to file2's image
        group:addEventListener( "touch", buttonListener )
 
        -- assign each fish a random velocity
        group.vx = math.random( 1, 5 )
        group.vy = math.random( -2, 2 )
 
        -- add fish to animation group so that it will bounce
        bounceAnimation[ #bounceAnimation + 1 ] = group
end
 
-- Function to animate all the fish
function bounceAnimation:enterFrame( event )
        local container = self.container
        local containerBounds = container.stageBounds
        local xMin = containerBounds.xMin
        local xMax = containerBounds.xMax
        local yMin = containerBounds.yMin
        local yMax = containerBounds.yMax
 
        local reflectX = nil ~= self.reflectX
        local reflectY = nil ~= self.reflectY
 
        -- the fish groups are stored in integer arrays, so iterate through all the 
        -- integer arrays
        for i,v in ipairs( self ) do
                local object = v  -- the display object to animate, e.g. the fish group
                local vx = object.vx
                local vy = object.vy
 
                -- TODO: for now, time is measured in frames instead of seconds...
                local dx = vx
                local dy = vy
 
                local bounds = object.stageBounds
 
                local flipX = false
                local flipY = false
 
                if (bounds.xMax + dx) > xMax then
                        flipX = true
                        dx = xMax - bounds.xMax
                elseif (bounds.xMin + dx) < xMin then
                        flipX = true
                        dx = xMin - bounds.xMin
                end
 
                if (bounds.yMax + dy) > yMax then
                        flipY = true
                        dy = yMax - bounds.yMax
                elseif (bounds.yMin + dy) < yMin then
                        flipY = true
                        dy = yMin - bounds.yMin
                end
 
                if ( flipX ) then
                        object.vx = -object.vx
                        if ( reflectX ) then object:scale( -1, 1 ) end
                end
                if ( flipY ) then
                        object.vy = -object.vy
                        if ( reflectY ) then object:scale( 1, -1 ) end
                end
 
                object:translate( dx, dy )
        end
end
 
Runtime:addEventListener( "enterFrame", bounceAnimation );