So I’m trying to make it so that a randomly selected sprite comes out from the top of the screen at one of five randomly selected x locations. Here is my code :
displayMode( FULLSCREEN)
function setup()
supportedOrientations(CurrentOrientation)
y=1000
dy=-1
loc={342, 433, 523, 608, 703}
car={}
table.insert(car, "Planet Cute:Character Boy")
table.insert(car, "Planet Cute:Character Cat Girl")
table.insert(car, "Planet Cute:Character Horn Girl")
table.insert(car, "Planet Cute:Character Pink Girl")
table.insert(car, "Planet Cute:Character Princess Girl")
x = loc[math.random(1,5)]
s = car[math.random(1,5)]
end
function draw()
background(52, 172, 22, 255)
-- This sets the line thickness
strokeWidth(5)
y = y + dy
sprite(s, x, y)
end
However, I want the sprites to be continuously created, instead of just one sprite, so I need a loop. Whenever I try to do a loop though it gives me an error. From what I understand this is because the draw function runs 60 times per second so it messes up the loop. How do I work around this?
Could you post the code where you attempted to use a loop? That would be most helpful, as I’m not quite sure I understand what you want, and I’d have a much better idea seeing your attempted code
@RedCoder1 I don’t know if you wanted more than 1 sprite on the screen at a time, but here is code to do 1 random sprite at 1 random location continuously. I changed the speed (dy) just to make the example faster.
displayMode( FULLSCREEN)
supportedOrientations(CurrentOrientation)
function setup()
loc={342, 433, 523, 608, 703}
car={}
table.insert(car, "Planet Cute:Character Boy")
table.insert(car, "Planet Cute:Character Cat Girl")
table.insert(car, "Planet Cute:Character Horn Girl")
table.insert(car, "Planet Cute:Character Pink Girl")
table.insert(car, "Planet Cute:Character Princess Girl")
next()
end
function next()
x = loc[math.random(1,5)]
s = car[math.random(1,5)]
y=1000
dy=-5
end
function draw()
background(52, 172, 22, 255)
y = y + dy
sprite(s, x, y)
if y<0 then
next()
end
end
The variables x, s, y, dy that you set in the function next() are local to that function only, and are not accessible in draw(). They need to be global in order for draw() to see them.
@skysaw A variable is local if you define it as local. Any variable defined in any function is global unless it’s defined as local (with some exceptions that I won’t go into here). So all the variables in next are global otherwise the program wouldn’t have worked.
Yea what I meant was I wanted more than one sprite on the screen at the same time
@RedCoder1 Is this close to what you’re after.
displayMode( FULLSCREEN)
supportedOrientations(CurrentOrientation)
function setup()
dy=-5
count=0
loc={342, 433, 523, 608, 703}
car={}
car1={}
table.insert(car, "Planet Cute:Character Boy")
table.insert(car, "Planet Cute:Character Cat Girl")
table.insert(car, "Planet Cute:Character Horn Girl")
table.insert(car, "Planet Cute:Character Pink Girl")
table.insert(car, "Planet Cute:Character Princess Girl")
next()
end
function next()
x = math.random(1,5)
s = math.random(1,5)
table.insert(car1,vec3(s,x,HEIGHT))
end
function draw()
background(52, 172, 22, 255)
for a,b in pairs(car1) do
sprite(car[b.x],loc[b.y],b.z)
b.z=b.z+dy
if b.z<0 then
table.remove(car1,a)
return
end
end
count=count+1
if count>50 then
next()
count=0
end
end
It does what I want but when the sprites are removed from the table it makes all the other sprites blink