How to change object:translate speed (velocity?)

Hi guys!

Recently I asked for help finding something for path finding, and i got a link of mobileTuts+ with a tutorial of that. So yeah, actually it was what i needed, but the thing is i’m such a newby on lua and I can’t find the solution to this.

I managed (hardly) to make an animation of the yellow circle (instead of putting “breadcrumbs” of the path), but the thing that have my brain tired is that i can’t figure how to manage the speed of the ball. I tried creating xspeed and yspeed and setting it to 2.8, then added to the final xpos and ypos … but it passes hover the “walls” …
Here’s the function I have for animate the ball:
[lua]animate = function(event)

if event.phase == ‘ended’ then

path = CalcPath(CalcMoves(board, 1, 1, 19, 19))
max_path = table.getn(path)
xpos, ypos = path[1].x, path[1].y
local marker = display.newCircle(xpos, ypos, 8)
marker:setFillColor(255, 174, 0)
contador = 2
local function makeMovement()

local newX = path[contador].x
local newY = path[contador].y

xpos = (newX * 32 - 16)
ypos = (newY * 32 - 16)
print("X: “…xpos…” Y: “…ypos…” C: "…contador)
marker:translate( xpos - marker.x, ypos - marker.y )

if(contador == max_path) then
contador = 0
Runtime:removeEventListener( “enterFrame”, makeMovement );
print(“Recorrido Terminado en “…max_path…” Frames”)
markerExists = true
else
contador = contador + 1
end

end

Runtime:addEventListener( “enterFrame”, makeMovement )
end
end[/lua]
and here’s the full code:
[lua]display.setStatusBar(display.HiddenStatusBar)

board = {} --Create an emptargetY table to hold the board
counter = 1

setup = function()
counter = 1
board = {}
–Populate the Table
for i = 1, 19 do
board[i] = {}
for j = 1, 19 do
board[i][j] = {}
board[i][j].square = display.newRect((i-1) * 32, (j-1) * 32, 32, 32)
board[i][j].square:setFillColor(71, 107, 214)
board[i][j].isObstacle = 0
end
end
end

addObstacle = function(event)
if event.phase == “ended” and event.y < 620 then
x = math.ceil(event.x/32)
y = math.ceil(event.y/32)
board[x][y].isObstacle = 1
if CalcPath(CalcMoves(board, 1, 1, 19, 19)) then
board[x][y].square:setFillColor(6, 33, 112)
else
board[x][y].isObstacle = 0
end
end
return true
end
Runtime:addEventListener(“touch”, addObstacle)

function CalcMoves(board, startX, startY, targetX, targetY)
local openlist={} --Possible Moves
local closedlist={} --Checked Squares
local listk=1 – open list counter
local closedk=0 – Closedlist counter
local tempH = math.abs(startX-targetX) + math.abs(startY-targetY)
local tempG = 0
openlist[1] = {x = startX, y = startY, g = 0, h = tempH, f = 0 + tempH ,par = 1}
local xsize = table.getn(board[1])
local ysize = table.getn(board)
local curSquare = {}
local curSquareIndex = 1 – Index of current base

while listk > 0 do
local lowestF = openlist[listk].f
curSquareIndex = listk
for k = listk, 1, -1 do
if openlist[k].f < lowestF then
lowestF = openlist[k].f
curSquareIndex = k
end
end

closedk = closedk + 1
table.insert(closedlist,closedk,openlist[curSquareIndex])

curSquare = closedlist[closedk] – define current base from which to grow list

local rightOK = true
local leftOK = true – Booleans defining if they’re OK to add
local downOK = true – (must be reset for each while loop)
local upOK = true

– Look through closedlist. Makes sure that the path doesn’t double back
if closedk > 0 then
for k = 1, closedk do
if closedlist[k].x == curSquare.x + 1 and closedlist[k].y == curSquare.y then
rightOK = false
end
if closedlist[k].x == curSquare.x-1 and closedlist[k].y == curSquare.y then
leftOK = false
end
if closedlist[k].x == curSquare.x and closedlist[k].y == curSquare.y + 1 then
downOK = false
end
if closedlist[k].x == curSquare.x and closedlist[k].y == curSquare.y - 1 then
upOK = false
end
end
end

– Check if next points are on the map and within moving distance
if curSquare.x + 1 > xsize then
rightOK = false
end
if curSquare.x - 1 < 1 then
leftOK = false
end
if curSquare.y + 1 > ysize then
downOK = false
end
if curSquare.y - 1 < 1 then
upOK = false
end

– If it IS on the map, check map for obstacles
–(Lua returns an error if you try to access a table position that doesn’t exist, so you can’t combine it with above)
if curSquare.x + 1 <= xsize and board[curSquare.x+1][curSquare.y].isObstacle ~= 0 then
rightOK = false
end
if curSquare.x - 1 >= 1 and board[curSquare.x-1][curSquare.y].isObstacle ~= 0 then
leftOK = false
end
if curSquare.y + 1 <= ysize and board[curSquare.x][curSquare.y+1].isObstacle ~= 0 then
downOK = false
end
if curSquare.y - 1 >= 1 and board[curSquare.x][curSquare.y-1].isObstacle ~= 0 then
upOK = false
end

