Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

i want to display different names instead of numbers when i tap everytime in example balloon app
Started by rajatrocks2 Jun 06 2018 12:22 AM

2 replies to this topic
display.newtext newbie creating first app
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

rajatrocks2

[GLOBAL: userInfoPane.html]
rajatrocks2
  • Observer

  • 5 posts
  • Corona SDK

guys i'm a newbie but i have experimented a lot and i'm able to change one name(string) to another name by using (object.text) but how do i change different name everytime i tap on balloon? 

 

say i have have a list of 5 names: john, ruby, bob, rob , mika

 

what code or  api or condition or table do i use to get a different name everytime i press balloon.

 

plz help

 



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,969 posts
  • Corona SDK

Easy, put this code in a file called shuffleBag.lua

local function shuffle = ( t, iter )
   local iter = iter or 1
   local n

   for i = 1, iter do
      n = #t 
      while n >= 2 do
         -- n is now the last pertinent index
         local k = math.random(n) -- 1 <= k <= n
         -- Quick swap
         t[n], t[k] = t[k], t[n]
         n = n - 1
      end
   end

   return t
end

local m = {}
function m.newBag( entries )
   local data = {}
   for k,v in pairs( entries ) do
   data[#data+1] = v
   --
   shuffle( data, #data )
   --
   local index = 1
   local bag = {}
   function bag.get()
      local retVal = data[index]
      index = index + 1
      if( index > #data ) then
         shuffle(data)
         index = 1
      end
      return retVal
   end
   --
   return bag
end
return m

Now use it anywhere like this:

local shuffleBag = require "shuffleBag"


local stuff= { "bob", "bill", "sue", "danny", "sarah" }

local bag = shuffleBag.new( stuff }

for i = 1, 10 do
   print( i, bag.get() )
end



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,969 posts
  • Corona SDK

Or... you could skip all that and use my more sophisticated ShuffleBag as part of SSK2.
 
1. Get SSK2: https://github.com/roaminggamer/SSK2
Docs: https://roaminggamer.github.io/RGDocs/pages/SSK2/

2. Install: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2

3. Use SSK2 ShuffleBag: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/shufflebag/
 
local bag = ssk.shuffleBag.new( "bob", "bill", "sue", "danny"  )

-- also has insert()
bag:insert("sarah")

-- Shuffle once
bag:shuffle()

-- Use
for i = 1, 10 do
   print( i, bag:get() )
end

Edited by roaminggamer, 06 June 2018 - 07:58 AM.



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: display.newtext, newbie, creating first app