As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

27,966

Views

Beginner

Level

17

Comments

Chapter 3 Challenge - The Solution

by Jon (Updated on 2015-10-26)


This is the solution to the Chapter 3 Challenge. We'll walk you through each step.

Return to the Chapter 3 Challenge

Download the Solution

Download

To install the project, either drag the file into the Stencyl window from the Welcome Center or import it from the File menu (File > Import Game…). Requires Stencyl 3.4.0 or later.

breakout-screenshot

Contents

  • Part 1: Setting Collision Groups
  • Part 2: Making the Two Way Movement Behavior
  • Part 3: Keeping the Paddle On Screen
  • Part 4: Creating the Destroy Block Behavior
  • Part 5: Making the Launch Ball Behavior
  • Part 6: Recycling the Ball Actors
  • Part 7: Putting it All Together

Part 1: Setting Collision Groups

1) To start, we need one new Collision Group, one for the Ball.

2) Next, we need to set our paddle Actor to the correct group. Which do you think it should belong to?

3) The group our Ball Actor belongs to should be easy to set.

4) Lastly, we want to ensure that our Ball Actor can collide with our paddle Actor when we set the Collision Groups for each of them.

Part 2: Making the Two Way Movement Behavior

Let’s start with the Behavior used to control the paddle.

1) We want it to allow the player to move the paddle right or left at a set speed. We also want it to stop moving when the player isn’t pressing any keys. You’ll need to use blocks that control the Actor’s horizontal speed and ones to check which keys are pressed.

Here’s a snapshot of the solution.

stencyl-design-mode-two-way-movement-behavior-example

Note: We use otherwise if instead of a series of if blocks because doing so improves performance. When the game sees otherwise if, it only evaluates the boolean (the true/false condition) rather than the complete chunk of code, thereby reducing how much it has to process.

2) One thing to remember is that when the player is pressing the key to move the paddle left, its x-speed is set to the negative of the Top Speed value.

2d-game-coordinates

Remember, as you move down the screen, the Y value gets larger, and as you move up, it gets smaller. As you move to the right, the X value gets larger, and as you move left, it gets smaller.

3) Next, the last part of the Behavior tells the game exactly what to do when the player isn’t pressing any keys.

4) The Attributes in the Behavior to make it easy to configure. Some obvious choices are Top Speed (of the ball), Right Control, and Left Control. Based on what they do, you should be able to figure out their types.

Part 3: Keeping the Paddle On Screen

Normally, when two Actors collide, they push each other in opposite directions. In our game, though, the paddle shouldn’t move when the ball collides with it. We need to do two things. First, we need to change how our paddle Actor responds when it collides with other Actors.

1) Open up the paddle Actor, click the Physics tab, then under the General tab, set the What Kind of Actor Type? option to Cannot be pushed. Now the Actor won’t move when the ball hits it.

stencyl-actor-physics-cannot-be-pushed

2) Although this setting keeps the ball from knocking the paddle off screen, our paddle Actor will now go right through tiles. We need a Behavior to fix this. The Behavior should check the position of the paddle Actor, and if it moves beyond certain boundaries, move it back into the correct bounds.

Here’s a screenshot one one that will do just that.

stencyl-design-mode-restrict-movement-example

Note: This Behavior “always” checks the position of the paddle relative to the walls of the Scene (the screen size is the same as the Scene size in our game). If we try to move the paddle Actor past those bounds, the Behavior adjusts the Actor’s position.

The screenshots below show how we figured out the boundary coordinates.

restrict-movement-behavior-tile-coordinates-explanation

The coordinate above is as far left as we want our paddle Actor to move.

stencyl-restrict-movement-tile-coordinates-example-right

The coordinate above is as far right as we want the paddle to move.

Part 4: Creating the Destroy Block Behavior

Now that we’ve got the Paddle’s movement under control, we need to tell our game what happens to a block when a ball collides with it.

1) Let’s go over what we originally planned: When the ball hits the block, the block should flash, make a sound, and then vanish.

2) You also want to ensure this only happens when the ball collides with the paddle.

Here’s the Behavior that makes this happen.

stencyl-design-mode-destroy-block-example

Note: The only thing you might not expect is the do after block that delays the sound effect and the block’s destruction. We need that delay so the player can see the color change effect in action.

