Homebrew [Help] [LÖVEPotion] How do I make it so that my character won't move past the screen?

DutchyDutch

COPYRIGHT LOLOLOLOL
OP
Member
Joined
Nov 16, 2014
Messages
954
Trophies
0
Age
24
XP
862
Country
Netherlands
Hi, yesterday I started developing in LovePotion for the 3DS. In about 20 hours I've already got the basics down... except for a few things. This glitch is one of them.

The problem is this:
I managed to make a top down game with a moving character and a scrolling background. When the dpad is pressed, the character moves up, and the BG moves down. This gives a scrolling BG effect. However, sometimes, my character moves past the screen, so you can't see him anymore. How do I fix this?

Source code:
function love.load()

background = love.graphics.newImage('background.png')
youngBoy = love.graphics.newImage('youngboy.png')
youngboyx = 0
youngboyy = 0
backgroundx = 0
backgroundy = 0
end


function love.draw()
love.graphics.draw(background, backgroundx, backgroundy)
love.graphics.draw(youngBoy, youngboyx, youngboyy)


end
function love.update(dt)

if love.keyboard.isDown("right") then
youngboyx = youngboyx + 1.1
end

if love.keyboard.isDown("left") then
youngboyx = youngboyx - 1.1
end
if love.keyboard.isDown("up") then
youngboyy = youngboyy -1.1
end
if love.keyboard.isDown("down") then
youngboyy = youngboyy + 1.1
end

if love.keyboard.isDown("down") then
backgroundy = backgroundy - 3.1
end



if love.keyboard.isDown("up") then
backgroundy = backgroundy + 3.1
end


function love.keypressed(key)
if key == 'start' then
love.event.quit()
end
end


If it's really necessary I could provide a short video of it pretty soon, just tell me.
Thanks for reading this!
 

c4388354

Well-Known Member
Member
Joined
Jan 23, 2015
Messages
142
Trophies
0
XP
623
Country
United States
before adding the 1.1, do a check to see if the current x or y position is lower then a certain value (like 240) if it is then add the 1.1, if not then don't do anything.

same when subtracting, make sure that the value is higher then a certain value (like 2), if it is then subtract the 1.1, if not then don't do anything.

maybe try something like:
Code:
if love.keyboard.isDown("right") then
    if youngboyx < 240: youngboyx = youngboyx + 1.1
end

also, you can combine the "youngboyy = youngboyy + 1.1" and "backgroundy = backgroundy - 3.1" part into one if statement.

example:
before:
Code:
if love.keyboard.isDown("down") then
youngboyy = youngboyy + 1.1
end

if love.keyboard.isDown("down") then
backgroundy = backgroundy - 3.1
end

after:
Code:
if love.keyboard.isDown("down") then
    if youngboyx < 240:
        youngboyy = youngboyy + 1.1
        backgroundy = backgroundy - 3.1

edit: fix codebox spacing
 
Last edited by c4388354,

DutchyDutch

COPYRIGHT LOLOLOLOL
OP
Member
Joined
Nov 16, 2014
Messages
954
Trophies
0
Age
24
XP
862
Country
Netherlands
before adding the 1.1, do a check to see if the current x or y position is lower then a certain value (like 240) if it is then add the 1.1, if not then don't do anything.

same when subtracting, make sure that the value is higher then a certain value (like 2), if it is then subtract the 1.1, if not then don't do anything.

<snip>

That worked perfectly! :D Thanks!
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    Xdqwerty @ Xdqwerty: https://youtu.be/5eLWoy0fFkc?si=1vvQ_2zAUlF1ThoD