How do I count each sprite width

Get help using Construct 2

Post » Sat Apr 22, 2017 9:34 am

hello,

how in text obiect display sum of all sprites width in game?
for example when i have sprites
A - width 10
B - width 20
C - width 50
etc

text object should display 80 ?
B
10
S
2
Posts: 44
Reputation: 721

Post » Sat Apr 22, 2017 10:07 am

Hi maxwell88 one way might be:

Create a global variable e.g. totalWidth

Put all your sprites in a family e.g. allSprites

Trigger once:
>>System|For each allSprites: System|Add allSprites width to globalwidth
>>Set text on text object to globalwidth
B
23
S
9
G
4
Posts: 500
Reputation: 4,417

Post » Sat Apr 22, 2017 1:15 pm

Message: maxwell88 can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
mekonbekon wrote:Hi maxwell88 one way might be:

Create a global variable e.g. totalWidth

Put all your sprites in a family e.g. allSprites

Trigger once:
>>System|For each allSprites: System|Add allSprites width to globalwidth
>>Set text on text object to globalwidth



Something dont work well, even if i add trigger once, variable counts every time to infinite

take look at capx
https://www.dropbox.com/s/ycyjvyviju9n3 ... .capx?dl=0
B
10
S
2
Posts: 44
Reputation: 721

Post » Sat Apr 22, 2017 6:03 pm

Are all the sprites one object or separate? If it is one object you can use a loop and a counter.

First set CounterVariable to 0.

Repeat Sprite.Count times - Add Sprite(loopindex).Width to CounterVariable.

Make sure the loop runs by trigger and not every tick.

If they are separate objects you should be able to do the same thing with family.
Mistakes were made.
B
57
S
28
G
115
Posts: 1,712
Reputation: 65,172

Post » Sun Apr 23, 2017 12:47 pm

Its one object like in my attached capx

I try fix this when i return home
oosyrag wrote:Are all the sprites one object or iseparate? If it is one object you can use a loop and a counter.

First set CounterVariable to 0.

Repeat Sprite.Count times - Add Sprite(loopindex).Width to CounterVariable.

Make sure the loop runs by trigger and not every tick.

If they are separate objects you should be able to do the same thing with family.
B
10
S
2
Posts: 44
Reputation: 721

Post » Sun Apr 23, 2017 6:46 pm

@oosyrag

First set CounterVariable to 0.
you mean global variable ? or instance variable ?
B
118
S
32
G
50
Posts: 1,889
Reputation: 38,034

Post » Sun Apr 23, 2017 8:19 pm

It can be either.
Mistakes were made.
B
57
S
28
G
115
Posts: 1,712
Reputation: 65,172

Post » Sun Apr 23, 2017 8:22 pm

oosyrag wrote:It can be either.



i make something like that, unfortunetly dont works
dropbox.com/s/c45rknmx4b99vi6/Zrzut%20ekranu%202017-04-23%2022.21.12.png
B
10
S
2
Posts: 44
Reputation: 721

Post » Mon Apr 24, 2017 1:15 am

Mistakes were made.
B
57
S
28
G
115
Posts: 1,712
Reputation: 65,172

Post » Mon Apr 24, 2017 8:36 am

oosyrag wrote:https://www.dropbox.com/s/keya08japv65wl4/totalwidth.capx?dl=0

Two ways, both work.



ok, now its possible to change width in real time not only at start?
because in my game, sprites should have posibliliy to change width size by user
B
10
S
2
Posts: 44
Reputation: 721

Next

Return to How do I....?

Who is online

Users browsing this forum: Google [Bot], Sal1mus and 17 guests