I’m trying to make a caterpillar program where the user moves around a caterpillar with their finger and they score when they touch the other sprite. Here is my code:
displayMode(FULLSCREEN)
function setup()
supportedOrientations(CurrentOrientation)
theSprite=readImage("Planet Cute:Character Boy")
tx = CurrentTouch.x
ty = CurrentTouch.y
score = 0
cat = {}
x= math.random(0, WIDTH)
y= math.random(0, HEIGHT)
w= CurrentTouch.x
z= CurrentTouch.y
end
function draw()
background(0)
for i=#cat,1,-1 do
sprite("Planet Cute:Character Boy",cat[i].x,cat[i].y)
end
nex()
sprite("Planet Cute:Character Boy",tx,ty)
sprite("Planet Cute:Character Princess Girl", x, y)
if CollisionDetected(w, z, 30, 30, x, y, 30, 30) then
score = score + 1
next()
end
end
function nex()
table.insert(cat,vec2(w,z))
end
function next()
x = math.random(0, WIDTH)
y= math.random(0, HEIGHT)
end
function touched(t)
if t.state==MOVING then
w= t.prevX
z= t.prevY
tx=tx+t.deltaX
ty=ty+t.deltaY
end
end
function CollisionDetected(x1, y1, w1, h1, x2, y2, w2, h2)
return not ((y1+h1 < y2) or (y1 > y2+h2) or (x1 > x2+w2) or (x1+w1 < x2))
end
Well If you run the program you’ll see that the “caterpillar” gets really messed up and sprites appear in weird places and you can touch far away from the caterpillar and it will become disconnected, etc. I suspect it has something to do with the prevX and prevY values. But I’m not really sure what to do about it