161,281

Views

Beginner

Level

102

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!

102 Comments

camu
"When updating" works the same as "Always". If your ship wont move (right or left or both) just make sure all the blocks are stuck together by moving the "When updating" block. The editor is veeeery sensible :)
1 1 month, 2 weeks ago
StrayLove
my when updating doesn't show a always when adding it. it shows when updating. I just started on this course and already confused. and oh btb the ship wnt move. lol. so going to go back and relook over this confusion. ty for this guide. its helping
1 1 month, 2 weeks ago
supernicknf
When I add the "When Updating" event, I do not get an "always" block. I get a "when updating" block. I figured this wouldn't matter, so I tested the game. I got an error, and the ship will not move. Is there any other way to get that "always" block?
0 2 months, 2 weeks ago
Matsangos
I clicked add Attribute and the Internal name and Name and Description showed up but not the Default value, how do I make the Default value show up?
0 3 months, 5 days ago
Matsangos
I'm on the Attributes tab the one under events next to Palette, how do I set the Ship Speed Attribute’s Default Value to 20, there are no General or Dropdown buttons, do I click on the button on top of Name the Add Attribute and then name it Ship and set type to actor?
1 3 months, 5 days ago
bobarino53535
the ship wont move, help

1 6 months, 1 week ago
Chuckderp
First off: This tutorial is obviously outdated. I can't even find the "Self" option.
So just select the "Ship" actor instead of "Actor". That will solve the Actionscript error you may get.

Next, for the bottom left ship error: Follow step 54 entirely. Set the tab to motion so it'll say "Set <x-speed> to <0> for <Actor>" instead of "Set <x> to <0> for <Actor>". So many people forget this, so just do it right, and never give up!

2 6 months, 2 weeks ago
LegendEpic
If you have problem with the ship that stuck in bottom left corner you can fix it like that.
When you follow step 54 make sure that you choise in the right part of the screen the motion option.
The mistake is propably that you choise the code from the position category and not from the motion category.
That would propably fix the problem and make the game run fine.

2 8 months, 1 week ago
ecasillas
Using build 9180 on Mac. The design mode is a pain so far; dragging
0 8 months, 4 weeks ago
khateeb
if ur ship is stuck to left or right corner .............follow this
1.ur ship has 2 events ........1st for control..which u are working on right now.......when u have completed the crsh course part 8..................comapre every minute details of step 62 image with ur event
2.change the blue block set x..to set x peed and compile the game
ur error wiil be corrected .beleive that

0 9 months, 1 week ago

Sign In to Comment