As shown in this video, this white square can go through the right and left border and ends up teleporting back up or down a row as well.
How do i prevent this and get the square to stop when it hits the edges?
NOTE: Each cell is a frame which is counted from 1 to 100 from the top left to the bottom right corner of the canvas.
function MovePlayer()
for index, CanvasCell in ipairs(GameCanvas:GetChildren()) do
if CanvasCell:IsA("Frame") and CanvasCell.CellType.Value == "Player" and not PlayerMoved then
if CurrentPlayerDirection == "Up" then -- Move up
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder - ConfigModule.CanvasSizeXY)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved up")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Down" then -- Move down
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder + ConfigModule.CanvasSizeXY)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved down")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Right" then -- Move right
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder + 1)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved right")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
elseif CurrentPlayerDirection == "Left" then -- Move left
local NextCell = GameCanvas:FindFirstChild("cell" .. CanvasCell.LayoutOrder - 1)
if NextCell and NextCell.CellType.Value == "Floor" then
ConfigModule.Map[CanvasCell.LayoutOrder] = "-"
ConfigModule.Map[NextCell.LayoutOrder] = "P"
PlayerMoved = true
print("Moved left")
elseif NextCell and NextCell.CellType.Value == "WinPart" then
print("Win!")
elseif NextCell and NextCell.CellType.Value == "KillPart" then
print("Dead!")
end
end
end
end
RefreshCanvas()
PlayerMoved = false
end
Feedback will also be nice too!