Help determining tilemap dimensions

It just seems I am full of posts lately, but considering I am new to this platform, I hope you can understand. I want to make a game for the ‘traffic’ game jam, and my idea is to replicate something like Frogger (https://en.wikipedia.org/wiki/Frogger).

I have the concept of moving the player around, however I need to know more about the boundaries of the tilemap. The players starts at the bottom and the center of the screen. I first thought I could use screen.height - player.height, but in a tile map, this was above the bottom. Since there is no function to get the tilemap width and height, a manually set the player using the col/row positions.

The problem I am having is moving the player up and then down. From the start, when I move up once, the player is no longer overlapping a bottom tile, but when I move it down again, it can still move down again (so from start, up once and twice down). I am expecting that moves only once up and once down.

Can anyone explain why what is going wrong here?

I have improved the code somewhat to determine if the player has stopped.

To clarify, the behavior you’re seeing that you want to fix is that the marker can go off the tilemap? In this case, that would be because you’re setting the position of the target off screen – within the physics or when given small movements we don’t let the position go outside of the tilemap, but if you move a sprite by a larger amount it will yield / do what it’s told.

Because frogging is already 16x16, I’d probably say it’s easiest thing is just to base the movements off the tilemap grid – e.g. if moving up, instead of explicitly setting x and y for the sprite, instead grab the tile directly above frogging

and below the if / else block, set the position. (I also added a check to see if the new position is a wall, and not move in that case – that should fix the behavior you’re describing here of moving down multiple times):

image

Side note, in this section of the code

playerOnCurrentTile is already a tile location; you can just do

image

Also, I believe the sprite grid extension was made to handle movements like this; it might be worth checking out to see if it handles things more easily here?

image

2 Likes

Thank you very much for your support. Moving the player to the grid locations is indeed much easier. Also after discovering @Adri314 game, https://arcade.makecode.com/92804-31727-55284-01424, I realized how much simpler it is to use color-coded-tilemap as well, and how I will eventually move the enemies. Here is my revamped frogger:

I just got around to looking at how the Sprite Grid moves a sprite to a location.

/**
 * Center the given sprite on a given location
 * @param sprite
 * @param loc
 */
//% blockId=mapplaceontile block="place $sprite=variables_get(mySprite) on top of $loc"
//% loc.shadow=mapgettile
//% blockNamespace="scene" group="Tiles" blockGap=8
//% help=tiles/place
export function placeOnTile(sprite: Sprite, loc: Location): void {
    if (!sprite || !loc || !game.currentScene().tileMap) return;
    loc.place(sprite);
}

Which calls the place method within the Location class in Game.

/**
     * Center the given sprite on this tile
     * @param sprite
     */
    place(mySprite: Sprite): void {
        if (!mySprite) return;
        mySprite.setPosition(this.x, this.y);
    }

If I understand this correctly, this would place the sprite onto the new location within an game cycle. I want to move the sprite with speed. Another feature perhaps, something like: :thinking:

place $sprite=variables_get(mySprite) on top of $loc with $speed
export function moveToTile(sprite: Sprite, loc: Location, speed: number)

I want to thank you again for all your support, you are a great help in explaining the mechanics of the platform.