i figured out how to flip the sprite on its x axis but cant figure out how to make the car face up or down when the UP or DOWN buttons are pressed. the car moves up or down but i cant get it to point up or down. how could i get this to work?
If you need to have only 1 sprite for the car, this thread has code that allows rotating the sprite:
https://www.lexaloffle.com/bbs/?tid=38548
The technique in that code does use up a map space though.
If you're okay with using a couple more spots on the sprite sheet, the easier way is to just make a sprite for the car facing up or down and give your player table an entry for the sprite. From there, the way to get the code to determine which sprite to use would be the same as you've already used for flipping.
the spr
function only lets us flip a sprite horizontally, vertically or both, but not rotate 90°, so we have to prepare a separate sprite that looks down (and another that looks up, for people or some vehicles — your car sprite would be okay with the same sprite to go up and down). you can use an advanced function like the one kimiyoribaka pointed to, then you get many possible angles (but not all sprites look good rotated, so need to experiment!), or if you only want a few directions (4 or 8), then preparing different sprites is still the simple solution.
@kimiyoribaka I’m relatively new to coding, so could write out how that would look? The part about making other sprites for up and down and implementing them like I did with flipping the sprite
Well, the other sprites would be made in the sprite editor. Your code currently has the car as sprite 1, so you would need to draw the vertical version of the car at the spot for a different number on the sprite sheet. I'll use 2 as the example so you can see it in the code.
From there the code would look like this:
function _init() player = {x=60,y=60,flipx=false,flipy=false,sprite=1,} end function _update60() if btn(0) then player.x-=1.5 end if btn(1) then player.x+=1.5 end if btn(2) then player.y-=1.5 end if btn(3) then player.y+=1.5 end if btn(0) then player.sprite = 1 player.flipx = true player.flipy = false end if btn(1) then player.sprite = 1 player.flipx = false player.flipy = false end if btn(2) then player.sprite = 2 player.flipx = false player.flipy = true end if btn(3) then player.sprite = 2 player.flipx = false player.flipy = false end end function _draw() cls(7) palt(15, true) -- beige color as transparency is true palt(0, false) -- black color as transparency is false spr(player.sprite, player.x, player.y,1,1,player.flipx,player.flipy) end |
Basically, it's just a matter of making sure your update code includes all the ways the drawing of the car could be different and your draw code plugs them all in. There's also ways to make this much more concise, but it's important to make sure you've got working code that you can read before trying to make it better.
[Please log in to post a comment]