Reuse code

Get help using Construct 2

Post » Fri Mar 30, 2012 6:21 am

Hey again

I have been trying to find an answer to this question, but with 20 scirra tabs open, and trying various search phrases I still am not winning.

As a normal developer I like to re-use code, create methods by name and then use the method when I want stuff done.

I haven't found a way to do this yet, and was wondering if there was. The example is that I have 4 unique sprites. But I want them to act the same, respond to the same events and behave the same way. It would be nice to then create events and pass parameters.

I saw there was a plugin of this sort of nature mentioned, but for the life of me I cant find where I saw it.

Any suggestions or feedback would be great!

cheers
B
22
S
7
G
3
Posts: 189
Reputation: 4,270

Post » Fri Mar 30, 2012 6:26 am

B
97
S
22
G
178
Posts: 4,122
Reputation: 104,051

Post » Fri Mar 30, 2012 6:37 am

Create a family for them (-not in the free I think-) and do the eventing for that.
One possibility to reuse code is including eventsheets.Joe72012-03-30 06:41:47
B
14
S
6
G
5
Posts: 173
Reputation: 4,742

Post » Fri Mar 30, 2012 6:40 am

Family can not be used in free version.
B
97
S
22
G
178
Posts: 4,122
Reputation: 104,051

Post » Fri Mar 30, 2012 7:05 am

Turn the 4 unique sprites into a single one, setting the animation speed to 0 and differentiating each instance with its initial animation frame.

That would allow to write a bit of code that can affect all 4 instances at the same time (using a loop) or separatly if you need (picking a specific instance).
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

Post » Fri Mar 30, 2012 10:33 am

[QUOTE=Kyatric] Turn the 4 unique sprites into a single one, setting the animation speed to 0 and differentiating each instance with its initial animation frame.

That would allow to write a bit of code that can affect all 4 instances at the same time (using a loop) or separatly if you need (picking a specific instance).
[/QUOTE]

I already use this solution and it work perfectly.
Just control every common behavior of "enemy" in my game using the same sprite and their different behaviours picking up the animation which they are running (using sub-event is ... animation playing). With this you can control a single instance event if it "trasform" changing it's animation.
Or you could even control each instance with the id.Dado2012-03-30 10:42:55
B
15
S
5
G
1
Posts: 40
Reputation: 2,026

Post » Fri Mar 30, 2012 6:22 pm

Generally, the best ways to reuse code are:
1. groups which are set active only occasionally
2. event sheet includes.
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Fri Mar 30, 2012 7:06 pm

function, use function :P thats how real coders do it
B
9
S
4
G
3
Posts: 223
Reputation: 3,433

Post » Mon Apr 02, 2012 11:41 am

Downloading and trying function.

Thanks for all the replies. I have a paid version :)

I'll let you know if I come right!
B
22
S
7
G
3
Posts: 189
Reputation: 4,270

Post » Mon Apr 02, 2012 5:18 pm

It is working.

I would love to pass a sprite as a parameter to a function as an object, but I will figure something out.superkew2012-04-02 17:20:10
B
22
S
7
G
3
Posts: 189
Reputation: 4,270

Next

Return to How do I....?

Who is online

Users browsing this forum: Vegamon007 and 16 guests