How do I generalise click event and its action using family?

Get help using Construct 2

Post » Mon Jul 24, 2017 6:20 am

Say I have several animal objects(cat, dog, goat etc....). On click of sprite i want to play respective sound of animals. Now to achieve this i have to detect click event on each sprite and perform corresponding action. Can I some how generalise this? May be using family. So If I click on any member of family, It should play respective sound stored in instance variable of the respective sprite. for e.g. All the sprite will have instance variable name 'sound' and they will store specific sound name in it. Like Dog will store 'dog_sound' and cat will store 'cat_sound'. So probably this way I have to write only one event - action pair.
Animal -> Onclick() -> play Animal.sound

Is it possible? Or any other way I can achieve the same?
B
8
Posts: 16
Reputation: 332

Post » Mon Jul 24, 2017 9:21 am

Yeah sounds like you already know what you're doing, did you try it?
B
47
S
18
G
84
Posts: 2,265
Reputation: 49,784

Post » Mon Jul 24, 2017 10:03 am

Just add the instance variable to the family, not to the instance.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Jul 24, 2017 10:39 am

@plinkie I'm not clear how to do exacly...

@99Instances2Go If I add instance variable to family, how I will play different animal sound? bcas family instance variable can contain max one value. What I want is onclick of family I want to play respective sound of the member. If the clicked member is cat sprite it should play cat_sound, If the clicked member is dog it should play dog_sound and so on.
B
8
Posts: 16
Reputation: 332

Post » Mon Jul 24, 2017 11:10 am

Add all the animals to a family.
Organise the names of the sounds so they are easy to use.

Add a instance variable 'Scream' to family, type = text

In the layout, click each animal, change the value for 'Scream' to the name of the audio file.

In the events.
Mouse > On object clicked (family)
Audio > Is tag playing (inverted) .... tag = str(family.uid)
____________Audio > Play (by name) ... name=Family.Scream .. tag = str(family.uid)
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Jul 24, 2017 12:37 pm

thanks a lot @99Instances2Go, it worked. Where i was missing is that I was not aware of family instance variables accessibility to member sprites.
B
8
Posts: 16
Reputation: 332

Post » Mon Jul 24, 2017 12:58 pm

Oh, that had me puzzled a year ago too.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950


Return to How do I....?

Who is online

Users browsing this forum: Cubeeo and 7 guests