Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
TextField isn't displayed — Gideros Forum

TextField isn't displayed

tytadastytadas Member
edited October 2016 in General questions
I haven't used forum help for a while, so I believe I have the permission to do that! :))
Im trying to save a players progress, but the score isn't being displayed. It should show on the screen "Money:" but there aren't any type of text like that. Here is my code:
local function loadScore()
local file = io.open("|D|settings.txt","r")
if not file then
score = 0
else
score=tonumber(file:read("*line"))
file:close()
end
end
 
local function saveScore()
local file=io.open("|D|settings.txt","w+")
file:write(score .. "\n")
file:close()
end
 
 
 
local function initScores()
local score = 0
local scoreText = TextField.new(nil, "Money: " .. score)
scoreText:setPosition(10,10)
stage:addChild(scoreText)
end
Why the problem occurs? It seems everything fine, there aren't any erros displayed on the output. So I was wondering, this problem occurs because of "local function initScores()". Because when I am creating a simple textField, it is displayed very well. And there are 1 more problem. Why does the text field is showed in the different places of the screen? For example, text field on 320x480 screen resolution is being displayed in the corner, on my LG K10 the text is being displayed in the different position. The main position is (10,10), but on my phone the text is being showed like (30,10).

Thank you for your time!

Comments

  • talistalis Guru
    edited October 2016
    Hi i will try to answer your questions.

    1-This forum's sole purpose is to share experience and ask questions. So you are more than welcome to ask any question here. But just to keep in mind that before asking any question a little forum search will be perfect :D

    2-Have you tried to print the score parameter. Maybe it is coming empty from the loadscore function. Because in your explanation as far as i understand normal textfield is working.

    3-About your position question. In the project properties 320x480 is your logical resolution. Which means that your ccordinates will change if your target device resolution is different from this one according to your chosen algorithm. (Pixel perfect, Stretch etc..)
    If you want no change at all than you need to set Scale Mode to "No Scale".
    By the way if you will read and experiment these you will understand better.
    http://docs.giderosmobile.com/automatic_image_resolution.html
    http://docs.giderosmobile.com/automatic_screen_scaling.html

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    Hi @tytadas,

    Maybe unrelated but a few comments:
    - You will probably need to update the score at some point in the TextField, but since you declare it local in your 'initScores' function it will be difficult to retrieve its instance later on for a scoreText:setText()
    - Could it be that something else is drawn over your TextField, for example a background added to the stage after initScores is called ?

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • tytadastytadas Member
    edited October 2016
    Hello Guys! Thanks for the respond! ^^

    @talis, Yeah, I should definately read next time. Literally it's really complicated to me to understand these hard understandable words, but I'll make it out. Thank you for the explanation about the position question. I know how to scale images and until this point im doing it great. I haven't tried to print the score parameter, cause I don't know how to do that :D

    @hgy29, I just declared it in initScores cause I have read the book and as I understood, it initalizats the score. How to make it simpler? Nop, the code is at the begining and it starts on line 1.

    Maybe it's because I haven't declared when the score should be added? Cause I need to make a function when the bird is touched it gives +1 score and I havent called this function so maybe because of that the score isn't being showed?
  • piepie Member
    Accepted Answer
    @tytadas to print out anything use print()
    print("HELLO") --> HELLO
     
    local myvar = 3
    local mystrvar = "SCORE"
    print("MY VARIABLE", myvar, mystrvar) --> MY VARIABLE 3 SCORE
    (use C button before the textarea used for posting here, to format your code as this one :) )

    if this is all of your code, i may guess that you're missing the call to
    initScores()
    what hgy29 was saying is that since you are creating scoreText as local, inside another local function, you are not keeping any reference to it; it will be a problem later when you will need to update its value.
    Basic fix is to remove local before scoreText, so you make it global and accessible from anywhere inside your code.

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • hgy29hgy29 Maintainer
    edited October 2016
    Do you mean you posted all of your code ?

    If so you should at least call initScores() somewhere if you want your TextField to appear.

    EDIT: @pie was faster :)

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • @pie Thank you! I'll keep in mind! Chheers! :)>-
  • @hgy29 That's the point, I don't have "initScores()" in my code :(
  • But why do I need initScores()? What does it stands for?
  • piepie Member
    Accepted Answer
    you need it because you have to call the function initScores() to make it "run".
    When you write
    local function initScores()
    local score = 0
    local scoreText = TextField.new(nil, "Money: " .. score)
    scoreText:setPosition(10,10)
    stage:addChild(scoreText)
    end
    you are defining a set of actions to run when you call the function, which is called by writing the function name followed by () - and inside () you can put some parameters, as in:
    local function printVar(v)
    print("MY VAR NOW IS", v)
    end 
     
    printVar("true")
    printVar(3)
    printVar(6)

    the same applies to any function (ie. loadScore() and saveScore() )

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • @pie Very useful information! I already wrote it to my notes :D Thanks :)
Sign In or Register to comment.