Showing posts with label tutorials. Show all posts
Showing posts with label tutorials. Show all posts

Wednesday, May 3, 2023

How to keep Your Actors From Going Off Screen

A common question that I run in to all of the time on different Stencyl related forums and blogs is, "How do I keep my character from leaving the screen?".

We all need this feature for different reasons, at different times. Some may need to stop the player actor from leaving just the sides of the screen. This works for side to side shooters like Galaga, where the player is fixed into a single Y position and can only move on the X axis. Then there are others who may need to set boundaries for left, top, and right, but leave the bottom alone, for pitfall style games where you want your player to be able to fall in to traps or large holes.

My instance was a little more complicated than this. I am making a game that scrolls vertically. The player sprite starts at the bottom of the scene, and moves to the top of the screen. The scene is about ten times bigger in height than the standard 15 tile layout. This entails some challenges and obstacles to overcome.

First off, the default screen position is X=0 and Y=0. That would mean that the default screen position is aligned with the top left corner of the scene. Well, this doesn't do us any good, because our players roughly 5,000 pixels further down in the scene! This simple problem can be solved with the addition of a camera.

Blocks for creating a moving camera in stencylHow I solved this problem was to create a "Dummy Actor" that the camera would stay centered on. Placing the Dummy on the very bottom layer of the scene, under the background, makes him invisible to the player, but the camera will still follow him through the scene. I simply drew the dummy actor down at the bottom of the scene, right in what would be the dead center of the screen in a normal sized scene. I then drew the player actor about five tiles down, but two layers up, above the background image. Now, using a few blocks of code, we can make the dummy actor move through the scene and the camera will follow him the whole way. Since all this Dummy Actor will do is move through the scene at a set pace while the camera follows, for ease of use, I just attached these blocks in the actors events opposed to creating a separate behavior.

Now, we have our camera. If you were to test your scene at this point you would see that the camera does in fact scroll northward to the top of the scene at the pace that you set the dummy actor to move. The problem with this is that with the camera moving rapidly, but the player actor being stationary without input, it is very easy for the actor to fall off the bottom of the screen and become in active.

In order to fix this little dilemma of ours we need to incorporate some screen boundaries to stop our player actor from exiting the screen. If you followed the crash course tutorials on stencylpedia, then you should know how to set boundaries for the X position, but I will include the code anyways for those slackers out there who didn't follow those specific tuts.

Setting the Y boundaries can be a little tricky depending on the type of game that your creating. I know when I first started using stencyl, figuring out this whole boundary thing after adding a camera to the mix was a migraine headache in the making. But, with a little persistence I was able to come up with this:

How to stop your character from going off screen in stencyl

There's a few very important things to note about these blocks. One is that when you get to the Y coordinates, you don't want to use Y(on screen). You want to just use Y. The reason for this is that you will be using the Y coordinate of the camera as a starting point. The scene is 150 tiles at 32 pixels per tile. That's 4800 pixels for the height of our scene. What we did here, is we used Y of the camera to find the top boundary. That one's easy. Next, we used Y of the top corner of the camera + the screen height to find the bottom border.
Screenshot of Aero Fighter for SNES
Voila! We now have boundaries that our player character can not exceed! This method works well for Y scrolling games, like Aero Fighter, but the same might not be true for other types of games. In the next tutorial, I will show you how to modify this script to fit other game types. Until then, Cheers!

Monday, May 1, 2023

Character Building 101: How to Build a Level Up System in Stencyl

What is Character Building?


Character building is the backbone of every great RPG game ever made.It's not only for RPG's, though. Over the past decade we are seeing more and more games of different genre's dip into the realm of character building. Even first person shooters are beginning to adopt character building principles (think Borderlands).

To be concise, when I say character building, I mean that the particular game in question has a built in system where your character accumulates experience. That experience usually translates into higher attack power, higher defense, new magical abilities etc. This style of character building is more commonly known as role playing (RPG  - role playing game).

