Go to our browserquest-images -> img -> 2 folder, and we'll pick an enemy from there. For now, let's use a rat as an enemy because it looks like it's easy to kill and fits for our first encounter. So, open our project again if you haven't already and insert a new Sprite object called "rat". We'll use the rat.png file from the folder we just opened, import the frames from sprite strip like we usually do. For this spritesheet there are 6 horizontal cells, and 10 vertical cells.
After you split it correctly you'll get a few animations divided like this:
- Idle / default animation: one frame, the first frame on first row
- Destroyed animation: three frames, starting from second frame on first row
- Attack right / left animation: five frames, second row
- Walk right / left animation: four frames, one from second row and three from the third row
- Sniffing right / left animation: two frames, fourth row
- Attacking up animation: four frames, fifth row
- Walking up animation: four frames, sixth row
- Sniffing up animation: four animations, seventh row
- Attacking down animation: four frames, eight row
- Walking down animation: four frames, ninth row
- Sniffing down animation: four frames, tenth row
Enemy's animations |
- Default animation: speed 5, no loop
- Destroyed animation: speed 9, no loop
- AttackRight animation: speed 15, no loop
- WalkRight animation: speed 15, loop
- SniffingRight animation: speed 5, no loop, repeat count 3
- AttackUp animation: speed 12, no loop
- WalkUp animation: speed 12, loop
- SniffUp animation: speed 4, no loop, repeat count 3
- AttackDown animation: speed 12, no loop
- WalkDown animation: speed 12, loop
- SniffDown animation: speed 4, no loop, repeat count 3
Add the 8 Direction behavior to the rat,and change both the "Set angle" and "Default controls" to no. Then we'll switch to the event sheet, before we move the rat it must first make a random decision about where to go: is it to go left, up, right, or down? Construct 2 has an expression to randomly select a set of options, called "choose". For now add a new event to compare the rat's playing animation, if it's the Default animation then we'll start randomly choosing a direction.
Before I continue, I need to explain a bit about variables. Variables are containers for values that can be accessed later, this is useful if we want to "remember" something to be used later on. Construct 2 knows two kinds of variables: global variables, and instance variables. Global variables are variables that are known throughout the project and the values are the same for a variable. Instance variables are variables that are "stuck" to objects, and the values can be different for each instance of objects.
For now let me explain about global variables, to make a global variable right click on the event sheet and select "add global variable" (or you can press "v" on the keyboard for shortcut). After that you'll see a box with several textboxes, these are what make a variable.
The Name textbox is of course the name of your variable, the name must be unique which means no two variables can have the same name. Type determines whether the variable is a text or just a number, this is important because text can't perform mathematical methods (you can't add or subtract or multiply a text) and a number can't store letters. Initial value is the first value this variable will have when the game starts, it can be important if your game expects something other than zero. And the description is optional, just in case you can't tell what a variable does by its name, you can explain it here.
New global variable |
To make an instance variable, click on an object and look at the properties bar, there would be an add / edit instance variables link. For example, click on the rat object and look at the properties bar.
Adding instance variables |
For now add an instance variable to the rat and name it "direction" with a "text" type, we'll use it to remember which direction the rat needs to go to. Pressing the "OK" button will create the instance variable, and you can see it in the instance variables box. Close the instance variables box and look at the event sheet.
On the event that checks rat's "default" playing animation add a new action, double click on the rat object and then scroll down until you see the "instance variable" section,and then double click on "set value". This action changes the value of an instance variable, because we only have one instance variable, we can only modify the "direction" variable from the dropdown list. On the value text box, type "choose" (without the quotes) as you type you'll see the expressions of Construct 2, they have many uses so I recommend you to read the documentation to know more.
After that type "(" (without the quotes) to start filling the expression, type "up", "down", "left", "right" (now with quotes) divided by commas. The commas serve to divide each selection up, and after we're done, type ")" (without the quotes) to close the expression. You'll get an event that looks like this
Choosing a direction randomly |
To simulate the 8 Direction control, double click on the rat and then select "Simulate control" from the 8 Direction section, and then select which direction you want to simulate from the dropdown list. Your event sheet will look like this.
Simulate 8 Direction |
Why don't we do the second option. Let's add a new behavior to the rat: Timer behavior. The timer behavior will "set off" or "trigger" on the time that we set, we'll use this to make the rat change animation and direction. First, add the timer behavior to the rat, you can find it under the general section when you add a behavior. And then we'll switch to the event sheet again.
We're going to use this timer right after we randomly change the rat's direction, so add a new action under the "set direction" action. Double click on the rat, and select "Start timer". You'll see a box with three parameters. The duration is the time in seconds we want the timer to trigger, the type determines whether this is a one time timer or repeated, and the tag is a text we use to identify the timer. For now change the duration to 0.5, the type set to once, and the tag to "change direction".
Start timer |
Finishing events |
Adding a blank sub-event |
As always, if you want to see the source code for this chapter you can download it from my Drive. If you like what you read please consider supporting my Patreon, thanks. 'Till next time!
Tidak ada komentar:
Posting Komentar