Hack Forums
[Lua] [SOLVED] Anyone able to help me out with this little issue - Love2D - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Lua] [SOLVED] Anyone able to help me out with this little issue - Love2D (/showthread.php?tid=4135593)



[SOLVED] Anyone able to help me out with this little issue - Love2D - Toph Beifong - Mar 26, 2014

So iv been learning Love2D and my project was simple, make a snake game. So i decided to load each segment of the snake separately via a loop but one the loop is complete the snake disappears and i haven't even go to movement yet.

Code
function love.load()
    love.graphics.setBackgroundColor(000,000,000)
    segmentImage = love.graphics.newImage('images/segment.png')
    startingPositionX = 200
    startingPositionY = 200

    bodyLength = 4;
end

function buildSnake()
    segmentCount = 1;
    while segmentCount <= bodyLength do
        love.graphics.draw(segmentImage, startingPositionX, startingPositionY)
        startingPositionX = startingPositionX - 35
        segmentCount = segmentCount + 1
    end
    --

end

function love.draw()
    buildSnake()
end

EDIT: since solved this, used a multi dimentional table to store the image position in: code below.

Code
function love.load()
    love.graphics.setBackgroundColor(000,000,000)
    segmentImage = love.graphics.newImage('images/segment.png')
    startingPositionX = 200
    startingPositionY = 200
    bodyLength = 4;
    count = 0

    segmentControl = {}
    while count < bodyLength do
        startingPositionX = startingPositionX - 20
        table.insert(segmentControl, {x=startingPositionX, y=startingPositionY})
        count = count + 1
    end
end

function love.draw()
    for i = 1, 3 do
        love.graphics.draw(segmentImage, segmentControl[i].x, segmentControl[i].y)

    end
end

[Image: Hm3Q8O5.png]


* - .eXe - Mar 26, 2014

Code
function love.load()
    love.graphics.setBackgroundColor(000,000,000)
    segmentImage = love.graphics.newImage('images/segment.png')
    startingPositionX = 200
    startingPositionY = 200

    bodyLength = 4;

function buildSnake()
    segmentCount = 1;
    while segmentCount <= bodyLength do
      love.graphics.draw(segmentImage, startingPositionX, startingPositionY)
      startingPositionX = startingPositionX - 35
      segmentCount = segmentCount + 1
    end

end

function love.draw()
    buildSnake()
end

I have never used Love2D so forgive me. But should it not be like that?


* - Toph Beifong - Mar 26, 2014

Remember your function ends xD, and naa that gives same issue. I think i may have found an alternative way around it and solved a problem later on anyway. Will let you know how it goes.


* - .eXe - Mar 26, 2014

(Mar 26, 2014 02:03 AM)Mr. P-teo Wrote:  Remember your function ends xD, and naa that gives same issue. I think i may have found an alternative way around it and solved a problem later on anyway. Will let you know how it goes.

Oh, thought I had too many ends when I moved it. But yeah, let me know. If not I'll download Love2D and have a proper look.