How do I Add together the collected stars in all my levels?

Get help using Construct 2

Post » Sun Jul 31, 2016 10:48 pm

The stages in my game have 3 stars hidden throughout the level. I want to add together all of the collected stars in a total and display them on screen. I already have set up where it will show, I just need help adding them together.

Much Thanks!
B
24
S
9
Posts: 112
Reputation: 2,010

Post » Sun Jul 31, 2016 10:56 pm

Objects have the expression "Count" which counts up the number of existing objects of this type, so you can use that. Something like,

Start of layout --> Set score to Stars.Count
B
52
S
24
G
18
Posts: 295
Reputation: 13,090

Post » Sun Jul 31, 2016 10:57 pm

Use a global variable to keep track of total stars collected. For example lets call it TotalStars.

On star collected - (System Actions) Add 1 to TotalStars.

Then to display, you can use a text or spritefont object to set text to TotalStars.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,698
Reputation: 64,444

Post » Mon Aug 01, 2016 6:49 am

oosyrag wrote:Use a global variable to keep track of total stars collected. For example lets call it TotalStars.

On star collected - (System Actions) Add 1 to TotalStars.

Then to display, you can use a text or spritefont object to set text to TotalStars.


Let me rephrase that. What I want is to add together all the collected stars from all the levels. Thanks again.
B
24
S
9
Posts: 112
Reputation: 2,010

Post » Mon Aug 01, 2016 6:53 am

Same answer? Or maybe I still misunderstand...

Whenever you collect a star on any level, add it to a global variable counter. You now have the total number of collected stars from all levels.

Global variables are global in that they keep their value across layouts.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,698
Reputation: 64,444

Post » Mon Aug 01, 2016 7:02 am

oosyrag wrote:Same answer? Or maybe I still misunderstand...

Whenever you collect a star on any level, add it to a global variable counter. You now have the total number of collected stars from all levels.

Global variables are global in that they keep their value across layouts.


I get what you're saying, but will this still work if I don't want the total number of collected stars to exceed the total amount of stars in the game?
B
24
S
9
Posts: 112
Reputation: 2,010

Post » Mon Aug 01, 2016 7:08 am

As long as there is no way to collect the star again after it has been collected (the star no longer appears on subsequent plays) it should work fine.

Otherwise, you'll need a flag system to keep track of each star and it's state, if it has been collected before or not. An array may be a more suitable method of data storage in that case.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,698
Reputation: 64,444

Post » Mon Aug 01, 2016 7:17 am

oosyrag wrote:As long as there is no way to collect the star again after it has been collected (the star no longer appears on subsequent plays) it should work fine.

Otherwise, you'll need a flag system to keep track of each star and it's state, if it has been collected before or not. An array may be a more suitable method of data storage in that case.


That's what I thought. Arrays just confuse me a little so I thought I'd ask first. Do you know how I should go about doing that? An example isn't necessary but would be greatly appreciated.
B
24
S
9
Posts: 112
Reputation: 2,010

Post » Mon Aug 01, 2016 7:27 am

I can make an example later, don't have C2 in front of me.

But basically just think of an array as a spreadsheet, pretty simple. You can have one column for each level, and 3 rows which represent each star. In each cell, a 0 would represent not collected, and a 1 would be collected.

When you collect a star, you would set that star's position in the array to 1. This way, if it was 0 before, it would change to 1, and if it was already 1, nothing would change.

Then run a loop in the array, for each xy, that adds everything in the array. That would give you your collected star count.

Example to come tomorrow!
Mistakes were made.
B
53
S
27
G
114
Posts: 1,698
Reputation: 64,444

Post » Mon Aug 01, 2016 7:40 am

oosyrag wrote:I can make an example later, don't have C2 in front of me.

But basically just think of an array as a spreadsheet, pretty simple. You can have one column for each level, and 3 rows which represent each star. In each cell, a 0 would represent not collected, and a 1 would be collected.

When you collect a star, you would set that star's position in the array to 1. This way, if it was 0 before, it would change to 1, and if it was already 1, nothing would change.

Then run a loop in the array, for each xy, that adds everything in the array. That would give you your collected star count.

Example to come tomorrow!


Thank you very much!!! :D
B
24
S
9
Posts: 112
Reputation: 2,010

Next

Return to How do I....?

Who is online

Users browsing this forum: chrisinfinger, YoHoho and 14 guests