Showing variables

Hi guys,
I want to create a variable “money = 0” and to display it,but not only that.
Here is the code:

local Money = display.newText(“Novac:”, 60, -15, nil, 30)

local myRandomNumber1 = math.random(1,5) 

box1 = display.newImage( “Box1.png” )

box1.x = display.actualContentWidth/6

box1.y = display.actualContentHeight/10

box1:scale( 0.5, 0.5 )

box1.numberValue=myRandomNumber1

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box1Text = display.newText({x = box1.x, y = box1.y, text = tostring(box1.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box1:addEventListener( “touch”, onObjectTouch )

local myRandomNumber2 = math.random(1,5) 

box2 = display.newImage( “Box1.png” )

box2.x = display.actualContentWidth/2.6

box2.y = display.actualContentHeight/10

box2:scale( 0.5, 0.5 )

box2.numberValue=myRandomNumber2

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box2Text = display.newText({x = box2.x, y = box2.y, text = tostring(box2.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box2:addEventListener( “touch”, onObjectTouch )

local myRandomNumber3 = math.random(1,5) 

box3 = display.newImage( “Box1.png” )

box3.x = display.actualContentWidth/1.66

box3.y = display.actualContentHeight/10

box3:scale( 0.5, 0.5 )

box3.numberValue=myRandomNumber3

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box3Text = display.newText({x = box3.x, y = box3.y, text = tostring(box3.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box3:addEventListener( “touch”, onObjectTouch )

local myRandomNumber4 = math.random(1,5) 

box4 = display.newImage( “Box1.png” )

box4.x = display.actualContentWidth/1.22

box4.y = display.actualContentHeight/10

box4:scale( 0.5, 0.5 )

box4.numberValue=myRandomNumber4

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box4Text = display.newText({x = box4.x, y = box4.y, text = tostring(box4.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box4:addEventListener( “touch”, onObjectTouch )

local myRandomNumber5 = math.random(1,5) 

box5 = display.newImage( “Box1.png” )

box5.x = display.actualContentWidth/6

box5.y = display.actualContentHeight/4

box5:scale( 0.5, 0.5 )

box5.numberValue=myRandomNumber5

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box5Text = display.newText({x = box5.x, y = box5.y, text = tostring(box5.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box5:addEventListener( “touch”, onObjectTouch )

local myRandomNumber6 = math.random(1,5) 

box6 = display.newImage( “Box1.png” )

box6.x = display.actualContentWidth/2.6

box6.y = display.actualContentHeight/4

box6:scale( 0.5, 0.5 )

box6.numberValue=myRandomNumber6

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box6Text = display.newText({x = box6.x, y = box6.y, text = tostring(box6.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box6:addEventListener( “touch”, onObjectTouch )

local myRandomNumber7 = math.random(1,5) 

box7 = display.newImage( “Box1.png” )

box7.x = display.actualContentWidth/1.66

box7.y = display.actualContentHeight/4

box7:scale( 0.5, 0.5 )

box7.numberValue=myRandomNumber7

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box7Text = display.newText({x = box7.x, y = box7.y, text = tostring(box7.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box7:addEventListener( “touch”, onObjectTouch )

local myRandomNumber8 = math.random(1,5) 

box8 = display.newImage( “Box1.png” )

box8.x = display.actualContentWidth/1.22

box8.y = display.actualContentHeight/4

box8:scale( 0.5, 0.5 )

box8.numberValue=myRandomNumber8

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box8Text = display.newText({x = box8.x, y = box8.y, text = tostring(box8.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box8:addEventListener( “touch”, onObjectTouch )

local myRandomNumber9 = math.random(1,5) 

box9 = display.newImage( “Box1.png” )

box9.x = display.actualContentWidth/6

box9.y = display.actualContentHeight/2.5

box9:scale( 0.5, 0.5 )

box9.numberValue=myRandomNumber9

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box9Text = display.newText({x = box9.x, y = box9.y, text = tostring(box9.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box9:addEventListener( “touch”, onObjectTouch )

local myRandomNumber10 = math.random(1,5) 

box10 = display.newImage( “Box1.png” )

box10.x = display.actualContentWidth/2.6

box10.y = display.actualContentHeight/2.5

box10:scale( 0.5, 0.5 )

box10.numberValue=myRandomNumber10

local function onObjectTouch( event )

    if ( event.phase == “began” ) then

        local box10Text = display.newText({x = box10.x, y = box10.y, text = tostring(box10.numberValue), fontSize = 15, font = native.systemFont })

    end

    return true

end

box10:addEventListener( “touch”, onObjectTouch )

So,I want to,if the box1 is touched and “myRandomNumber1” displayed some number from 1,5,that generated number and displayed to add in my variable money.
Please help  :slight_smile:

Can you add the code into the “<>” that you see on the top in the replying options.? It’s hard to read and understand what you want when it’s not formatted correctly. Thanks!

–SonicX278 

Sure :slight_smile:

local Money = display.newText("Novac:", 60, -15, nil, 30) local myRandomNumber1 = math.random(1,5) box1 = display.newImage( "Box1.png" ) box1.x = display.actualContentWidth/6 box1.y = display.actualContentHeight/10 box1:scale( 0.5, 0.5 ) box1.numberValue=myRandomNumber1 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box1Text = display.newText({x = box1.x, y = box1.y, text = tostring(box1.numberValue), fontSize = 15, font = native.systemFont }) end return true end box1:addEventListener( "touch", onObjectTouch ) local myRandomNumber2 = math.random(1,5) box2 = display.newImage( "Box1.png" ) box2.x = display.actualContentWidth/2.6 box2.y = display.actualContentHeight/10 box2:scale( 0.5, 0.5 ) box2.numberValue=myRandomNumber2 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box2Text = display.newText({x = box2.x, y = box2.y, text = tostring(box2.numberValue), fontSize = 15, font = native.systemFont }) end return true end box2:addEventListener( "touch", onObjectTouch ) local myRandomNumber3 = math.random(1,5) box3 = display.newImage( "Box1.png" ) box3.x = display.actualContentWidth/1.66 box3.y = display.actualContentHeight/10 box3:scale( 0.5, 0.5 ) box3.numberValue=myRandomNumber3 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box3Text = display.newText({x = box3.x, y = box3.y, text = tostring(box3.numberValue), fontSize = 15, font = native.systemFont }) end return true end box3:addEventListener( "touch", onObjectTouch ) local myRandomNumber4 = math.random(1,5) box4 = display.newImage( "Box1.png" ) box4.x = display.actualContentWidth/1.22 box4.y = display.actualContentHeight/10 box4:scale( 0.5, 0.5 ) box4.numberValue=myRandomNumber4 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box4Text = display.newText({x = box4.x, y = box4.y, text = tostring(box4.numberValue), fontSize = 15, font = native.systemFont }) end return true end box4:addEventListener( "touch", onObjectTouch ) local myRandomNumber5 = math.random(1,5) box5 = display.newImage( "Box1.png" ) box5.x = display.actualContentWidth/6 box5.y = display.actualContentHeight/4 box5:scale( 0.5, 0.5 ) box5.numberValue=myRandomNumber5 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box5Text = display.newText({x = box5.x, y = box5.y, text = tostring(box5.numberValue), fontSize = 15, font = native.systemFont }) end return true end box5:addEventListener( "touch", onObjectTouch ) local myRandomNumber6 = math.random(1,5) box6 = display.newImage( "Box1.png" ) box6.x = display.actualContentWidth/2.6 box6.y = display.actualContentHeight/4 box6:scale( 0.5, 0.5 ) box6.numberValue=myRandomNumber6 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box6Text = display.newText({x = box6.x, y = box6.y, text = tostring(box6.numberValue), fontSize = 15, font = native.systemFont }) end return true end box6:addEventListener( "touch", onObjectTouch ) local myRandomNumber7 = math.random(1,5) box7 = display.newImage( "Box1.png" ) box7.x = display.actualContentWidth/1.66 box7.y = display.actualContentHeight/4 box7:scale( 0.5, 0.5 ) box7.numberValue=myRandomNumber7 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box7Text = display.newText({x = box7.x, y = box7.y, text = tostring(box7.numberValue), fontSize = 15, font = native.systemFont }) end return true end box7:addEventListener( "touch", onObjectTouch ) local myRandomNumber8 = math.random(1,5) box8 = display.newImage( "Box1.png" ) box8.x = display.actualContentWidth/1.22 box8.y = display.actualContentHeight/4 box8:scale( 0.5, 0.5 ) box8.numberValue=myRandomNumber8 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box8Text = display.newText({x = box8.x, y = box8.y, text = tostring(box8.numberValue), fontSize = 15, font = native.systemFont }) end return true end box8:addEventListener( "touch", onObjectTouch ) local myRandomNumber9 = math.random(1,5) box9 = display.newImage( "Box1.png" ) box9.x = display.actualContentWidth/6 box9.y = display.actualContentHeight/2.5 box9:scale( 0.5, 0.5 ) box9.numberValue=myRandomNumber9 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box9Text = display.newText({x = box9.x, y = box9.y, text = tostring(box9.numberValue), fontSize = 15, font = native.systemFont }) end return true end box9:addEventListener( "touch", onObjectTouch ) local myRandomNumber10 = math.random(1,5) box10 = display.newImage( "Box1.png" ) box10.x = display.actualContentWidth/2.6 box10.y = display.actualContentHeight/2.5 box10:scale( 0.5, 0.5 ) box10.numberValue=myRandomNumber10 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box10Text = display.newText({x = box10.x, y = box10.y, text = tostring(box10.numberValue), fontSize = 15, font = native.systemFont }) end return true end box10:addEventListener( "touch", onObjectTouch )

Since ‘Money’ is a text object. You can change its text using Money.text = “What you want”. Just make sure the object is acccessible.

See this https://docs.coronalabs.com/api/type/TextObject/text.html

Okay,but still I want to word “Novac:” have variable 0 and I want to display it next to that word “Novac:”
And for that variable 0,I want to increase for each “myRandomNumber” (for each box clicked) which is randomly displayed.

This might help.  Keep in mind you should consider and do some research to see how you can use one event listener function to handle the touch for all 10 boxes, rather then having 10 separate functions. 

But here is the quick answer for you, based on your code the way you have it.

local money = 0 local moneyText = display.newText("Novac: " .. money, 60, 35, nil, 30) local box1 = display.newImage( "Box1.png" ) box1.x = display.actualContentWidth/6 box1.y = display.actualContentHeight/10 box1:scale( 0.5, 0.5 ) box1.numberValue=math.random(1,5) local function onObjectTouch( event ) if ( event.phase == "began" ) then local box1Text = display.newText({x = box1.x, y = box1.y, text = tostring(box1.numberValue), fontSize = 15, font = native.systemFont }) -- this will increase money value by whatever the box1.numberValue is money = money + box1.numberValue -- now display the new value moneyText.text = "Novac: " .. money end return true end box1:addEventListener( "touch", onObjectTouch )

Notice I added a variable to hold the value and named it ‘money’ and then renamed your original handle for the ‘money text object’ to  ‘moneyText’

Also, your text object you had set at -15 on the y axis, so that text object likely would not show on the screen.  In my example I just arbitrarily place it at 35.

I also think you should make box1 thru box10 local variables, as they appear to be global. Unless of course somewhere higher up in your code (not shown) you declared those variables local, or for some other reason you need or want the global.

I also removed the  ‘myRandomNumber1’, as it does not appear to be needed.

These are just suggestions, that would seem to make the code more efficient and easier to debug.

Hope it helps.

Bob

Thank you very much!

It works but when I multiple times click on that box it is still adding that displayed number so how can I do that I when i click for the second time on that box to not add that number to “Novac:” ?

add this field,right after  box1.numberValue = math.random(1,5)

box1.alreadyClicked = false

in the touch function,redo the if event.phase ==“began” then  to:

if event.phase == "began’ and box1.alreadyClicked == false then

    – add this line to  set the flag to true

    box1.alreadyClicked = true

end

Can you add the code into the “<>” that you see on the top in the replying options.? It’s hard to read and understand what you want when it’s not formatted correctly. Thanks!

–SonicX278 

Sure :slight_smile:

local Money = display.newText("Novac:", 60, -15, nil, 30) local myRandomNumber1 = math.random(1,5) box1 = display.newImage( "Box1.png" ) box1.x = display.actualContentWidth/6 box1.y = display.actualContentHeight/10 box1:scale( 0.5, 0.5 ) box1.numberValue=myRandomNumber1 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box1Text = display.newText({x = box1.x, y = box1.y, text = tostring(box1.numberValue), fontSize = 15, font = native.systemFont }) end return true end box1:addEventListener( "touch", onObjectTouch ) local myRandomNumber2 = math.random(1,5) box2 = display.newImage( "Box1.png" ) box2.x = display.actualContentWidth/2.6 box2.y = display.actualContentHeight/10 box2:scale( 0.5, 0.5 ) box2.numberValue=myRandomNumber2 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box2Text = display.newText({x = box2.x, y = box2.y, text = tostring(box2.numberValue), fontSize = 15, font = native.systemFont }) end return true end box2:addEventListener( "touch", onObjectTouch ) local myRandomNumber3 = math.random(1,5) box3 = display.newImage( "Box1.png" ) box3.x = display.actualContentWidth/1.66 box3.y = display.actualContentHeight/10 box3:scale( 0.5, 0.5 ) box3.numberValue=myRandomNumber3 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box3Text = display.newText({x = box3.x, y = box3.y, text = tostring(box3.numberValue), fontSize = 15, font = native.systemFont }) end return true end box3:addEventListener( "touch", onObjectTouch ) local myRandomNumber4 = math.random(1,5) box4 = display.newImage( "Box1.png" ) box4.x = display.actualContentWidth/1.22 box4.y = display.actualContentHeight/10 box4:scale( 0.5, 0.5 ) box4.numberValue=myRandomNumber4 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box4Text = display.newText({x = box4.x, y = box4.y, text = tostring(box4.numberValue), fontSize = 15, font = native.systemFont }) end return true end box4:addEventListener( "touch", onObjectTouch ) local myRandomNumber5 = math.random(1,5) box5 = display.newImage( "Box1.png" ) box5.x = display.actualContentWidth/6 box5.y = display.actualContentHeight/4 box5:scale( 0.5, 0.5 ) box5.numberValue=myRandomNumber5 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box5Text = display.newText({x = box5.x, y = box5.y, text = tostring(box5.numberValue), fontSize = 15, font = native.systemFont }) end return true end box5:addEventListener( "touch", onObjectTouch ) local myRandomNumber6 = math.random(1,5) box6 = display.newImage( "Box1.png" ) box6.x = display.actualContentWidth/2.6 box6.y = display.actualContentHeight/4 box6:scale( 0.5, 0.5 ) box6.numberValue=myRandomNumber6 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box6Text = display.newText({x = box6.x, y = box6.y, text = tostring(box6.numberValue), fontSize = 15, font = native.systemFont }) end return true end box6:addEventListener( "touch", onObjectTouch ) local myRandomNumber7 = math.random(1,5) box7 = display.newImage( "Box1.png" ) box7.x = display.actualContentWidth/1.66 box7.y = display.actualContentHeight/4 box7:scale( 0.5, 0.5 ) box7.numberValue=myRandomNumber7 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box7Text = display.newText({x = box7.x, y = box7.y, text = tostring(box7.numberValue), fontSize = 15, font = native.systemFont }) end return true end box7:addEventListener( "touch", onObjectTouch ) local myRandomNumber8 = math.random(1,5) box8 = display.newImage( "Box1.png" ) box8.x = display.actualContentWidth/1.22 box8.y = display.actualContentHeight/4 box8:scale( 0.5, 0.5 ) box8.numberValue=myRandomNumber8 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box8Text = display.newText({x = box8.x, y = box8.y, text = tostring(box8.numberValue), fontSize = 15, font = native.systemFont }) end return true end box8:addEventListener( "touch", onObjectTouch ) local myRandomNumber9 = math.random(1,5) box9 = display.newImage( "Box1.png" ) box9.x = display.actualContentWidth/6 box9.y = display.actualContentHeight/2.5 box9:scale( 0.5, 0.5 ) box9.numberValue=myRandomNumber9 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box9Text = display.newText({x = box9.x, y = box9.y, text = tostring(box9.numberValue), fontSize = 15, font = native.systemFont }) end return true end box9:addEventListener( "touch", onObjectTouch ) local myRandomNumber10 = math.random(1,5) box10 = display.newImage( "Box1.png" ) box10.x = display.actualContentWidth/2.6 box10.y = display.actualContentHeight/2.5 box10:scale( 0.5, 0.5 ) box10.numberValue=myRandomNumber10 local function onObjectTouch( event ) if ( event.phase == "began" ) then local box10Text = display.newText({x = box10.x, y = box10.y, text = tostring(box10.numberValue), fontSize = 15, font = native.systemFont }) end return true end box10:addEventListener( "touch", onObjectTouch )

Since ‘Money’ is a text object. You can change its text using Money.text = “What you want”. Just make sure the object is acccessible.

See this https://docs.coronalabs.com/api/type/TextObject/text.html

Okay,but still I want to word “Novac:” have variable 0 and I want to display it next to that word “Novac:”
And for that variable 0,I want to increase for each “myRandomNumber” (for each box clicked) which is randomly displayed.

This might help.  Keep in mind you should consider and do some research to see how you can use one event listener function to handle the touch for all 10 boxes, rather then having 10 separate functions. 

But here is the quick answer for you, based on your code the way you have it.

local money = 0 local moneyText = display.newText("Novac: " .. money, 60, 35, nil, 30) local box1 = display.newImage( "Box1.png" ) box1.x = display.actualContentWidth/6 box1.y = display.actualContentHeight/10 box1:scale( 0.5, 0.5 ) box1.numberValue=math.random(1,5) local function onObjectTouch( event ) if ( event.phase == "began" ) then local box1Text = display.newText({x = box1.x, y = box1.y, text = tostring(box1.numberValue), fontSize = 15, font = native.systemFont }) -- this will increase money value by whatever the box1.numberValue is money = money + box1.numberValue -- now display the new value moneyText.text = "Novac: " .. money end return true end box1:addEventListener( "touch", onObjectTouch )

Notice I added a variable to hold the value and named it ‘money’ and then renamed your original handle for the ‘money text object’ to  ‘moneyText’

Also, your text object you had set at -15 on the y axis, so that text object likely would not show on the screen.  In my example I just arbitrarily place it at 35.

I also think you should make box1 thru box10 local variables, as they appear to be global. Unless of course somewhere higher up in your code (not shown) you declared those variables local, or for some other reason you need or want the global.

I also removed the  ‘myRandomNumber1’, as it does not appear to be needed.

These are just suggestions, that would seem to make the code more efficient and easier to debug.

Hope it helps.

Bob

Thank you very much!

It works but when I multiple times click on that box it is still adding that displayed number so how can I do that I when i click for the second time on that box to not add that number to “Novac:” ?

add this field,right after  box1.numberValue = math.random(1,5)

box1.alreadyClicked = false

in the touch function,redo the if event.phase ==“began” then  to:

if event.phase == "began’ and box1.alreadyClicked == false then

    – add this line to  set the flag to true

    box1.alreadyClicked = true

end