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
  • realtimesave @ realtimesave:
    jdhello
  • realtimesave @ realtimesave:
    @SylverReZ that guy seriously spent over 2 grand on that hot wheels pc?
    +1
  • RedColoredStars @ RedColoredStars:
    LTT is a noob
  • RedColoredStars @ RedColoredStars:
    Guy doesnt know wtf hes talking about half the time
  • realtimesave @ realtimesave:
    @SylverReZ that reminds me that my friend sells CRTs he gets cheap or free for hundreds of dollars. people are dumb
    +1
  • RedColoredStars @ RedColoredStars:
    And is incredibly annoying.
  • realtimesave @ realtimesave:
    man I just washed my shoes and they look exactly the same as before I washed them :|
  • realtimesave @ realtimesave:
    luckily they didn't fall apart
  • RedColoredStars @ RedColoredStars:
    Some CRTs are very much worth the price
  • RedColoredStars @ RedColoredStars:
    i used to go out to the recycling center every couple weeks and look at the crts and other electronics people would drop off. Usually screens were broken or severely scratched from being tossed around. Did find a good one here and there, but never anything like a 1080i widescreen crt.
  • RedColoredStars @ RedColoredStars:
    Or a good contition 40" Sony Trini that weighs 300 lbs. lol
  • RedColoredStars @ RedColoredStars:
    Literally 300 lbs. lolol
  • BigOnYa @ BigOnYa:
    I have a few of those boat anchors in my basement I tried giving away but no one wanted them, So anyone close to Columbus, Ohio area that wants them, (26", 2x 19") please come get, for free.
  • RedColoredStars @ RedColoredStars:
    Dont know anyone wants those smaller ones. Most are after larger sizes and the kinda top of the line models
  • RedColoredStars @ RedColoredStars:
    Motion handling and input lag on those things destroy plasmas, led, oled
  • realtimesave @ realtimesave:
    I had some really nice CRTs I should've kept
  • realtimesave @ realtimesave:
    now I have all lcd
  • realtimesave @ realtimesave:
    one in particular I regret getting rid of oh well :|
  • realtimesave @ realtimesave:
    the Sonys and stuff I don't care about
  • realtimesave @ realtimesave:
    and used LCD are hard to sell I can imagine.. not worth much
  • realtimesave @ realtimesave:
    @SylverReZ where do u lurk
  • a_username_that_isnt_cool @ a_username_that_isnt_cool:
    Is it piracy if it was released for free? Not in my opinion, but I also think it's not piracy if buying it isn't owning it, and it's not piracy if you can't buy it from the original creators anymore.
  • K3Nv2 @ K3Nv2:
    Free release can have loopholes where they still make money through ads
    K3Nv2 @ K3Nv2: Free release can have loopholes where they still make money through ads