Workshop › Zelda-style Dungeon › Step 7 of 22
When the middle of the hero leaves the room through a gap, the room next to it is loaded, and the hero appears on the opposite side, as if walking through the doorway.
```js
// Walking out through a gap in the wall: into the next room, on the opposite side.
const cx = player.x + SIZE / 2
const cy = player.y + SIZE / 2
if (cx < 0 || cx > COLS * T || cy < 0 || cy > ROWS * T) {
const rx = room.rx + (cx < 0 ? -1 : cx > COLS * T ? 1 : 0)
const ry = room.ry + (cy < 0 ? -1 : cy > ROWS * T ? 1 : 0)
if (cx < 0) player.x += COLS * T - 4
if (cx > COLS * T) player.x -= COLS * T - 4
if (cy < 0) player.y += ROWS * T - 4
if (cy > ROWS * T) player.y -= ROWS * T - 4
enter(rx, ry)
return
}
```cx, cy is the middle of the hero; the room is COLS * T pixels wide and ROWS * T high.rx - 1), and so on.At the end of update, walk into the next room.
Skills: Game states