Page 1 of 1

How do i create a auto moving character without any user inputs

Posted: Fri Dec 01, 2017 7:14 am
by XBiscuits
Hi!!

Im have this idea of creating a endless runner but i cant seem to create a function for the player's character to move forward without any user inputs(much like how flappy bird moves forward)

please help me get some idea in how to do this and how it works so that i can implement it on future stuff...

Any help would be appreciated! :D

Code: Select all

function love.load()
    -- FullScreen
    love.window.setFullscreen(true, "desktop")
    end

    --Player Stats
    player ={x = 100, y = 375, speed = 5 }
    

function love.update(dt)
    --Auto Forward Action

    --Keyboard S and W or Up and Down
    if love.keyboard. isDown("s") then
        player.y = player.y + player.speed
    end

    if love.keyboard. isDown("w") then
        player.y = player.y - player.speed
    end
end

function love.draw()
    --CharacterRectangleWhite
    love.graphics.setColor(255, 255, 255)
    love.graphics.rectangle("fill", player.x, player.y, 50 , 50)
end


Re: How do i create a auto moving character without any user inputs

Posted: Fri Dec 01, 2017 9:24 am
by MadByte
Question:
What do you think could possibly happen if you do the same that you did with the y axis to the x axis, but without the if loop?
(And btw, you might want to use dt to calculate your movement.)

Re: How do i create a auto moving character without any user inputs

Posted: Fri Dec 01, 2017 10:08 am
by xNick1
If you take away the if where you check if someone's pressing the key, the characters moves without any user input.
In this case it keeps moving forward

Code: Select all

function love.update(dt)
        player.y = player.y + player.speed * dt
end

Re: How do i create a auto moving character without any user inputs

Posted: Sat Dec 02, 2017 12:27 am
by XBiscuits
MadByte wrote: Fri Dec 01, 2017 9:24 am Question:
What do you think could possibly happen if you do the same that you did with the y axis to the x axis, but without the if loop?
(And btw, you might want to use dt to calculate your movement.)
Ahhh so i need the dt to create a much smoother control system?
and that`s what i am missing in my last attempt because i simply put player.x = player.x + player.speed...

Thanks a lot!! :D

Re: How do i create a auto moving character without any user inputs

Posted: Sat Dec 02, 2017 12:31 am
by XBiscuits
xNick1 wrote: Fri Dec 01, 2017 10:08 am If you take away the if where you check if someone's pressing the key, the characters moves without any user input.
In this case it keeps moving forward

Code: Select all

function love.update(dt)
        player.y = player.y + player.speed * dt
end
thanks!! now that`s the use of delta time..
one question.. if i change the variables so that it may affect something else, will i be able to use it for moving obstacles??