252,449

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

mbtheorissa
my problem is the one that move is an enemy when the other enemies and ship aren't move
0 1 month, 4 weeks ago
nonedeval
guys if your ship cant move check if you checked out updating under add event section (and sorry if my English is some where bad just wanted to help )
0 2 months, 2 weeks ago
Isaiahhehehe
This is fun/not but i still love to make games off of this and coding too! ;)
0 2 months, 3 weeks ago
BillGmaer254
my problem is it always moves to the right if i press left it goes left but when i release it doesnt work helps pls respond quick too
0 3 months, 1 week ago
BillGmaer254
Matsangos sorry if this is WAY too late but you set it to actor not number
look at step 55 more closely

0 3 months, 1 week ago
BillGmaer254
I fixed the problem but help because it cant stop moving

0 3 months, 1 week ago
BillGmaer254
Hey can someone help on this? I put in ships atributes but i cant see the ship speed at all i tried making my own but ntohing so far oh im on the newest version of stencyl someone pls respond quickly
0 3 months, 1 week ago
Mr BLOB
i noticed that there should be no incomplete code or when you test it shows error

0 6 months, 2 weeks ago
DasherDarren
When I try to test it, it has to save first, but it can't save and gives an "unexpected problem" error. What should I do?
0 9 months, 3 weeks ago
bmofmc
I found a way to fix the problem if you are a newbie like me and don't fully understand how to debug the code. After checking over my code and scrolling through the comments, I fixed the problem of the ship not being able to move. Although that problem was gone, I found a new one. I pressed the movement keys down and the ship moved, which was great, but then when I let go of the key the ship would snap back into the middle. I fixed this by making a "Created" event and making an infinite loop by using a "repeat" block and putting "Pi" into the number of times repeating area. Since pi is infinite, it basically made an infinite loop (I did this because I could not find the infinite loop block). Inside I put a "Set x" block (not Set x-speed) and I set the value of that to "x of Self" so it would infinitely set the x position of the ship to its current one, ultimately canceling out the snap-back-to-middle bug. Its a lengthy fix, but it works :)
0 1 year, 3 months ago

Sign In to Comment