Log In  

Let me explain.

I'm a new to PICO-8 (this is my first post actually) but not programming. I'm looking to develop games on PICO-8 to improve my programming skills. That being said, I'm following a tutorial to make a small breakout clone, and I've decided to give it my own flair in several ways, one of which is giving the ball a smooth animation when it bounces off of a wall or the paddle. It works swimmingly when bouncing off of a wall, and I was able to make it animate when bouncing off of a paddle by forgoing the tutorial and adding my own rudimentory collision detection. The collision detection isn't the greatest, but it works and I've gotten the ball to animate when bouncing off of the paddle in the past. Today, I decided to finish up the animation by making it only play when the ball is within a certain range of the paddle. As soon as I added this functionality, the animation stopped playing, as shown in the gif below (the animation of the ball bouncing is slowed down since it's an admittedly very small detail in normal game play)

This is the code in question:

  if inrange(mn,-1,1) then print("1",75,0,7) ball_spr = pad_spr end
  if inrange(mn,-2,2) then print("2",75,0,7) ball_spr = 18 end
  if inrange(mn,-3,3) then print("3",75,0,7) ball_spr = 17 end
  if inrange(mn,-4,4) then print("4",75,0,7) ball_spr = 0 end

Note the inrange function, it's basically the equivalent of a function I wish PICO-8 actually had which is "if mn >= -1 and mn <= 1". I've tried replacing the function with the actual equivalent code and it still does not activate the latter command, ball_spr = X. But, although I didn't show it in the GIF, I can confirm it activates the former print command. So what did I do wrong? Why is this function activating the print command but not the one after it? Keep in mind that without the print function it still doesn't work.

Here's the code in question. Note that the parts I believe to be unimportant are cut out because I don't want to release all the code just yet.

P#80570 2020-08-09 21:00 ( Edited 2020-08-09 21:01)

Just curious, but how come inrange() has a FLIP call inside of it?

P#80573 2020-08-09 22:18

Sorry for late response, but that's something I put in specifically to slow down the game for the GIF (and also for debug so I could make sure the inrange function was even being called)

P#80679 2020-08-12 18:58 ( Edited 2020-08-12 18:58)

I recommend you reverse the order of the statements.

Your code is completely correct in that each statement does what you likely intended. However, the flow is wrong. If mn is in the range -1 and 1, then it will also be in the ranges -2 and 2, -3 and 3, and -4 and 4. As a consequence, if the code does anything at all, it will result in ball_spr being equal to 0. The simplest way to fix this is to make the biggest range be checked first so that it will still run more than one of the branches, but the one with the smallest range that is still true will be last, thus also being the one with lasting effect.

Note, that it'd be generally better on an efficiency level if you use else blocks to ensure only one branch actually occurs, but that would likely make the code harder to read and use up more tokens.

P#80736 2020-08-14 06:05

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 21:30:27 | 0.006s | Q:16