Math.random, Storyboard And Functions

if chance1 == 1 then local function mini\_facebook2 ( self, event )     if event.phase == "began" then         system.openURL( "WEBSITE 1" )          return true     end end local function mini\_twitter2 ( self, event )     if event.phase == "began" then         system.openURL( "WEBSITE 2" )               return true     end end end if chance1 == 2 then local function mini\_facebook2 ( self, event )     if event.phase == "began" then         system.openURL( "WEBSITE 3" )         return true     end end local function mini\_twitter2 ( self, event )     if event.phase == "began" then         system.openURL( "WEBSITE 4" )            return true     end end end function scene:createScene( event )     local screenGroup = self.view          chance1 = math.random (1,2)     print(chance1)     mini\_facebook = display.newImage("levelfailed\_mini\_facebook.png")     mini\_facebook.x = display.contentWidth /10     mini\_facebook.y = display.contentHeight / 1.3     screenGroup:insert( mini\_facebook )     mini\_facebook.touch = mini\_facebook2     mini\_facebook:addEventListener( "touch", mini\_facebook )          mini\_twitter = display.newImage("levelfailed\_mini\_twitter.png")     mini\_twitter.x = display.contentWidth /4.45     mini\_twitter.y = display.contentHeight / 1.3     screenGroup:insert( mini\_twitter )     mini\_twitter.touch = mini\_twitter2     mini\_twitter:addEventListener( "touch", mini\_twitter )          end  

When I press the Facebook or Twiiter button, nothing happens; only works if i remove a 

if chance1 == X then

but then i have only one case. How can I randomly acces a function?

Thank you!

This should do it. If you want to only use one of the URLs each time the scene loads, move ‘local chance1 = math.random(1,2)’ to the top of the file.

[lua]

local urls = {“WEBSITE 1”,“WEBSITE 2”,“WEBSITE 3”,“WEBSITE 4”}

local touchButton = function (event)

    local obj = event.target

    local chance1 = math.random(1,2)

    if event.phase == “began” then

             system.openURL(urls[chance1+obj.id] )

             – 1 & 2 = facebook URL, 3 & 4 = twitter URL

    end

    return true

end

function scene:createScene( event )

    local screenGroup = self.view

    mini_facebook = display.newImage(“levelfailed_mini_facebook.png”)

    mini_facebook.x = display.contentWidth /10

    mini_facebook.y = display.contentHeight / 1.3

    screenGroup:insert( mini_facebook )

    mini_facebook:addEventListener( “touch”, touchButton)

    mini_facebook.id = 0

    mini_twitter = display.newImage(“levelfailed_mini_twitter.png”)

    mini_twitter.x = display.contentWidth /4.45

    mini_twitter.y = display.contentHeight / 1.3

    screenGroup:insert( mini_twitter )

    mini_twitter:addEventListener( “touch”, touchButton)

    mini_twitter.id = 2

end

[/lua]

Thank you for your post, I managed to make it work.

However if i use the same chance1 to create a image in the createScene, it seems to be a problem;

I use the chance1 function to display a quote (there are 11 quotes). it displays a quote in createScene (a image with that quote) and I want that, when the facebook or twitter button is pressed the same quote to be shared on facebook or twitter.

If I display the function above the code, it doesn’t display a new image in createscene if the scene is re-entered :frowning:  . Any idea how to do this? :slight_smile:

Thank you!

Problem solved  :slight_smile: I used a storyboard.variable to generate a random value in another scene, then moved it in this scene. chance1, chance2 etc. get the value of the storyboard.variable.

This should do it. If you want to only use one of the URLs each time the scene loads, move ‘local chance1 = math.random(1,2)’ to the top of the file.

[lua]

local urls = {“WEBSITE 1”,“WEBSITE 2”,“WEBSITE 3”,“WEBSITE 4”}

local touchButton = function (event)

    local obj = event.target

    local chance1 = math.random(1,2)

    if event.phase == “began” then

             system.openURL(urls[chance1+obj.id] )

             – 1 & 2 = facebook URL, 3 & 4 = twitter URL

    end

    return true

end

function scene:createScene( event )

    local screenGroup = self.view

    mini_facebook = display.newImage(“levelfailed_mini_facebook.png”)

    mini_facebook.x = display.contentWidth /10

    mini_facebook.y = display.contentHeight / 1.3

    screenGroup:insert( mini_facebook )

    mini_facebook:addEventListener( “touch”, touchButton)

    mini_facebook.id = 0

    mini_twitter = display.newImage(“levelfailed_mini_twitter.png”)

    mini_twitter.x = display.contentWidth /4.45

    mini_twitter.y = display.contentHeight / 1.3

    screenGroup:insert( mini_twitter )

    mini_twitter:addEventListener( “touch”, touchButton)

    mini_twitter.id = 2

end

[/lua]

Thank you for your post, I managed to make it work.

However if i use the same chance1 to create a image in the createScene, it seems to be a problem;

I use the chance1 function to display a quote (there are 11 quotes). it displays a quote in createScene (a image with that quote) and I want that, when the facebook or twitter button is pressed the same quote to be shared on facebook or twitter.

If I display the function above the code, it doesn’t display a new image in createscene if the scene is re-entered :frowning:  . Any idea how to do this? :slight_smile:

Thank you!

Problem solved  :slight_smile: I used a storyboard.variable to generate a random value in another scene, then moved it in this scene. chance1, chance2 etc. get the value of the storyboard.variable.