Another type of character building would be acquiring experience in one form or another, and then using that to purchase upgraded abilities (Dante's Inferno).  We will cover building a skill tree of that nature in another tutorial here in a few days.

Today, we are going to go in depth on the first idea I stated, a classic RPG style level up system. The system is pretty simple overall. We are basically going to create a character behavior to attach to our main actor, or playable character. This behavior will watch for one variable ($experience) to become greater than, or equal to, another variable ($NextLevel). Once the experience surpasses the quota for the next level, it will increment our level up one. It will then increment all of our other character attributes, like attack, defense, and max health. Once all of our character specs have been incremented, it will create a new $NextLevel value, so that the behavior can once again begin to watch for a new level up instance.

The Difference Between Getters/Setters and Game Attributes


Before we get started putting our blocks together, we need to discuss the difference between getter/setters, and game attributes. Getters and setters in stencyl are the equivalent of a variable with local scope in an actual programming language. For those of you who know a bit of programming, you already know where this is heading. For those that don't know, a variable with local scope will only function within the confines of where it's created. Sometimes this means one page of a website, or it could mean one particular function of a whole program. These types of variables are used for things that we don't need to pass on to the rest of the program.

A good example of when to use local scope in a game would be the boss battles in the old Mario games. Instead of coding a health bar and doing math to take damage, in a game like this, you could create a code block that checked how many times the boss has been hit, every time he gets hit. The code block increments the HitCount by 1. It then checks to see if the HitCount is greater than, or equal to, three. If this is the case, Bowser dies (sorry Bowser). All of this would be done locally as we don't need it to carry on to any other part of the game.
An example of how to increment damage for simpler games.

When coding a level up system, we have no use for local scope variables. They won't do us any good if they can't follow us around for the entirety of the game, no matter where the game takes us. But don't you worry your pretty little head. Like a good neighbor... global scope is there!

That's right. With a global scope, a variable and it's contents and be manipulated and called on from anywhere in the game. The stencyl equivalent of global scope is Game Attributes. To access these, while in the coding screen, click on the attribute category of blocks, and then in the sub-categories click on Game Attributes.

If we create all of our characters statistics with game attributes, we are now able to create a very simple code block that allows the game to constantly check to see if our experience is sufficient to level up. If we have enough experience to do so, that same code block will increment all of our character traits, increment the amount of experience needed to level up again, and then just for fun, it will also refill our health and energy meters to our new max health and max energy levels.



Implementation


Code blocks depicting how to create a level up system in stencyl.Now we have our very basic level up system. Our game now knows that once we accrue a certain amount of experience, to let us know that we are a little bit smarter than we were a few seconds ago (and stronger, too). Now, how do we start collecting all that experience?:Easy...peasy...Mrs. pacmans sleazy.

All we need to do is add a little bit of code to all of our enemies to define the amount of experience that they will give us if we completely and utterly destroy them (isn't that nice of them to do that?). Simply create another game attribute with a unique name, that can easily be identified as being the experience given for killing this particular enemy.


An example of how to retrieve experience from enemies in stencyl





Optionally, if you were building a much larger game where you may face the same enemies down the road, but they will be much stronger, you could increment the experience similarly to how I did in our level up blocks. You could also use this method to increment the traits of the enemy it's self, instead of creating a whole new actor with the same graphics.





What's Next?


My next few tutorials are going to walk you through the advanced mechanics of character building. Here are just a few of the topics that we are going to discuss:

  1. HUD's, or Heads Up Displays. I am going to show you how to implement health bars, energy bars, magic bars, etc.
  2.  I am going to get a little more in depth with the math behind  this level up system.
  3. Leveling up for RPG style games where you control more than one character, I.E. leveling up several playable characters separately (Final Fantasy Series).
  4. Skill Tree's ( I told ya so)
  5. Status Effects (poison, sleep, etc.)
  6. Stat boosting based on equipped items (think Diablo).
  7. Xbox style achievements
  8. And a whole lot more...