Hi, I am a complete beginner, I have never looked at any coding before and am only about a month into this so please forgive me if this is a stupid question.
Basically I want to have a sprite appear on the screen at random places. Using the Lua Jump game I was able to get that far. But now I want the sprite to be chosen at random from a list of several sprites. Do I create a table of the sprites and if so, how do I call them out randomly and put them into another table that it is used to draw them on the screen? Would I create a variable that equals the sprite? If so I am really not sure how to do that as my previous attempts have failed.
Sorry if I am not being clear on the question and will happy to clarify anything I can.
Thanks for any help
Try this for starters.
displayMode(FULLSCREEN)
function setup()
tab1={}
table.insert(tab1,readImage("Planet Cute:Character Boy"))
table.insert(tab1,readImage("Planet Cute:Character Cat Girl"))
table.insert(tab1,readImage("Planet Cute:Character Horn Girl"))
table.insert(tab1,readImage("Planet Cute:Character Pink Girl"))
table.insert(tab1,readImage("Planet Cute:Character Princess Girl"))
et=-10
end
function draw()
background(40, 40, 50)
if ElapsedTime-et>1 then
offset=math.random(#tab1)
x=math.random(50,WIDTH-50)
y=math.random(50,HEIGHT-50)
et=ElapsedTime
end
sprite(tab1[offset],x,y)
end
Or is this more of what you’re after.
displayMode(FULLSCREEN)
function setup()
tab1={}
tab2={}
table.insert(tab1,readImage("Planet Cute:Character Boy"))
table.insert(tab1,readImage("Planet Cute:Character Cat Girl"))
table.insert(tab1,readImage("Planet Cute:Character Horn Girl"))
table.insert(tab1,readImage("Planet Cute:Character Pink Girl"))
table.insert(tab1,readImage("Planet Cute:Character Princess Girl"))
end
function draw()
background(40, 40, 50)
fill(255)
text("tap screen for random picks",WIDTH/2,HEIGHT/2)
for a,b in pairs(tab2) do
sprite(tab1[b.z],b.x,b.y)
end
end
function pickRandom()
tab2={}
for z=1,#tab1 do
offset=math.random(#tab1)
x=math.random(50,WIDTH-50)
y=math.random(50,HEIGHT-50)
table.insert(tab2,vec3(x,y,offset))
end
end
function touched(t)
if t.state==BEGAN then
pickRandom()
end
end
Maybe this is what you’re looking for? Every time you tap it adds a new character where you tapped, with a random texture from the sprites table.
-- Random Sprites
function setup()
-- Sprites is a table containing all the images we want to choose from.
sprites = {
readImage("Planet Cute:Character Boy"),
readImage("Planet Cute:Character Cat Girl"),
readImage("Planet Cute:Character Horn Girl"),
readImage("Planet Cute:Character Pink Girl"),
readImage("Planet Cute:Character Princess Girl"),
}
-- Chars is empty for now, but we add to it when the user taps.
chars = {}
end
function draw()
background(255)
-- Go through all of the characters (one more every time you tap), and draw their image at their position
for k,v in ipairs(chars) do
sprite(sprites[v.texture], v.x, v.y)
end
-- Instructions for the user
font("Futura")
fontSize(72)
fill(0)
text("Tap to add character", WIDTH / 2, HEIGHT / 2)
end
function touched(touch)
if touch.state == BEGAN then
-- Here we add a sub-table to the chars table when the user taps.
-- "texture = math.random(1, #sprites)" - The variable "texture" is a random index of the table of all the sprites to choose from.
-- "x = touch.x, y = touch.y" - Set the X and Y coordinates of the character to where the user tapped.
table.insert(chars, {texture = math.random(1, #sprites), x = touch.x, y = touch.y})
end
end
Wow thank you for those, I really appreciate it. But what I am thinking of is a little different. If you look at the Lua Jump game they have a class called clouds and one called cloud levels. I have been messing with this game just trying to figure stuff out and have done quite a bit including changing the clouds to a sprite. That was pretty simple, but now instead of generating the same sprite for every cloud I was wondering if I could make it randomly pick from a table of different ones. So the sprite needs to stay on the screen in its location but the next time it draws one it would be a different sprite. Does that make sense?
And thanks again for your help, this forum is awesome!
Wow thank you for those, I really appreciate it. But what I am thinking of is a little different. If you look at the Lua Jump game they have a class called clouds and one called cloud levels. I have been messing with this game just trying to figure stuff out and have done quite a bit including changing the clouds to a sprite. That was pretty simple, but now instead of generating the same sprite for every cloud I was wondering if I could make it randomly pick from a table of different ones. So the sprite needs to stay on the screen in its location but the next time it draws one it would be a different sprite. Does that make sense?
And thanks again for your help, this forum is awesome!
Sorry, not sure how all those duplicates appeared.
Here’s the above program modified. You just need to change the Girls to your different cloud sprites.
displayMode(FULLSCREEN)
function setup()
tab1={}
tab2={}
et=-5
table.insert(tab1,readImage("Planet Cute:Character Boy"))
table.insert(tab1,readImage("Planet Cute:Character Cat Girl"))
table.insert(tab1,readImage("Planet Cute:Character Horn Girl"))
table.insert(tab1,readImage("Planet Cute:Character Pink Girl"))
table.insert(tab1,readImage("Planet Cute:Character Princess Girl"))
end
function draw()
background(40, 40, 50)
fill(255)
if ElapsedTime-et>2 then
pickRandom()
et=ElapsedTime
end
for a,b in pairs(tab2) do
sprite(tab1[b.z],b.x,b.y)
end
end
function pickRandom()
offset=math.random(#tab1)
x=math.random(50,WIDTH-50)
y=math.random(50,HEIGHT-50)
table.insert(tab2,vec3(x,y,offset))
end
Actually, the programs are fine, it’s just that for clouds like in Lua Jump, you could have it generate the cloud texture randomly, say, five times. Every time you make one, use table.insert to add to the table of randomly chosen sprite. Done.
Sorry for the delay in getting back. Thanks I will try these suggestions. Really appreciate the responses.