- Making a game with Flash Develop + Flixel: Part 1, Installing and Configuring
- Making a game with Flash Develop + Flixel: Part 2, Coding a simple example
- Making a game with Flash Develop + Flixel: Part 3, Input and Objects
- Making a game with Flash Develop + Flixel: Part 4, Tilemaps and Collisions
- Making a game with Flash Develop + Flixel: Part 5, Objects and Animation
- Making a game with Flash Develop + Flixel: Part 6, Cameras and Text
(Want to bypass the long explanations and see the final product? Here, download this post’s code as a ZIP file.)
Now that we have the ability to show messages to the player, we also have a problem. If we want to show lots of text, we will need to both write it and have it displayed from its associated person or place. Yet, how do we do that?
Of course, we could write it all in the PlayState. Each response and every possible message could be written and then added to the showDialog array when needed. However, we would still need to associate the text with a person.
One solution to this problem, then, involves actually adding new people, non-player characters, as separate objects. Each one could have their own responses to the player, which could be triggered when needed. This would allow us to give each NPC, if we wanted, different information, appearances, and even properties.
Building from our PlayerSprite, we can create a NPCSprite with the following properties in mind:
- Has an image
- Has collision
- Has speech
And, because we want the ability to draw and update them, we can build a new FlxGroup: NPCSpriteGroup. Then, because FlxGroups can hold other groups too, we can copy and paste the dialog code from PlayState into the code.
However, even with this code, there is a new and complicated problem. It’s on line 28. We want player to be stopped, but “player” doesn’t exist in this scope. It’s in PlayState, not NPCSpriteGroup.
One solution — there are many — is to move player to a larger scope, something that both PlayState and NPCSpriteGroup can see. For that, we can use a Registry with variables declared to be “static public” (singleton).
(Note: While not always ideal for every project, a Registry pattern for this project is fine. Without much complexity, we can manage the few items in the registry without risking putting too much code in there.)
Because of that, we need to change PlayState.
And, because player is now in the same scope as everything else, we can move the simple collision code for our map into its object too.
Finally, we can move to add a NPC.
Within NPCSpriteGroup, we can embed the file and then add the collision code. However, this time we can use the third argument of Flxg.collide to list a callback function. Whenever Registry.player collides with the NPC, we can check (within this callback function) to see if “ENTER” was pressed too. If so, we display the speech of the NPC.
Now, if we press “ENTER” while colliding with the NPC, we get its speech.
That’s it for Part 7.
In Part 8, we finally get to making different states and then using them as both menus and rooms.