Guia per iniciante nello Construct 2(Traducció in Mantena)

1

Index

Taggé

Statistiques

18,100 visites, 26,178 vues

Outils

Partager

Traductions

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 13 Feb, 2015. Last updated 25 Feb, 2019

Adding behaviors

Behaviors are pre-packaged functionality in Construct 2. For example, you can add a Platform behavior to an object, and the Solid behavior to the floor, and you instantly can jump around like a platformer. You can do the same in events, but it takes longer, and there's no point anyway if the behavior is already good enough! So let's have a look at which behaviors we can use. Amongst others, Construct 2 has these behaviors;

- 8 Direction movement. This lets you move an object around with the arrow keys. It will do nicely for the player's movement.

- Bullet movement. This simply moves an object forwards at its current angle. It'll work great for the player's bullets. Despite the name, it'll also work nicely to move the monsters around - since all the movement does is move objects forwards at some speed.

- Scroll to. This makes the screen follow an object as it moves around (also known as scrolling). This will be useful on the player.

- Bound to layout. This will stop an object leaving the layout area. This will also be useful on the player, so they can't wander off outside the game area!

- Destroy outside layout. Instead of stopping an object leaving the layout area, this destroys it if it does. It's useful for our bullets. Without it, bullets would fly off the screen forever, always taking a little bit of memory and processing power. Instead, we should destroy the bullets once they've left the layout.

- Fade. This gradually makes an object fade out, which we will use on the explosions.

Let's add these behaviors to the objects that need them.

How to add a behavior

Let's add the 8 direction movement behavior to the player. Click the player to select it. In the properties bar, notice the Behaviors category. Click Add / Edit there. The Behaviors dialog for the player will open.

Click the green 'add behavior' icon in the behaviors dialog. Double-click the 8 direction movement to add it.

Do the same again and this time add the Scroll To behavior, to make the screen follow the player, and also the Bound to layout behavior, to keep them inside the layout. The behaviors dialog should now look like this:

Close the behaviors dialog. Hit Run to try the game!

Hopefully you have a HTML5 compatible browser installed. Otherwise, be sure to get the latest version of Firefox or Chrome, or Internet Explorer 9 if you're on Vista and up. Once you have the game running, notice you can already move around with the arrow keys, and the screen follows the player! You also can't walk outside the layout area, thanks to the Bound to Layout behavior. This is what behaviors are good for - quickly adding common bits of functionality. We'll be using the event system soon to add customised functionality.

Adding the other behaviors

We can add behaviors to the other objects by the same method - select it, click Add / Edit to open the behaviors dialog, and add some behaviors. Let's add those other behaviors:

- Add the Bullet movement and Destroy outside layout to the Bullet object (no surprises there)

- Add the Bullet movement to the Monster object (because it just moves forwards as well)

- Add the Fade behavior to the Explosion object (so it gradually disappears after appearing). By default the Fade behavior also destroys the object after it has faded out, which also saves us having to worry about invisible Explosion objects clogging up the game.

If you run the game, you might notice the only thing different is any monsters you can see suddenly shoot off rather quickly. Let's slow them down to a leisurely pace. Select the Monster object. Notice how since we added a behavior, some extra properties have appeared in the properties bar:

This allows us to tweak how behaviors work. Change the speed from 400 to 80 (this is in pixels travelled per second).

Similarly, change the Bullet object's speed to 600, and the Explosion object's Fade behavior's Fade out time to 0.5 (that's half a second).

Create some more monsters

Holding control, click and drag the Monster object. You'll notice it spawns another instance. This is simply another object of the Monster object type.

Object types are essentially 'classes' of objects. In the event system, you mainly deal with object types. For example, you might make an event that says "Bullet collides with Monster". This actually means "Any instance of the Bullet object type collides with any instance of the Monster object type" - as opposed to having to make a separate event for each and every monster. With Sprites, all instances of an object type also share the same texture. This is great for efficiency - when players play your game online, rather than having to download 8 monster textures for 8 monsters, they only need to download one monster texture and Construct 2 repeats it 8 times. We'll cover more on object types vs. instances later. For now, a good example to think about is different types of enemy are different object types, then the actual enemies themselves (which there might be several of) are instances of those object types.

Using control + drag, create 7 or 8 new monsters. Don't place any too close to the player, or they might die straight away! You can zoom out with control + mouse wheel down if it helps, and spread them over the whole layout. You should end up with something a bit like this.

Now it's time to add our custom functionality via Construct 2's visual method of programming - the event system.

  • 0 Comments

Want to leave a comment? Login or Register an account!