#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Moving Sprites On Screen

edited August 2014 Posts: 145

I am trying to scroll move clouds across the screen. I keep an array of their positions and keep shifting it. When a cloud goes off the screen I remove it from the array and add it to the end of the array as a new cloud. However, my code is very jerky and glitchy. Could someone help me figure out why?

``````displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
clouds = {}
cloudsx = {}
cloudsy = {}
end

function draw()
background(160, 79, 79, 255)
drawclouds()
end

function drawclouds()
if clouds[1]==nil then

--Generate Clouds
for i=0,30 do
c = math.random(0,2)
x = WIDTH*1.1

--Which Cloud
clouds[i]=c
--X position
cloudsx[i]=x-200*i
--y position
cloudsy[i]=math.random(HEIGHT/1.65,HEIGHT/1.5)

end

end

--draw clouds
scale(1.35)
print(cloudsx[1])
for i=1,8 do

--scroll cloud
cloudsx[i]=cloudsx[i]-6

--display different types of clouds
if (clouds[i]==0)then
sprite("Platformer Art:Cloud 1",cloudsx[i],cloudsy[i])
end
if (clouds[i]==1)then
sprite("Platformer Art:Cloud 2",cloudsx[i],cloudsy[i])
end
if (clouds[i]==2)then
sprite("Platformer Art:Cloud 3",cloudsx[i],cloudsy[i])
end

--find greatest x coordinate if a cloud
gr=0
for i=1,8 do
if(cloudsx[i]>gr)then
gr=cloudsx[i]
end
end
--if a cloud is off the screen the make it follow the farers cloud to the rigt
if(cloudsx[i]<-400 )then
cloudsx[i]=gr+200
end

end
scale(1/1.5)

end

``````
Tagged:

• Mod
Posts: 5,396

Try taking the print statement out of draw

• Posts: 145

I discovered I created 30 sprites instead of 8 in the beginning which messed up my whole loop.

• Mod
Posts: 3,297

to avoid this kind of pb, this is a better way to code it:

``````for i=1,#clouds do
``````