Now that we have our player object added to the layout, we need to get it moving when
we press the arrow keys.
In Construct 3, this can be done very easily, through use of the built in Behaviors.
We'll take a look at what behaviors are, and how to add them to an object.
Also, we'll discuss how movement works in Construct 3.
In Construct 3, Behaviors are the way that we add more functionality to an object.
Later, we'll look at the event sheet, and how it can add even further functionality
to our games, but for now, think of Behaviors as a shortcut to adding commonly used movements,
and abilities.
These behaviors can be replicated in the event sheet, but they are things that are used so
commonly in games, that they are built-in, as Behaviors.
So, let's take a look at adding a behavior to our player object.
First, make sure you have the player object selected, and go over to the properties bar.
About half way down, there is a section called Behaviors, with a blue Behaviors link.
We can add new Behaviors to the selected object here, just click Add New Behavior.
From here, we can choose from a bunch of Behaviors, but we're going to keep it simple, and choose
8 direction, from under the Movements category.
The 8 Direction Behavior allows an object to be moved in 8 directions, up, down, left,
right, or diagonally between each of those.
This is useful for a top down type of game, like we've started to build with this player
ship.
Either double click the 8 direction behavior, or click it and then the Add button, to add
the 8 Direction Behavior to our player object.
Now, 8 Direction is added to the list of Behaviors this object will be using.
We can have multiple behaviors assigned to one object, combing them to get our object
performing exactly how we want them to.
We'll add another behavior to the Player object in a minute, but first, let's take a look
at what adding the 8 Direction Behavior has done.
To do that, we need to test what we've got so far, which you can do by previewing the
layout.
To preview the layout, click on this play button, the Preview Layout button.
That will launch our game in a new window.
By default, the 8 direction behavior allows us to control the movement of an object using
the arrow keys.
So, if I press the up arrow, the ship moves up.
But, it looks like the ship is flying sideways, not forwards like it should be.
We'll fix that right now, but first let's make sure we can move in all 8 directions.
We can go up, down, left or right, and we can move diagonally between each of those.
Now, let's fix the orientation of the ship.
The reason why it's moving sideways, is because Construct 3 expects the front of the sprite
to face to the right.
Right now, it considers this side of the ship the front, and when the ship changes direction,
using the 8 Direction Behavior, Construct rotates the sprite, so that it's front faces
the direction it's moving.
All we need to do is double click on the Player object, and open up the Animations Editor.
Then, we've got options on the toolbar to rotate clockwise or anti-clockwise.
We want to rotate the ship clockwise, so the front of the ship is facing to the right.
I'll preview the layout again, and this time, the ship is facing the right direction, and
when we turn, the front of the ship is always facing forwards.
Now the ship can fly around the layout, and everything looks great.
But, the ship can fly off the screen, which I don't want it to be able to do.
I want to force the ship to stay within the boundaries of the layout.
To do this, we can add another behavior, the Bound to Layout Behavior.
The Bound to Layout Behavior will stop an object from leaving the layout, making sure
it's contained within the edges of the layout.
With the ship selected, click on Behaviors, and then Add New Behavior.
This time we need to select Bound to Layout, under the General category.
Now, our player object has two behaviors applied, 8 direction, and bound to layout.
If I preview again, now when I fly towards the edge of the screen, the player's ship
is held within the layout.
I feel like the ship is a little too slow, though.
I'd like to get it moving a little faster.
Just like objects, and the layout itself, most behaviors also have properties that can
be adjusted in the Properties Bar.
With the Player object selected, under 8 Direction, change Max Speed to 300.
I'll preview the layout again, and now the player moves around at a better speed.
Last, I'd like to talk a bit about how movement works in Construct 3.
In an earlier video, we set the size of the layout to 900 x 600.
This means the layout is 900 pixels wide, and 600 pixels tall.
We can place objects, like our Player sprite, anywhere inside or outside of this 900 by
600 pixel area.
But, for the object to be on screen, it must be within this area.
You can place an object using X and Y coordinates, with X representing horizontal placement,
and Y representing vertical placement.
The X and Y coordinates begin at the top left corner of the layout, so this position, at
the very top left corner, is represented with XY coordinates of 0, 0.
X is equal to 0, and Y is equal to 0.
As we move to the right, the X value increases, and to the left, it decreases.
If we move the sprite down, the Y values increases, and if we move it up, the Y value decreases.
Since the layout is 900 pixels wide, the maximum X coordinate for an object to be visible,
would be 900.
If the object was placed at X coordinate of 901, it would be one pixel off the screen.
But, our player ship is not a single pixel, it's a sprite made up of many pixels.
If I double click the Player object, and open the Animations Editor, and click on the Resize
button, we can see that the Player sprite is 64 pixels by 64 pixels.
The coordinates work the same way within an object, as they do in the layout.
The top left corner is 0, 0.
So, if we place an object on the layout at a specific XY coordinate, we need to specify
a point within the sprite, called the origin point, that will determine exactly where it
is placed.
Let's me show you what I mean.
I'll close down the Animations Editor for a second.
Under the Player object's properties, we can set the exact XY coordinates we want.
I'll set the ship to 0, 0, the top left corner.
You can see, that the ship was placed in the top left corner, but the center of the ship
is located at the actual 0, 0 coordinates.
That's because the default origin point is set to the center of a sprite object.
Double click the Player ship, and click on this button, edit the image points.
We'll talk more about image points later, but every sprite has at least one image point,
known as the origin point.
The origin point corresponds to the X and Y position of the sprite in the layout.
We can click anywhere on the sprite to place the origin point, or if you have a numpad,
press the number keys to automatically place the origin point.
It will place the origin point on the sprite relative to the placement of the number you
push, for example, pressing 1 on the numpad places the origin point at the bottom left
corner, pressing 9 places it at the top right corner, and pressing 5 places it right in
the center of the sprite.
I'll press 7, and put it in the top left corner, and exit out of the Animations Editor.
Now, the ship is still placed at 0, 0 coordinates, but the origin point has been moved.
You can see how the origin point directly corresponds to the XY position of the object.
Objects can be placed outside of the layout as well, they'll just be off screen.
If you place an object off the left side of the layout, it will have a negative X position,
and if you place it off the top off the layout, it will have a negative Y position.
But, if you place it off the bottom of the layout, it's Y position will just be greater
than the height of the layout, in this case 600.
Similarly, off the right side of the layout, the X position will be greater than the width
of the layout, in this case 900.
It's important to understand how this coordinate system works, because we'll be using these
values to trigger events, spawn enemies, and more.
Just make sure you understand the X and Y coordinates, as well as what an origin point
is, and you'll be good to go.
If these tutorials are useful to you, please support me on Patreon, at patreon.com/todaystuts.
For a dollar a month, you'll get early access to each new tutorial, and you'll help keep
this channel active.
Thanks for watching, see you next time.
Không có nhận xét nào:
Đăng nhận xét