Part 5: Making the Launch Ball Behavior

We have a paddle the player can move and blocks that break, but we need a Behavior to launch the ball from the paddle. Although we could make a Behavior that lets the player launch new balls as quickly as he or she can press a key, adding a short delay keeps the player from spamming the controls.

1) To start, you’ll need blocks that create our Ball Actor and one that sets it in motion.

Here’s a screenshot of the Behavior that does this.

stencyl-design-mode-launch-ball-behavior-example

Note: To be clear, although the when created block is shown in the image above, it’s actually in a separate Event in the Behavior.

2) The Behavior only allows the player to launch a new ball once per second. Notice the Wait? boolean the Behavior checks? The Launch Ball key only launches a new ball when Wait? is set to false.

3) In addition, to improve the game’s performance, we use Recycled Actors. Also, when creating our Recycled Actor, it’s important to create it at coordinates offset from the paddle Actor’s current position.

Part 6: Recycling Our Ball Actors

Because we’re creating Recycled Actors, we need to use another Behavior called Recycle Self to recycle our Ball Actor.

1) This Behavior only involves two blocks. What condition should apply when the ball needs to be recycled? That should give you a hint as to which Event to use.

Here is a screenshot of the solution.

stencyl-design-mode-recycle-actor-example

Note: We want to recycle the ball when it’s off-screen, so we use an Actors Event that checks when our Ball Actor leaves the screen.

Part 7: Putting it All Together

Now we’ve got all our the game assets we need, let’s put everything together.

1) Our paddle Actor Type needs 3 Behaviors. Out of the 3 we made, which are they?

2) Our Ball Actor Type only needs one Behavior. Same goes for our Brick.

3) Test our game!

stencyl-breakout-screenshot-final

Note: If something doesn’t work right, start with the Collision Groups. Make sure each Actor Type belong to the correct Group. From there, look over the Behaviors and compare the ones you made to those in the screenshots. Lastly, make sure each Actor Type has the correct Behavior(s).

On to Chapter 4

Now that you’ve learned more about Actors and how they can use Behaviors, let’s move on to Chapter 4 and talk about Scenes.

Continue to Chapter 4

Print Article Edit Article How to Edit an Article
Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

17 Comments

Chris24XD
@STimroth Everything changed after that version.
0 2 years, 10 months ago
Chris24XD 2
Nice solution but update the page.
Because recycle it's kill and other things make people mixed up.

1 3 years, 1 week ago
csmithson
I am looking for the answer to the part 6, the blue recycle self block is no where in my right list. Anyone come up with an answer? Is it obsolete with the new version 3.4?
1 7 years, 2 months ago
Osman2001
I didn't even need the solution to make the game on my own. I did it in 50 minutes.
1 8 years, 1 week ago
Whiteredeye
Never mind, I deleted the file and downloaded it again and now it works fine.
1 8 years, 7 months ago
Whiteredeye
are the files compatible with the current version of Stencyl? I unzipped and added it to my games directory, and it appears as an option when I click to create a new game, but Stencyl crashes if I try to open it.
1 8 years, 7 months ago
STimroth
Oh the train of thought in these things, Since these are behaviors, when you go to apply it to the paddle itself, the attributes associated with the behavior has the launch speed incorporated in it. Now I don't know if I still need the created attribute I put in there to make that show up. Some clarification of that would help future programmers.
1 9 years, 1 month ago
STimroth
In Part 5, in the screenshot Solution, you have a Launch Speed in the push block, I am assuming this is an attribute? I then assume I need another event to set Launch Speed block to something. You have no mention or explanation of how to setup that part. I see this as an error in the tutorial/solution.
2 9 years, 1 month ago
STimroth
I Withdraw my last comment, with one addition, the recycle self screenshot should either be replaced with the 3.0 equivalent, or if you wanting to show both versions, a screenshot solution of both versions. That was very confusing, and since their is no mention of a revamp in this tutorial solution a waste of time. I am assuming the blue Block Kill Self would be the new version answer?
1 9 years, 1 month ago
STimroth
In part 6, the blue recycle self block is no where in my right list. Is it an attribute I have to create? I'm using 3.0 and I was wondering if something was change?
3 9 years, 1 month ago

Sign In to Comment