Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Some problem with code
Started by ppodmazko Feb 11 2019 01:03 PM

- - - - -
4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ppodmazko

[GLOBAL: userInfoPane.html]
ppodmazko
  • Observer

  • 1 posts
  • Corona SDK

I'm start learning Lua and Corona SDK and wanna make a simple project today- just a clicker with to button, 1) money=money+wokers and 2) -10 money +1 worker. But i have some problems with it,because i dont know why, but it work wrong. Maybe someone can help me with this problem?

--add button
local platform = display.newImageRect( "img/worker.png", 300, 50)
platform.x = display.contentCenterX
platform.y = display.contentHeight+10
local balloon = display.newImageRect ( "img/coin.png", 112, 112)
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
--step 1
local money=0
local workers=1
--text
local tapText= display.newText( "workers", display.contentCenterX,100,native.systemFont, 40)
local tapText= display.newText( workers, display.contentCenterX,130,native.systemFont, 40)
local tapText= display.newText( "money", display.contentCenterX,-5,native.systemFont, 40)
local tapText= display.newText( money, display.contentCenterX,50,native.systemFont, 40)
--step 2
local function earn()
money=money+workers
tapText.text = money
end

local function buy()
if money >= 10 
then money=money-10
tapText.text = money
workers=workers+1
tapText.text = workers
end
end
--step 3
balloon:addEventListener( "tap", earn)
platform:addEventListener( "tap", buy)



[TOPIC: post.html]
#2

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

https://docs.coronalabs.com/



[TOPIC: post.html]
#3

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

One question I'll ask you - how does the program know which tapText you are talking about when you set tapText.text? You have done the equivalent of naming four children the same and then expecting the one you need to come when you call them.



[TOPIC: post.html]
#4

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,803 posts
  • Corona SDK

I didn't mean that to sound quite so harsh, of course we all make mistakes. The key to debugging is to read each line of code carefully, and ask yourself several questions:

 

What do I need this line of code to do?

What is the object I am acting on? Is it the right one? Where did it come from? What state should it be in before it gets here?

What is this line of code really doing to this object? (use print statements to confirm outcomes)

Did the outcome match what I meant it to?

If not, do I know how to fix the code to get the outcome I want?

 

Only when I get to the end of this list and still have no answer, do I embark on google searches, and as a last resort, forum posts.



[TOPIC: post.html]
#5

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 636 posts
  • Corona SDK

--text
local tapText= display.newText( "workers", display.contentCenterX,100,native.systemFont, 40)
local tapText= display.newText( workers, display.contentCenterX,130,native.systemFont, 40)
local tapText= display.newText( "money", display.contentCenterX,-5,native.systemFont, 40)
local tapText= display.newText( money, display.contentCenterX,50,native.systemFont, 40)

you use same variable to create multiple texts. this is wrong. create 4 different variables, please.




[topic_controls]
[/topic_controls]