How do I count each sprite width

0 favourites
From the Asset Store
Be quick and count the cards and choose the right number!
  • 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 ?

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

    dropbox.com/s/ycyjvyviju9n3 ... .capx?dl=0

  • 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.

  • Its one object like in my attached capx

    I try fix this when i return home

    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.

  • oosyrag

    First set CounterVariable to 0.

    you mean global variable ? or instance variable ?

  • It can be either.

  • 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

  • dropbox.com/s/keya08japv65wl4/totalwidth.capx

    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

  • Here's an example using a function. In the example I call the function at the start and on any mouse click (the mouse click also randomises the widths of the sprites to simulate the sprite widths changing during runtime) but you can call it whenever you need to in your project:

    https://www.dropbox.com/s/zel61966va8ym ... .capx?dl=0

  • Here's an example using a function. In the example I call the function at the start and on any mouse click (the mouse click also randomises the widths of the sprites to simulate the sprite widths changing during runtime) but you can call it whenever you need to in your project:

    https://www.dropbox.com/s/zel61966va8ym ... .capx?dl=0

    good work <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile" />

  • Here's an example using a function. In the example I call the function at the start and on any mouse click (the mouse click also randomises the widths of the sprites to simulate the sprite widths changing during runtime) but you can call it whenever you need to in your project:

    https://www.dropbox.com/s/zel61966va8ym ... .capx?dl=0

    Hi, i download your example and im wondering how add posibility to count spawned and stretched new sprites.

    https://www.dropbox.com/s/v1au9pauscki7 ... .capx?dl=0

    can you fix the problem?

    i dont know why stretched lines after drop getting smaller

  • IGDev Hi there, the reason your lines widths are changing is because you haven't disabled/deleted event 8, which randomises the the width of any sprite in the "sprites" family; I put that event in to demonstrate how the totalwidth value updates when the widths change - you don't need it.

  • IGDev Also in your example it looks like you have a bug where clicking on the same sprite1 will generate additional lines - or is this intended? If you explain the required behaviour I can have a go at patching it up.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)