– Ooze
–this is all in main
–please help dave, read the title
--variables
local slimeMovement
local timer
local oozeWidth
local oozeHeight
function setup()
print("Hello World!")
displayMode(FULLSCREEN)
--defining variables
timer=0
oozeWidth=WIDTH/2
oozeHeight=HEIGHT/2
--sprites
sprite("Dropbox:ooze",oozeWidth,oozeHeight,64,48)
end
-- This function gets called once every frame
function draw()
--setting up
sprite("Dropbox:ExperimentMap",WIDTH/2,HEIGHT/2,1024,768)
sprite("Dropbox:ooze",oozeWidth,oozeHeight,64,48)
strokeWidth(5)
timer=timer+1
--deciding where slime will move
if timer==1 then
slimeMovement=math.random(4)
print(slimeMovement)
if slimeMovement==1 then print("moving up")
oozeHeight=oozeHeight+25
sprite("Dropbox:ooze",oozeWidth,oozeHeight,64,48)
end
if slimeMovement==2 then print("moving right")
oozeWidth=oozeWidth+25
sprite("Dropbox:ooze",oozeWidth,oozeHeight,64,48)
end
if slimeMovement==3 then print("moving down")
oozeHeight=oozeHeight-25
sprite("Dropbox:ooze",oozeWidth,oozeHeight,64,48)
end
if slimeMovement==4 then print("moving left")
oozeWidth=oozeWidth-25
sprite("Dropbox:ooze",oozeWidth,oozeHeight,64,48)
end
elseif timer==60 then timer=0
end
end
--end of code
@codeaprogrammer If you want a smooth random motion in an up/down, right/left direction, then here’s a way to do it. It also accounts for going off the screen edge. I don’t have your sprites, so I used the Codea sprites. There are better ways to make the random motion in any direction, but you show just up, down, right, or left so that’s what I show.
displayMode(FULLSCREEN)
--variables
local slimeMovement
local timer
local oozeWidth
local oozeHeight
function setup()
timer=60
oozeWidth=WIDTH/2
oozeHeight=HEIGHT/2
dx,dy=0,0
end
function draw()
background(0)
sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
timer=timer+1
if timer>60 then
timer=0
vx,vy=0,0
slimeMovement=math.random(4)
if slimeMovement==1 then
vy=1
elseif slimeMovement==2 then
vx=1
elseif slimeMovement==3 then
vy=-1
elseif slimeMovement==4 then
vx=-1
end
end
dx=dx+vx
dy=dy+vy
sprite("Planet Cute:Character Boy",(oozeWidth+dx)%WIDTH,(oozeHeight+dy)%HEIGHT,50)
end
@codeaprogrammer Here’s a version that uses more random directions.
displayMode(FULLSCREEN)
--variables
local timer=60
local oozeWidth=WIDTH/2
local oozeHeight=HEIGHT/2
local speed=2
function setup()
end
function draw()
background(0)
sprite("Cargo Bot:Starry Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
timer=timer+1
if timer>60 then
timer=0
dir=vec2(math.random(-100,100),math.random(-100,100))
dir=dir:normalize()*speed
end
oozeWidth=oozeWidth+dir.x
oozeHeight=oozeHeight+dir.y
sprite("Planet Cute:Character Boy",oozeWidth%WIDTH,oozeHeight%HEIGHT,50)
end
Some good precedents to look at it flocking swarm code in processing, a guy called daniel shiffman explains it really well. That knowledge has spawned a lot of other functions for me and just gave me a really good foundation for thinking in terms of “time” in the app