Cart #blocks-0 | 2024-01-25 | Code ▽ | No License

My second progress update:

I'm trying to implement the ability for my player character to create temporary blocks which can be used as platforms to reach otherwise unreachable areas. The idea is that the bunny player character digs up earth which piles up behind him and then disappears after a few seconds.

1. When the player presses the o button while on a ground tile, a 'newblock' is added to a table so that it can be deleted after its set duration. In order to ensure the original map tile is restored after the 'dirt block' disappears, mget is used to grab the original tile sprite as 'oldblock.'

`if btnp(🅾️) and p.landed then newblock={} newblock.spr=14 newblock.x=p.x newblock.y=p.y newblock.age=0 oldblock = mget(flr(p.x/8)-1,flr(p.y/8)) add(blocks,newblock)`

1. The block is drawn according to the coordinates stored in a table. The block is drawn behind the player based on whether the character sprite is flipped.

`function draw_block() for myblock in all(blocks) do if p.flp then mset((myblock.x/8)+2,(myblock.y/8),myblock.spr) else mset((myblock.x/8)-1,(myblock.y/8),myblock.spr) end end end`

1. The block is deleted from the table after it's aged past its duration and mset replaces the block with the original 'oldblock' variable.

`function update_block() for myblock in all(blocks) do myblock.age+=.1 if myblock.age>10 then del(blocks,myblock) mset((myblock.x/8)-1,(myblock.y/8),oldblock) end end end`

Problems

There are multiple problems I'm encountering.

1. The dirt block doesn't always draw directly behind the player sprite but sometimes 2 tiles away.

2. Two blocks are drawn if the player happens to move in the opposite direction immediately after the first block is drawn.

3. The original map tile is not always stored and replaced. Frequently, the 'oldblock' variable will store the adjacent tile sprite instead.

4. If the button is held down, multiple dirt blocks are drawn and only the final one appears to delete.

Gratitude

If anyone has any insight into how to fix these problems, even it is simply nudging me in the right direction or pointing to a resource, you have my eternal gratitude. I'm still a complete novice at this. Thank you.

P#140599 2024-01-25 16:28 ( Edited 2024-01-25 16:53)

2

Firstly, and I wish this was either the default or stated in a place that was easier for beginners to find, the reason holding down the button causes multiple to draw is because of the engine's button repeat time. To fix that, put this at the top of your code in any tab:

 `poke(0x5f5c,255)`

Next, the first issue I noticed from your code is that you're separately treating values that should be the same. The x and y values on the new block are only ever treated as map coordinates, so they should be set as map coordinates. That way, you can then use them directly when fetching the sprite for oldblock and when drawing them. You should also check which way the player is facing only when placing the block, not when drawing it.

Lastly, your code for drawing the block is changing map data. That's not a bad way to make the block appear, but it shouldn't be done during the drawing code. It should be done once when placing the block. That combined with check for the player's direction is probably why you got the double block bug.

P#140624 2024-01-25 22:54

Thank you @kimiyoribaka! I think I was able to follow your instructions and it's all but entirely fixed now (about 5% of the time, it resets the wrong map tile with an adjacent one). I probably need to tweak the math a bit, but I'm more than satisfied with how well it works at the moment. Thank you!

Cart #blocks-1 | 2024-01-26 | Code ▽ | No License

P#140664 2024-01-26 19:48