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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
XBiscuits
Prole
Posts: 3
Joined: Fri Dec 01, 2017 4:05 am

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

Post 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

A Man who can fly a rocket to orbit will have a hard time dealing with women :cool:
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

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

Post 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.)
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

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

Post 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
User avatar
XBiscuits
Prole
Posts: 3
Joined: Fri Dec 01, 2017 4:05 am

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

Post 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
A Man who can fly a rocket to orbit will have a hard time dealing with women :cool:
User avatar
XBiscuits
Prole
Posts: 3
Joined: Fri Dec 01, 2017 4:05 am

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

Post 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??
A Man who can fly a rocket to orbit will have a hard time dealing with women :cool:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 5 guests