How do I count numbers of sprites overlaping X object

0 favourites
  • 14 posts
From the Asset Store
An educational game for counting pictures. An easy to use template for developers to build larger games
  • How i can show in the Text object how many Sprites with a Instance variable: Active: 1 are overlapping the red line?

    I actually do this:

    For each Text Object

    Sprite Value=1

    Sprite Overlaping RedLine >> Set Text to Sprite.PickedCount

    Works but is CPU intense, and having lots of lines get my CPU at 80%. The sprites are activated to White/Green on mouse click. I tried "On any mouse click" and do a function that does this but not works.

    The sprites are static and the line always pass over the same ones.

    How can i fix it?

  • The intensive CPU usage is because you're updating the text object every tick I guess. You should avoid doing this where possible. The logic itself seems fine.

  • Well, thats the problem, i tried using a function to when is call execute this one time or just update on "Any mouse click" then do that, but never updates ¿?

  • post the code with the function, that makes it much easier to see any possible problem

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Post a screenshot of the event with mouse click or function, should work fine...

  • Here the screenshot:

    Instead of a red line i use the TextObject adding a larger area that overlaps the sprites so there are less objects involved.

    Without the "On Any click" works perfects but CPU intense. I also added a it on to a funtion and call it with a click or keyboard or whatever and nothing.

  • You could try moving the overlapping condition above the variable comparison. But you'll probably get better results from changing your logic.

    When do the sprites change? If they only change one at a time you could just update that line instead of all of them.

  • 'Pick all' is what you need.

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

  • 'Pick all' is what you need.

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

    Oh! thanks! In my case use an instance variable because can be various numbers and the text object have to show the sum, hope this solution works for my needs.

  • Well that will not be enough. Easy way:

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

  • 99Instances2Go Thanks for the example! May I ask 2 questions about your capx?

    1. why do you use a blank subevent under "start of layout"?

    2. In subevent 4 I see "if sprite.row = loopindex" and "if text.row = loopindex". What does this do? It seems that the example also works without these 2 lines?

  • Blank = same as 'Every tick'. For me, that way the events read better. And they are 'collapsable' to 1 line. Less mess.

    They pre-calculate the situation for those 3 blocks that are set in the layout, the set up does not start with no blocks selected, there are 3 blocks pre-selected.

    Now, there are 2 rows. I could have done it with 1 event using sprite.row = 0. And then duplicate those evens and change sprite.row = 1 for the other row. But that are 'Duplicates' for a minor change. Duplicated events (and if one does that a lot) makes up for more events. While it should be efficient: as less events as possible.

    You can do this by calling a function to the same code, using row as a parameter. That is the main reason for using a function: eliminate duplicated events.

    I opted for a repeat loop. Loopindex is the current iteration of the loop. The repeat performs those evens twice. That first time loopindex = 0. The second time loopindex = 1. So the first time it picks the first row, the second time the second row. It looks for selected blocks only in that row, and calculates the sums.

  • If you are working with multiple Sprites, it's harder. Easier way is set animations and put there single sprites or anims. Then you can create ONE simple Boolean for the sprite. If active - set 1. Simple. Then you can put a command in your sheet = Pick all -> IfActive = 1 -> Write it to the text. Dunno what else you can do except what guys up there told you. I am still trying to improve my skill to get better performance with less code.

  • 99Instances2Go Oh great! Thanks for the help and explanation!

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