Well, @Fred, I think you’ll be happy to learn that in cleaning up my code the bug went away. So cheers!
For giggles, here’s the cleaned up code. It’s intended to trace a line wherever you drag your finger, and make a little spider sprite follow that line.
You may be able to spot that I’m coming from Objective C and am not totally doing things the “lua way”. Any tips will be appreciated.
I have to say I’m a little blown away by how short lua programs can be.
--# Main
function setup()
displayMode(FULLSCREEN)
--object to hold drawn lines:
webs = {}
TouchHandler:recordTouchesIn(webs)
--the spider!
spider = MakeSprite ("Documents:Orb Spider Red PNG hopefully")
spider:followPath(webs)
--old, delete asap:
draggedLines = {}
end
function draw()
--standard, apparently necessary to prevent weirdness:
background(0,0,0,0)
--sprite for background:
sprite("Documents:Flower Twigs",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
--this makes it easier to see thin lines:
noSmooth()
--drawing all recorded touches:
TouchHandler:drawTouches(webs)
--the spider!
spider:followPath()
spider:draw()
end
function touched (touch)
TouchHandler:touched(touch)
end
--# MakeSprite
MakeSprite = class()
function MakeSprite:init( name )
self.name = name
self.position = nil
self.angle = 0
self.movementVector = nil
self.destination = nil
self.path = nil
self.resultReport = nil
end
function MakeSprite:draw()
if self.position ~= nil then
sprite( self.name , self.position.x, self.position.y )
end
end
function MakeSprite:moveSelf()
if self.destination == nil then
self.resultReport = "no destination"
else
if self:checkIfCloseEnough() == true then
self.position = self.destination
self.resultReport = "at destination"
else
self.position = self.position + self.movementVector
self.resultReport = "moved"
end
end
end
function MakeSprite:setDestination(givenDestination)
self.destination = givenDestination
if givenDestination == nil then return end
local angleToDestination = vec2(1,0):angleBetween(self.position - self.destination)
self.movementVector = vec2(-math.cos(angleToDestination),-math.sin(angleToDestination))
end
function MakeSprite:followPath(unlessNewPathPassedHere)
--if no path is passed, try to move:
if unlessNewPathPassedHere == nil then
self:moveSelf()
--if it was anything but a normal move, check where to go next:
if self.resultReport == "moved" then return else self:checkPath() end
--otherwise set path to value passed:
else
self.path = unlessNewPathPassedHere
end
end
function MakeSprite:checkPath()
--if at destination, iterate to find which one, then assign the next one
if self.resultReport == "at destination" then
--maxn(path) is -1 here so that loop stops before the end of the line
for nextPoint = 1, table.maxn(self.path)-1 do
if TouchHandler:arePositionsTheSameOfVectorAndTouch(self.position,
self.path[nextPoint]) == true then
self:setDestination(TouchHandler:makeVectorFromTouch(self.path[nextPoint+1]))
break
end
end
--if not, if path table total == 2, set position and destination
elseif self.resultReport == "no destination" and table.maxn(self.path) == 2 then
self.position = TouchHandler:makeVectorFromTouch(self.path[1])
self:setDestination(TouchHandler:makeVectorFromTouch(self.path[2]))
--if there's no path, bail:
elseif self.path == nil then
return
end
end
function MakeSprite:checkIfCloseEnough()
if (self.position.x < self.destination.x + 1 and self.position.x > self.destination.x - 1)
and (self.position.yself.destination.y-1)
then
return true
end
end
--# TouchHandler
TouchHandler = class()
function TouchHandler:recordTouchesIn(givenTable)
-- takes passed-in table and fills it with recorded touches
touchTable = givenTable
end
function TouchHandler:touched(touch)
--to functionally function this function should be called by the Main:touched(touch) function
--now: if a value has been set for touchTable, put each touch in it
if touchTable ~= nil then
table.insert(touchTable, touch)
end
end
function TouchHandler:drawTouches(givenTouchTable)
--for each point, draw a line between it and the next point, until one away from the end
for currentTouch = 1, table.maxn(givenTouchTable)-1 do
line(givenTouchTable[currentTouch].x,givenTouchTable[currentTouch].y,
givenTouchTable[currentTouch+1].x,givenTouchTable[currentTouch+1].y)
end
end
function TouchHandler:makeVectorFromTouch(givenTouch)
--turning a touch into a vector
return vec2(givenTouch.x, givenTouch.y)
end
function TouchHandler:arePositionsTheSameOfVectorAndTouch(givenVector,givenTouch)
if givenVector == TouchHandler:makeVectorFromTouch(givenTouch) then
return true else return false
end
end
```