Nov 222011
 

A tutorial and sample showing how to use two rolling dice in your Corona SDK app.

A friend of mine is working on app where something like this could come in handy, so I decided to put together a simple example that he, and others, may find useful.

The code is below, although you will need to download the sample using the link at the bottom of the page to make it work because obviously you will be using my sprite.

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
--Hide the status bar
display.setStatusBar(display.HiddenStatusBar)

--Require sprite
require "sprite"

--Random seed
math.randomseed(os.time())

--The background image
local bg = display.newImage( "bg.png" )

--Total sum of dice after a roll
local diceTotal = 0

--States whether or not the dice may be rolled
local canRoll = true

--Sprite Setup
local diceSheet = sprite.newSpriteSheet( "dice.png", 64, 64)

local diceSet = sprite.newSpriteSet( diceSheet, 1, 6 )
sprite.add( diceSet, "dice", 1, 6, 195, 0)
sprite.add( diceSet, "dice2", 1, 6, 210, 0)

--First Die
local dice = sprite.newSprite( diceSet )
dice.x = 120
dice.y = 200
dice:prepare("dice")

--Second Die
local dice2 = sprite.newSprite( diceSet )
dice2.x = 200
dice2.y = 300
dice2:prepare("dice2")

--Roll button
local rollBtn = display.newImage("die.png")
rollBtn.x = 280
rollBtn.y = 40

--End Roll Function
local function endRoll()
dice:pause()
dice2:pause()
diceTotal = dice.currentFrame + dice2.currentFrame
print(diceTotal) --Print the result in the terminal
canRoll = true --Allow the dice to be rolled again
end

--Roll function
local function rollDice()
if canRoll == true then
canRoll = false --Prevent dice from being rolled again before the current role is over
dice:play()
dice2:play()
randomTime = math.random(1500, 3500)
timer.performWithDelay(randomTime, endRoll, 1)
end
end
rollBtn:addEventListener("tap", rollDice)

Download The Dice Rolling Project Here

Enjoy!

Peach Pellen :)

Like this post? Subscribe to the monthly newsletter!

 

  18 Responses to “Rolling The Dice”

  1. Peach, I sent you an email with my code. Cheers!

  2. Did you download the project so you also have the images?

    This code would not conflict with Director so it’s hard to isolate the error without knowing your project.

  3. Peach, I put this code (altered slightly) into my .lua file, using Director Class, but cannot get my change scene to work….Please help

  4. Thx 4 your reply. ‘See’ you soon then :)

  5. Hey there Mileni,

    I am a tad behind on emails (I often am, I get a lot) but should be caught up by tonight or tomorrow, so you shall hear from me soon.

    Thanks,
    Peach :)

  6. peach, could u please check your email?
    thx :)

  7. Excellent! I’m always pleased to help :)

  8. Thanks again, Peach! This is what I was looking for. I owe you one :-)

  9. @sergiu – you don’t need sprite.lua in your project, you simply require it. (This is built into Corona.) For storyboard, you need to be using one of the latest daily builds. (Are you a paid subscriber? If not you will have to wait until the next stable release to use storyboard.)

    @Joanne – I’m glad to hear it :)

    @Mohamed – That link requires a plugin I don’t have I’m afraid. Is it anything like this; http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-memory-match-game/ ? That’s a good tutorial. (Not one of mine.)

    Peach :)

  10. Thanks for your great tutorials. Your spirit of sharing and giving is exemplary. I hope you will have the time to make a memory picture puzzle tutorial, something like this one

    http://www.crpuzzles.com/mem/mem0013.html

    You could make it one of your template if you wish :-)

    Thanks again Peach!

  11. Nice, nice…..you really give me what I need at the right time!

  12. Hi,

    I have a question about your script. Where are defined the methods for dice , the play(), pause(), because the sprite.lua is not present and I don’t know where to look for them.

    @ertzel I dont found the storyboard.lua in your shared code.
    local storyboard = require “storyboard”

    Thanx for your tuts.

  13. Awesome! Will have to check this out later tonight.

    Thanks for crediting and linking to me in the description too – most appreciated :)

  14. Thanks for this tutorial Peach. I didn’t think it would be this easy to do something like two dice rolling but I guess with Corona I shouldn’t be surprised that it is so easy to accomplish.

    I took this code you shared and did some changes to it and turned the code into a Slot Machine template instead of dice rolling. It uses all of the same code pretty much as your dice rolling but instead of two it has three objects and looks like a slot machine.

    The code can be found here for anyone interested.

  15. Hey catg,

    They actually give different numbers already, although your code does make things more thorough, plus it would make the dice stop at slightly different times.

    Nicely done!

    Peach :)

  16. Thank you for this handy tool, I just made a slight change for each dice to give different numbers.

    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
    --Hide the status bar
    display.setStatusBar(display.HiddenStatusBar)

    --Require sprite
    require "sprite"

    --The background image
    local bg = display.newImage( "bg.png" )

    --Total sum of dice after a roll
    local diceTotal = 0

    --States whether or not the dice may be rolled
    local canRoll = true
    local canRoll_dice = true
    local canRoll_dice2 = true

    --Sprite Setup
    local diceSheet = sprite.newSpriteSheet( "dice.png", 64, 64)

    local diceSet = sprite.newSpriteSet( diceSheet, 1, 6 )
    sprite.add( diceSet, "dice", 1, 6, 195, 0)
    sprite.add( diceSet, "dice2", 1, 6, 210, 0)

    --First Die
    local dice = sprite.newSprite( diceSet )
    dice.x = 120
    dice.y = 200
    dice:prepare("dice")

    --Second Die
    local dice2 = sprite.newSprite( diceSet )
    dice2.x = 200
    dice2.y = 300
    dice2:prepare("dice2")

    --Roll button
    local rollBtn = display.newImage("die.png")
    rollBtn.x = 280
    rollBtn.y = 40

    --End Roll Function
    local function endRoll_dice()
        dice:pause()
        canRoll_dice = true
        if canRoll_dice2 == true then
            canRoll = true
            diceTotal = dice.currentFrame + dice2.currentFrame
            print(diceTotal) --Print the result in the terminal
        end
    end

    local function endRoll_dice2()
        dice2:pause()
        canRoll_dice2 = true
        if canRoll_dice == true then
            canRoll = true
            diceTotal = dice.currentFrame + dice2.currentFrame
            print(diceTotal) --Print the result in the terminal
        end
    end

    --Roll function
    local function rollDice()
        if canRoll == true then
            canRoll = false --Prevent dice from being rolled again before the current role is over
            canRoll_dice = false
            canRoll_dice2 = false
            dice:play()
            dice2:play()
            randomTime_dice = math.random(1500, 3500)
            randomTime_dice2 = math.random(1500, 3500)
            timer.performWithDelay(randomTime_dice, endRoll_dice, 1)
            timer.performWithDelay(randomTime_dice2, endRoll_dice2, 1)
        end
    end
    rollBtn:addEventListener("tap", rollDice)
  17. No worries Rodrigo,

    I had a little time this evening while waiting to hear back from someone about a problem and in that time figured I’d throw something together ;)

    Peach :)

  18. Mrs. Peach!

    WOW! How quick you are you?! ;D

    Thank you VERY MUCH for sharing your high-quality Corona`s knowledge with us! It is awesome for sure! :)

    My best regards,
    Rodrigo RSCdev.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.