(Help) Remembering last tile

I'm trying to have an "enemy" sprite move around, but I can't quite figure out how to have its last visited tile be swapped correctly. I can get it to always swap for "white," but then anything the enemy moves to gets erased, essentially. Here's what's it currently doing...


...yeah. I'm at a loss. I think it has something to do with the following:

currenttile = name event.tx,event.ty
	if currenttile=="hazard" then
		swap "hazard"
	elseif currenttile=="switchON" then
		swap "switchON"
	elseif currenttile=="switchOFF" then
		swap "switchOFF"
		swap "white"

Any help would be greatly appreciated. Here's the code:

Zelda Clone Test.zip (3.5 KB)

P.S. If anyone would be kind enough to briefly explain the difference between event.x and event.tx
I would also very much appreciate that. Thanks!

One trick you can do is make a bunch of frames on the enemy, then when it moves onto a tile, set its frame to the one you've assigned to be that tile. When it's time to move off of that tile, look at the frame of the enemy and replace with the associated tile (this will be a lot of if/elseif/elses).

Before you move the enemy to a tile, you can store the tileID of that tile in a variable, for example underEnemy. When the enemy moves away, just swap in underEnemy again.

A limitation of this is that the sprite can be only used once per room. If you’d use it more than once underEnemy would be used by multiple sprites, resulting in the whole thing not working correctly. One way to solve this is to duplicate the enemy sprite and only change the variable name.

And about the difference between event.x and event.tx: The first returns the x value of the tile it is currently called from, while the latter returns the x coordinate of the target tile of the player. It can not return the target tile of a sprite/item. Those tiles don’t really have a target tile, as they don’t move by default.

Thanks. So if event.tx is the player's x-coordinate, then what is event.px?

event.px is the Player's current x coordinate. See here for more: Playdate PulpScript

One more point for clarity, tx is where the player tried to move, their target tile. px and tx are only the same if the player didn’t just try to walk into a wall. :sweat_smile:

Would it be an option to tell enemies to avoid any space that is not "empty"("white")?

// moves the enemy tile to the new tile
	newtile = name x,y
	if newtile=="white" then
		tell x,y to
			swap "enemy"
		swap "white"

You would only have to keep this in mind during level design.

Otherwise, if you are ok with defining separate variables for each enemy, as in Kurcomy's suggestion, you could just store the enemy sprite as a player tile and draw it from the draw event. (see attachment). But then interaction/battle would be more complicated to do.

move enemy.json.zip (3.0 KB)

Thanks for the response, gummipferd. This is actually exactly what I did after playing around with the code for awhile. It works as a temporary solution. It does create two new problems, however. One, the "enemy" will never move to the same tile as the player (aka attacking them), and two, if I have two of these enemy sprites in the same room and they both jump to the same tile, one will be erased.

Are you sure about the "enemy" not moving on the player tile?
As far as I know, the player tile is on a different "layer", and

name event.px,event.py

should return whatever tile is below the player.

(Regarding your second problem, I cannot htink of any solution right now.)

Ok, I happened to think about your problem today.
One solution would be to create a separate "enemy" tile for every tile the monster can walk on.
Then your "enemy" ("white") code, after calculating the new x,y position would look like this:

tell x,y to
   call "StepOn"
swap "white"

Then in every tile, the "enemy" can step on you would have a StepOn routine.
In, say, the healthpack it would look like this:

on StepOn do
   swap "enemy healthpack

The tile "enemy healthpack" would be identical to "enemy". Just instead of "swap "white"", it would do "swap "healthpack""

In theory you could do a similar thing, but use frames of the monster tile instead of separate tiles.
However, there's currently a bug that prohibits saving the frame number in variable.

Can I have an example of this? There was a weird bug where you couldn't assign a variable to another variable with the word “frame” in the name but that should have been fixed sometime last week.

Weirdly I cannot reproduce it anymore.
I was referring to this bug:

I ran into this issue yesterday or so, this is why I searched the forums and found the above thread.
Maybe it had already been fixed by then and it was just sloppy work on my part. Would not be surprised :slight_smile: