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
25
XP
872
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
634
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
25
XP
872
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
  • Skv0ra @ Skv0ra:
    yep
  • Skv0ra @ Skv0ra:
    screenless ones are plenty
  • Skv0ra @ Skv0ra:
    i got the $99 one years ago, and just need to remember where tf i put it from never really using it
  • SylverReZ @ SylverReZ:
    @Skv0ra, Wacom is that drawing tablet manufacturer?
    +1
  • SylverReZ @ SylverReZ:
    They're expensive but really good.
    +1
  • UltraHurricane @ UltraHurricane:
    i just really like having a portable all-in-one device to draw on and take anywhere, i was tempted to get a Microsoft Surface but a friend of mine had a real bad experience with them
    +2
  • SylverReZ @ SylverReZ:
    At least better than most cheap alternatives anyways. :D
  • Skv0ra @ Skv0ra:
    @UltraHurricane,
    ANYTHING M$ is an awful experience, period
    +1
  • UltraHurricane @ UltraHurricane:
    the Galaxy Tab already uses the same digitizer tech as wacom's, i even use a wacom stylus with mine
    +1
  • UltraHurricane @ UltraHurricane:
    and it has Clip Studio so i was already sold on that heh
  • Skv0ra @ Skv0ra:
    when a software company sticks its nose into shit they haven't a clue about, like h/w...
  • Skv0ra @ Skv0ra:
    @UltraHurricane,
    noice, but Samsung
  • Skv0ra @ Skv0ra:
    any other modern android tablets that do that?
  • UltraHurricane @ UltraHurricane:
    the only ones i can think of lately are XP-Pen's, Lenovo Tab P12 and the Oneplus Pad but i don't like how lower spec they are in other areas
    +1
  • Delta984 @ Delta984:
    good afternoon
    +1
  • Plazorn @ Plazorn:
    Good morning
    +1
  • Plazorn @ Plazorn:
    How are you today @Delta984 ?
    +1
  • Plazorn @ Plazorn:
    I guess I’m somewhere between a 6 or 7 out of 10
    +1
  • Xdqwerty @ Xdqwerty:
    I'm gonna tell you something
  • Xdqwerty @ Xdqwerty:
    something
    +1
  • AncientBoi @ AncientBoi:
    I'm in a 7/8ish mood for some reason. :)
  • Delta984 @ Delta984:
    @Plazorn pretty good
  • Sicklyboy @ Sicklyboy:
    wew lad
  • lwiz @ lwiz:
    Time to switch PS4's from my old slim to my "new" phat. Kinda annoying that the whole PS4 scene consists of only twats, very little good and extensive text documentation can be found anywhere :blink:
  • lwiz @ lwiz:
    (Twats - persons posting oneliners in twatter)
    lwiz @ lwiz: (Twats - persons posting oneliners in twatter)