Saturday, April 29, 2023

Introduction to Power Ups - How to boost your characters

Recently I stumbled across a question on quora.com about how to accelerate your character speed without having to write extra blocks of code to figure out exactly what direction the character is traveling.

How do I increase the acceleration of an actor in stencyl without having to know a direction?

This immediately triggered a response in my mind. It made me realize that a lot of people who are using stencyl haven't fully grasped the whole picture of what a simple variable can do! So, today, I am going to show you how you can use a couple of game variables so that your speed automatically adjusts it's self when you hit a power up in a scene.

The process of doing this is relatively simple.

1. Create your variables

You will need 6 total for this example. You will need:

  • One that contains the normal movement speed
  • One that contains the normal movement speed as a negative
  • One that contains the boosted movement speed
  • One that contains the boosted movement speed as a negative
  • One that is empty that can contain either of the positive speeds
  • One that is empty that can contain either of the negative speeds
  • A boolean variable (true or false switch) that we can use to trigger the power up
2. Create a block of code similar to these in your main characters behavior. Note that you will have to recreate this for every movement direction.
A Stencyl block showing how to form the Up button movement
A Stencyl block showing how to form the right button movement






3. Add another block to your character that looks like this.
A Stencyl block that shows how to adjust the movement speed of your character
What this block does is determine if the boolean variable $speedboost has been switched to true. If the variable becomes true, it changes the $speed variable for normal to godspeed. In this block I kept the normal movement speed at 10, and the boosted speed at 20. So when you run over the power up it doubles your movement speed. The "do after" block at the end makes it so that after ten seconds all of the movement speeds revert back to normal speed. Pretty clever!

4. Create a character that will be your power up item. 


Edit it's behavior to include a block like this:
(Note: Ignore the red flags in this block. Stencyl threw those up because I didn't add animations to this actor before creating the behaviors.)

Stencyl block that shows how to trigger the movement speed change when I power up is collided with

You may also want to add something to that block that kills the actor and removes it from the screen. I had planned to do that for this example but it slipped my mind. If anyone is unsure how to do that, just let me know in the comments and I'll cover it in another post.

That's all you need to do to change your movement speed without first calculating directions. Play around with those blocks and see what other kinds of cool things you can achieve with this principle. 

If this blog helps you out, or you just enjoy reading it, consider subscribing to my feed burner so you get immediate updates when this blog is updated. I am always looking for suggestions on what to post next that will everyone will get the most value from, so if there is anything in particular that you would like to me to explain, please let me know in the comments and I'll cover it. Thanks for reading!

1 comment:

  1. How can I do it so that the speed boost works more like a x2, x3 or x5 icon when pressed?

    ReplyDelete