The last article featured my write-ups as I followed along to two of SquidGod's Playdate tutorials listed as resources on Playdate's official website.
This article is a write-up of the third tutorial, where I'll be shown how to make:
- Directional player sprites
- A short melee attack
- A simple enemy pathfinding AI
- Lock a room until all enemies are defeated
Directional player sprites
This seems simple enough!
- Create four Player tiles, one for each facing - really, it's just the eye sockets that move or disappear
- Write an
on update doevent listener
- Leverage the
event.dyvariable members to determine which direction the player is facing
- Swap for the appropriate tile in each condition
A short melee attack
- Create four Item tiles, one for each sword facing
- Write an
on confirm doevent listener, which triggers when the
Abutton is pressed
- Create two variables that store the eventual position of the sword tile
- Leverage the same
dyvariable members to store the appropriate tile name to be used
- Swap the tile, wait a bit, then swap it back to white
Fix bug: swinging at walls
- Move the sword-swinging code into a custom function
- Store the name of the tile being swung at
- If it is
enemy, swing the sword
Fix bug: don't move during sword swing
listenfunctions to essentially disable and enable player input before and after sword swings
A simple enemy pathfinding AI
- At a set interval...
- Determine the position of the target tile...
- Ensure it is available...
- Swap tiles only when available
- 3 variables
- 4 conditionals
- 4 event members
- 2 tile swaps
- I made
enemya sprite, as the author did
- When playing the game, all enemies instantly moved toward the player
- I checked everything to ensure my code matched the author's
- After several minutes, I tried one last thing: change the tile type from Sprite to Item
- That worked!
- I'm not sure why Sprites and Items behave differently with respect to intervals and the game's
- But now I know it's something to be mindful of
- Time to duplicate a bunch of code from the previous tutorial
- It was fairly easy to toggle between games and copy code snippets
- It was wonderfully simple to export and import layer tiles
- Just a bummer that the names didn't transfer
- After play testing, my game feels nearly identical
- The only difference seems to be a delay in shake after colliding with an enemy: it happens immediately in the video, whereas in mine it seems delayed
Lock a room until all enemies are defeated
- I imported the gate sprite from the other game, placed it in the room, and added the custom event to make the gate disappear
- I added
on enter doevent listeners to the
- I set or updated the same variable appropriately in each function
- I updated the
playerscript to adjust that variable and trigger the custom event on the
gatewhen the variable is 0
- Play testing confirms everything works as expected!
- That was another really fun tutorial!
- When broken down, all of the code is fairly simple: checking tile type, checking positions, incrementing or decrementing values, calling functions from other functions inside conditionals
- I'm glad I took the time to re-create all the code and play test along the way
Next up - you guessed it - another tutorial!
Top comments (0)