How do I create an event for when 2 keys are down?

Get help using Construct 2

Post » Thu May 15, 2014 1:34 pm

No problem, I am glad to help.

So, in my example, events 6,7,8 and 9 are checking the key-presses and store a "direction" into an Instance Variable. This variable exists on the Player Sprite object. Click on the Player and you will see on his properties that under the "Instance Variables" category, a Variable named "Direction" exists and it's initial value is "Right". You can add/remove/edit Instance Variables by clicking on the "Instance variables" link.

So, after the direction is stored in the variable, events 10 and 15 check if the player is moving or not and then, within the sub events, decide which animation should be played.

You don't have to use an Instance variable, you could use a global variable and the result would be just the same, but in practice you want less global variables, for organization purposes.

I hope that makes sense :)
composer - multimedia artist
www.eli0s.com/en/
B
69
S
26
G
5
Posts: 1,146
Reputation: 9,829

Post » Thu May 15, 2014 10:03 pm

Alright, I'll give it a go and I'll get back to you!
B
4
Posts: 25
Reputation: 256

Post » Thu May 15, 2014 11:46 pm

I like the idea of using an instance variable, since they usually keep everything nice and tidy, but every time I try to make an action to change its value, it says that "down" is an unknown expression and "right" "does not take 0 parameters". I think I might be wrong, but I'm supposed to use "Instance Variables -> Set Value", right? Is that the action I'm supposed to use? I still don't know how to set the direction to anything, as I just don't know which action I should use.
B
4
Posts: 25
Reputation: 256

Post » Thu May 15, 2014 11:56 pm

TheBuob wrote:I like the idea of using an instance variable, since they usually keep everything nice and tidy, but every time I try to make an action to change its value, it says that "down" is an unknown expression and "right" "does not take 0 parameters". I think I might be wrong, but I'm supposed to use "Instance Variables -> Set Value", right? Is that the action I'm supposed to use? I still don't know how to set the direction to anything, as I just don't know which action I should use.


To use an instance variable inside an expression, you have to write it in the format : Object.Instance_Variable (e.g. Sprite.Down, Enemy.HP, etc..)

To set it, you just have to select it from the dropdown list, and to enter the value you want
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Fri May 16, 2014 12:19 am

Aphrodite wrote:
TheBuob wrote:I like the idea of using an instance variable, since they usually keep everything nice and tidy, but every time I try to make an action to change its value, it says that "down" is an unknown expression and "right" "does not take 0 parameters". I think I might be wrong, but I'm supposed to use "Instance Variables -> Set Value", right? Is that the action I'm supposed to use? I still don't know how to set the direction to anything, as I just don't know which action I should use.


To use an instance variable inside an expression, you have to write it in the format : Object.Instance_Variable (e.g. Sprite.Down, Enemy.HP, etc..)

To set it, you just have to select it from the dropdown list, and to enter the value you want


Where should I write this instance variable?
B
4
Posts: 25
Reputation: 256

Post » Fri May 16, 2014 12:53 am

First make sure that the Instance variable you have created is of the Text type.
To set the Instance variable of the Player object, create an action, double click on the Player Sprite Object, double click on the Set value (under the Instance variables category), pick the Direction variable (that you had created before) from the list (it should be the only one existing) and on the Value field type the direction you want to set e.g. "Right" with the quotes.

Do that for every direction, just like in my example.

The same applies when you check the content of a variable (compare variable) in a condition. You type the corresponding direction with quotes (e.g. "Down") and Construct only triggers the down animation when the condition is true.
composer - multimedia artist
www.eli0s.com/en/
B
69
S
26
G
5
Posts: 1,146
Reputation: 9,829

Post » Fri May 16, 2014 1:06 am

eli0s wrote:First make sure that the Instance variable you have created is of the Text type.
To set the Instance variable of the Player object, create an action, double click on the Player Sprite Object, double click on the Set value (under the Instance variables category), pick the Direction variable (that you had created before) from the list (it should be the only one existing) and on the Value field type the direction you want to set e.g. "Right" with the quotes.

Do that for every direction, just like in my example.

The same applies when you check the content of a variable (compare variable) in a condition. You type the corresponding direction with quotes (e.g. "Down") and Construct only triggers the down animation when the condition is true.


Great! I managed to set the directions accordingly (I had forgotten about the quotes, silly me!). Only one more thing, and I'll be on my way. What did you do with events 15 to 19? I see you managed to link 4 different events to event #15, and I don't know how to do that. I reckon that's a very useful thing to do and I would like to know what you pressed in order to do that.
B
4
Posts: 25
Reputation: 256

Post » Fri May 16, 2014 1:49 am

These events are called sub-events. There are many ways to create them. I use the "B" shortcut that creates an empty sub-event. The "S" opens the Add event dialog, so you can immediately choose a condition. Finally, if you right click on the furthest left side of a condition (just right before the icon of the object that the condition checks), a menu appear that has many important choices, the first of all being the "add" in which you will find the sub-event options.

And yes, this is a very useful thing, not only it saves you space (because you can nest many sub-conditions that have a common condition on top and those conditions can nest sub-events by them self etc), but since they act hierarchically, you can create complex events that pass along actions from the parent conditions to the child conditions whenever the parent is true. This has the added bonus of being very efficient, since when a top condition isn't true, all sub-events are ignored and C2's code is being read faster per tick.
composer - multimedia artist
www.eli0s.com/en/
B
69
S
26
G
5
Posts: 1,146
Reputation: 9,829

Post » Fri May 16, 2014 2:09 am

eli0s wrote:These events are called sub-events. There are many ways to create them. I use the "B" shortcut that creates an empty sub-event. The "S" opens the Add event dialog, so you can immediately choose a condition. Finally, if you right click on the furthest left side of a condition (just right before the icon of the object that the condition checks), a menu appear that has many important choices, the first of all being the "add" in which you will find the sub-event options.

And yes, this is a very useful thing, not only it saves you space (because you can nest many sub-conditions that have a common condition on top and those conditions can nest sub-events by them self etc), but since they act hierarchically, you can create complex events that pass along actions from the parent conditions to the child conditions whenever the parent is true. This has the added bonus of being very efficient, since when a top condition isn't true, all sub-events are ignored and C2's code is being read faster per tick.


Aaaaand everything works as intended! Thank you so much!
B
4
Posts: 25
Reputation: 256

Post » Fri May 16, 2014 11:26 am

You are welcome!
composer - multimedia artist
www.eli0s.com/en/
B
69
S
26
G
5
Posts: 1,146
Reputation: 9,829

Previous

Return to How do I....?

Who is online

Users browsing this forum: rayolf and 23 guests