– check if the move from the current base is shorter then from the former parrent
tempG =curSquare.g + 1
for k=1,listk do
if rightOK and openlist[k].x==curSquare.x+1 and openlist[k].y==curSquare.y and openlist[k].g>tempG then
tempH=math.abs((curSquare.x+1)-targetX)+math.abs(curSquare.y-targetY)
table.insert(openlist,k,{x=curSquare.x+1, y=curSquare.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
rightOK=false
end

if leftOK and openlist[k].x==curSquare.x-1 and openlist[k].y==curSquare.y and openlist[k].g>tempG then
tempH=math.abs((curSquare.x-1)-targetX)+math.abs(curSquare.y-targetY)
table.insert(openlist,k,{x=curSquare.x-1, y=curSquare.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
leftOK=false
end

if downOK and openlist[k].x==curSquare.x and openlist[k].y==curSquare.y+1 and openlist[k].g>tempG then
tempH=math.abs((curSquare.x)-targetX)+math.abs(curSquare.y+1-targetY)
table.insert(openlist,k,{x=curSquare.x, y=curSquare.y+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
downOK=false
end

if upOK and openlist[k].x==curSquare.x and openlist[k].y==curSquare.y-1 and openlist[k].g>tempG then
tempH=math.abs((curSquare.x)-targetX)+math.abs(curSquare.y-1-targetY)
table.insert(openlist,k,{x=curSquare.x, y=curSquare.y-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
upOK=false
end
end

– Add points to openlist
– Add point to the right of current base point
if rightOK then
listk=listk+1
tempH=math.abs((curSquare.x+1)-targetX)+math.abs(curSquare.y-targetY)
table.insert(openlist,listk,{x=curSquare.x+1, y=curSquare.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

– Add point to the left of current base point
if leftOK then
listk=listk+1
tempH=math.abs((curSquare.x-1)-targetX)+math.abs(curSquare.y-targetY)
table.insert(openlist,listk,{x=curSquare.x-1, y=curSquare.y, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

– Add point on the top of current base point
if downOK then
listk=listk+1
tempH=math.abs(curSquare.x-targetX)+math.abs((curSquare.y+1)-targetY)
table.insert(openlist,listk,{x=curSquare.x, y=curSquare.y+1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

– Add point on the bottom of current base point
if upOK then
listk=listk+1
tempH=math.abs(curSquare.x-targetX)+math.abs((curSquare.y-1)-targetY)
table.insert(openlist,listk,{x=curSquare.x, y=curSquare.y-1, g=tempG, h=tempH, f=tempG+tempH, par=closedk})
end

table.remove(openlist,curSquareIndex)
listk=listk-1

if closedlist[closedk].x==targetX and closedlist[closedk].y==targetY then
return closedlist
end
end

return nil
end
function CalcPath(closedlist)
if closedlist==nil then
return nil
end
local path={}
local pathIndex={}
local last=table.getn(closedlist)
table.insert(pathIndex,1,last)

local i=1
while pathIndex[i]>1 do
i=i+1
table.insert(pathIndex,i,closedlist[pathIndex[i-1]].par)
end

for n=table.getn(pathIndex),1,-1 do
table.insert(path,{x=closedlist[pathIndex[n]].x, y=closedlist[pathIndex[n]].y})
end

closedlist=nil
return path
end
–This is the function I modified to make the ball animated
animate = function(event)

if event.phase == ‘ended’ then

path = CalcPath(CalcMoves(board, 1, 1, 19, 19))
max_path = table.getn(path)
xpos, ypos = path[1].x, path[1].y
local marker = display.newCircle(xpos, ypos, 8)
marker:setFillColor(255, 174, 0)
contador = 2
local function makeMovement()

local newX = path[contador].x
local newY = path[contador].y

xpos = (newX * 32 - 16)
ypos = (newY * 32 - 16)
print("X: “…xpos…” Y: “…ypos…” C: "…contador)
marker:translate( xpos - marker.x, ypos - marker.y )

if(contador == max_path) then
contador = 0
Runtime:removeEventListener( “enterFrame”, makeMovement );
print(“Recorrido Terminado en “…max_path…” Frames”)
markerExists = true
else
contador = contador + 1
end

end

Runtime:addEventListener( “enterFrame”, makeMovement )
end
end

– Initialize map for the first time, so the screen doesn’t goes black at start
setup()
local goButton = display.newImage(“PlaceMarkers.png”, 10, 630)
goButton:addEventListener(“touch”, animate)

local resetButton = display.newImage(“Reset.png”, 10, 700)
resetButton:addEventListener(“touch”, setup)[/lua]

Please, I really need help, my brain is killing me!

Thanks (again) [import]uid: 58067 topic_id: 10443 reply_id: 310443[/import]