Working on a game for LOWREZJAM 2017 and I'm running into some issues with getting the background to scroll up to simulate movement while still maintaining the player's position.
Here is the current issue I'm encountering:
My issue is arising from the camera moving up along the drawn map and snapping back to the bottom when it reaches a certain y value. That works fine for the background but I'm not sure how to get the player to maintain their y position when the map snaps back to the bottom.
The code I've written is as follows:
function _init() --renders game at 64x64 poke(0x5f2c,3) cls() --creates variable t to store timer info later in code t=0 --establishes boundaries of the level level={ w=64, h=250, cy=0 } cam_x=0 cam_y=0 player = { sp=1, x=28, y=50, h=100, t=0, imm=false } end function _update() --autoscrolling code --moves both the level/background and the player at the same speed, to simulate movement if level.cy<level.h-64 then level.cy-=2 player.y-=2 end --statement that will trigger invincibility frames --inactive atm if player.imm then player.t += 1 if player.t >30 then player.imm = false player.t = 0 end end --timer and statement that flickers back and forth between sprites to simulate movement t=t+1 if(t%8<3) then player.sp=1 else player.sp=2 end --basic movement code if btn(0) then player.x-=1 end if btn(1) then player.x+=1 end if btn(2) then player.y-=1 end if btn(3) then player.y+=1 end end function _draw() cls() --camera that follows level's movement --also keeps player on screen camera(0,level.cy) if level.cy<-80 then level.cy=0 player.y=50 end --draws map, extended to -100 to allow for longer stretch of road map(0,0,0,-100,12,63) --draws player car spr(player.sp,player.x,player.y,.75,2) end |
I'm still fairly new to programming so good coding practices and math functions aren't things I'm entirely familiar with yet. If anyone could help me figure out what tools I should look into to solve my problem or different approaches I should be taking with vertical scrolling loops, I'd greatly appreciate it!
That totally worked, but now I'm running into the issue of the sprite flickering whenever that loop resets. What can I do to have the sprite stay consistent?
Think about happens on that final frame of the loop.
camera(0,level.cy) |
You position the camera.
if level.cy<-80 then level.cy += 80 player.y += 80 end |
Then you subtract 80 from each value (making it loop)
And then finally
spr(player.sp,player.x,player.y,.75,2) |
You draw the player.
You positioned the camera BEFORE you looped, and you drew the car AFTER you looped, so the camera doesn't catch up until next frame.
Change the order that things happen in.
Do your math first, THEN set the camera, THEN draw the car.
Oh man I'm a dingus. Still need to get used to the concept of order of operations as far as lines of code being processed. Thanks for the help!
[Please log in to post a comment]