Jump to content

math.random image


SnoopCat

Recommended Posts

hey i need to know how i can make a code to show random pictures in this case i will use race as example.

g_GUI['travelImage']   = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, 'img/travelling.png', false, nil) 

how i can make that code to show diferent images each changemap.

thx

Link to comment

In race/race_client.lua, go to line: 132-140 - you should see TravelScreen.init function.

Try it replace with this:

function TravelScreen.init() 
    local screenWidth, screenHeight = guiGetScreenSize() 
    local randomImages = {'img/travelling1.png','img/travelling2.png','img/travelling3.png'} 
    g_GUI['travelImage']   = guiCreateStaticImage(screenWidth/2-256, screenHeight/2-90, 512, 256, tostring(randomImages[math.random(1,3)]), false, nil) 
    g_dxGUI['travelText1'] = dxText:create('Travelling to', screenWidth/2, screenHeight/2-130, false, 'bankgothic', 0.60, 'center' ) 
    g_dxGUI['travelText2'] = dxText:create('', screenWidth/2, screenHeight/2-100, false, 'bankgothic', 0.70, 'center' ) 
    g_dxGUI['travelText1']:color(240,240,240) 
    hideGUIComponents('travelImage', 'travelText1', 'travelText2') 
end 

Also you must have 3 pictures: travelling1.png,travelling2.png etc... you can change however you want.

Link to comment
local imgTable = { "img/travelling.png","img/image2.png","img/image3.png","img/image4.png" } 
g_GUI['travelImage']   = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, imgTable[math.random(#imgTable)], false, nil) 

Link to comment

this one is not working:

    local imgTable = { "img/travelling.png","img/image2.png","img/image3.png","img/image4.png" } 
    g_GUI['travelImage']   = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, imgTable[math.random(#imgTable)], false, nil) 
  

and also checked if the name was wrong and it wasnt :S

Link to comment
  • Moderators

Maybe try this:

local imgTable = { "img/travelling.png","img/image2.png","img/image3.png","img/image4.png" } 
g_GUI['travelImage'] = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, ""..imgTable[math.random(#imgTable)], false) 

if it don't work, try this:

local ran = math.random(1,3) 
outputChatBox("img/travelImage"..ran) 
g_GUI['travelImage'] = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, "img/travelImage"..ran, false) 

and rename your images like this:

'travelImage3.png' or 'travelImage2.png' or 'travelImage3.png' etc

If those codes don't works, it's maybe because the path is wrong ( the image aren't in the 'img' folder ) or those images aren't in the png format or you there aren't in your meta.xml :|

Link to comment

i checked if all its correct images are in img folder, they are in the meta and i cheked the name also...

the thing i cant understand in your second code its:

    local ran = math.random(1,3) 
    outputChatBox("img/travelImage"..ran) 
    g_GUI['travelImage'] = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, "img/travelImage"..ran, false) 

why u used outputChatBox if its is to say something on the chat?

well its not working :(

Link to comment
  • Moderators

Yeah sorry my bad, I forgot the ".png" :roll:

local ran = math.random(1,3) 
outputChatBox("img/travelImage"..ran..".png") 
g_GUI['travelImage'] = guiCreateStaticImage(screenWidth/2-400, screenHeight/2-300,800, 600, "img/travelImage"..ran..".png", false) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...