Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Get random picture — Gideros Forum

Get random picture

edited May 2016 in General questions
Hi
I have spent 30-60min every evening the last couple of days playing with Gideros, reading and watching videos and starting to get the idea of things. I have almost finished coding my app... well finished it in my head. Now I just need to write the code.

First baby step I want to play with and understand is getting a random picture, and I was hoping for help in connecting the dots.

The real question is: How do I use the random number to get the image??

This is my code so far. I have also pasted the "--" code so you maybe can see what I was thinking.
--local img = Bitmap.new(Texture.new("pen1.png"))
local halfposition = application:getDeviceWidth() / 2
 
local img = {"pen1.png", "pen2.png", "pen3.png" }
local r = math.random(1, #img)
 
--stage:addChild(Bitmap.new(Texture.new(img)))
--img:setAnchorPoint(0.5, 0)
--img:setPosition(halfposition , 0)
--img:setScale(0.25 , 0.25)
 
print (r)

*note: I will use the r-number for something later.
**I could add as many as 150 images.
*** I do know that the "img" in Texture.new(img) dosn't work. I was trying and hoping it might...

Comments

  • greengeekgreengeek Member
    Accepted Answer
    Once you have the random number 'r', use it to index 'img' table like img[r].
    math.randomseed(os.time())
    is needed to make it truly random. But it looks like Gideros framework is calling it internally. No need to bother about it.
    local img = {"pen1.png", "pen2.png", "pen3.png" }
     
    local r = math.random(1, #img)
    stage:addChild(Bitmap.new(Texture.new(img[r])))
    +1 -1 (+1 / -0 )Share on Facebook
  • greengeekgreengeek Member
    edited May 2016
    I got an error on posting my earlier comment and but it was posted fine. So, editing this comment because I can't figure out how to delete comment :)
    +1 -1 (+1 / -0 )Share on Facebook
  • piepie Member
    Accepted Answer
    see also
    http://giderosmobile.com/forum/discussion/comment/32537#Comment_32537
    to read all textures from a texturepack instead of adding them all by hand :)
    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks guys
    I have a trip to norway the few days so I will try it on monday!

    @pie Looks like a solution I need to play with. I ties well into solving the next problem. Adding text underneath the image and keeping al the text so it is easy to work width.
Sign In or Register to comment.