274,373

Views

Beginner

Level

119

Comments

Crash Course 2: Invaders - Part 8

by Hectate (Updated on 2015-02-01)


Part 8: Making the Ship Move

We've added an Event to the scene to play our music, now let's add an Event to our player's Ship so they can move it around!

Step 48: Click on the Dashboard tab, then on Actor Types, and select our Ship Actor Type. Now that we’re in the Actor Editor and can edit our Ship, click the Events tab.

Step 49: What we are looking at is Design Mode again, this time for the Ship. We’re going to create an Event that will allow us to move our Ship both left and right in the scene by pressing keyboard keys. To do this, we need to specify what happens when the player presses certain keyboard keys AND what happens when the player isn’t pressing anything.

To start, click the + Add Event button in the Events pane on the left, select Basics, and then choose When Updating from the three options shown.

The following block will appear in the work area:

Step 50: This time, rather than dragging a block over from the Palette, we’re going to use the Block Picker. Right-click anywhere in the workspace area, mouse over the Place a Block option, then over the Flow category, then click on the if block. That block will appear in the work area where you first right-clicked.

Step 51: We drag the if block into the Always block so it snaps in place.

Step 52: Now we need to select the right block to go inside the hexagonal field in our If block. Click the empty hexagonal field (this shape is always for a boolean, i.e. a value that can be true or false, in Stencyl) in the block and a new dialog will pop up. Mouse over the User Input category, and then choose the Control is Down block.

The Control is Down block will appear for us in the If block, as shown below.

Step 53: The Always block will constantly run through all the logic inside of it while the game runs. Thus the if block inside checks (using a boolean) whether something is true constantly while the game runs to allow or disallow the code that it contains to run.

As a result, our Control is Down boolean detects whether a control key is pressed or not to let code inside the If block run. We do, however, need to choose which control key to check. Click the Control dropdown on the block and select Choose Control.

From the dialog that appears, choose the right option, as shown.

Again, the block will change to reflect this choice.

Step 54: Next, we need to go to the Actor category of the palette and select the Motion sub-category and find the Set X Speed to [0] for Self block, as shown below.

This block will allow us to control the ship's speed. Drag it over to the work area, and snap it inside the if block's empty space so it looks like the image below.

Step 55: We now need to create an Attribute that we can use to adjust the value for x-speed. By doing this we won’t need to edit the speed value directly on the block in our Event every time we want to tweak it. To do this, let's click the Attributes category on the Palette, then click the Create an Attribute button.

In the dialog that appears, we will give our new attribute a Name of "Ship Speed" and set the Type to "Number". Click OK to continue.

Now you’ve got a blue block for the Ship Speed Attribute that you can set to different numeric values. We’ll show you how to set its value later when we complete this Event.

Step 56: Click on the empty number field in the set x-speed to [ ] for [Self] block, select Attributes in the pop-up dialog, and choose the one we just created, Ship Speed.

As you would expect, the block will appear in the field.

Step 57: Next, go to the Flow category, Conditions sub-category in the Palette and drag the otherwise if block over to the work area.

Snap the otherwise if block under the if block inside the Always Event block, as shown.

Step 58: Now we need to set up what happens when the user presses the left key instead of the right key. Get another Control is Down block and set it to left. We now need another "set x-speed to [Ship Speed] for [Self]" block.

Tip: To easily duplicate a block, hold down ALT and click-and-drag the block you want to duplicate. If there are other blocks nested inside of the one you drag, they will be duplicated as well. You can use this to quickly copy a whole stack of blocks!

Once we have our second x-speed block with the Ship Speed inside of it, we need to make a slight change to have the Ship move left. Because positive speed values move an actor to the right, we have to use a negative value to make the actor move left instead.

To do this, insert a negate block (it's in the Numbers & Text category, Math sub-category) to the field where Ship Speed would go, then replace Ship Speed inside the negate block. This block will take the number inside of it and make it a negative; thus making our Ship Speed work for moving left without needing a second number.

Step 59: If we were to set Ship Speed to a value and test now, we could press left and right to make our ship move in those directions at the desired speed. However, we'd quickly notice an issue; it won't stop moving!

This is because our logic sets the X-speed to a value, but never resets it to 0 when a key is not being pressed. To fix this, we need to add one more conditional block. Find and insert another otherwise if block below the otherwise if block shown previously. Then add the < > and < > block (under Flow -> Conditions on the Palette) to the empty hexagonal field.

Step 60: In the [ ] and [ ] block, we click on the empty field, select Comparison from the dialog, and then the not < > block. We need to do this for both empty fields.

We should get the following:

Step 61: Add both a [right] [is down] and a [left] [is down] control block to each empty field.

Step 62: Grab the set x-speed to [ ] for [Self] block again, click the [x-speed] dropdown on the block, and set the value in the field to 0. Then snap it in place. Here's the complete Event.

Ship movement pic

Step 64: The last thing we need to do is set the Ship Speed value. Click on the Attributes tab at the bottom of the entire Palette.

Then set the Ship Speed Attribute’s Default Value to 20.

Step 65: Test the game to make sure the Ship moves from left to right and stops when you let go of a key. Now that you’re more familiar with Design Mode, it's time to move on to more Events. Our ship can move left to right, but there's nothing to stop it from disappearing off screen if you attempt to move it past the screen's edge. Our next task in Design Mode will restrict the Ship's movement.

 

 

Let's move on to Part 9.

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!

119 Comments

Briano
Nevermind *facepalm* I screwed up the coding.
-2 13 years, 8 months ago
Briano
Can someone help I'm having the same problem as JD-Maker, The "Ship" Actor always stays in the upper left corner, and whenever I press the right arrow key on my keyboard it moves right then returns to its corner.
-2 13 years, 8 months ago
danielpis
you can easily copy created events with alt drag
-1 13 years, 8 months ago
Fitch
Skioski you missed a step. When choosing the "set x to []" look at the tabs above it. choose motion -> "set x-speed of Actor"
0 13 years, 8 months ago
Skioski
I can't set ship speed or anything to self just actor and I can choose an actor
0 13 years, 8 months ago
JD-Maker
I have a problem when run test:
my ship appears in the upper left corner of the screen, and when release the key the ship appears in the start site.

I am having many problems with the running of the program, for example the Attributes tab at the bottom of the Palette, not appear.

I have a Windows 7.

0 13 years, 9 months ago
BelgianRockfan
Never mind, the next step fixed it.
0 13 years, 9 months ago
BelgianRockfan
My ship can move offscreen, and once it's gone, it cannot come back.
Is this normal or did I do something wrong?
Also, I think my screen doesn't show the complete scene, because the first time I made the scene I had to move my ship because it wasn't shown when I tested it.

0 13 years, 9 months ago
ThatSmartDUde
I figured out why the ship wasnt moving! It sets the x - speed to the Ship Speed; which is default 0. So simply by Going in to the attributes tab, and clicking on setters, you can see a set ship speed to 0, drag that in just after the always and set the speed! (I set mine to 20)
0 13 years, 9 months ago
DawnWelkin
I have the same problem as BellyBoy2010 and dan76 . I even went so far as to delete ship speed and do it again as New ship speed and the same problem persists, there is no way to alter the values of the ship speed atribute in the way described. The screen shot you have in the guide does not exist in my download as of 7/10/12 at 656 pm tucson az time following the steps provided.
0 13 years, 9 months ago

Sign In to Comment