How do I Create 1 Object for Every X Objects

0 favourites
  • 6 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • I may have over looked a post on this subject.

    I would like to create 1 Blue box for every 20 Red Boxes created.

    I tried the 'for every' expression but i may be using it wrong.

    If anyone could share some help it would be greatly appreciated.

  • You could do the following:

    Repeat RedBox.count times
          (if) loopindex % 20 = 0
                 (then) Create object BlueBox[/code:2omopuix]
    
    This procedure would be repeated as many times as there are Red Boxes.
    When the division [i]loopindex / 20[/i] returns a number without any digits behind the point (the loopindex is dividable by 20), a Blue Box is being created.
    
    This is the way without a variable.
    
    There also is a way with a variable, but I guess this one is sufficient for the moment.
  • randomly

    Thanks for the reply. I did end up using a variable.

    Redcount=0

    On Red destroyed > add 1 to Redcount

    Redcount=20 --

    random (2,5) sec > create blue box

    Blue box on destroyed > reset Redcount

    Works good. Now i just need to figure out how to create a random number of those blue boxes during the time the 1st blue box is created and then destroyed (which is around 3 secs before the Global Variable Redcount resets).

    Thanks for the reply, again.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well what you did is actually the simplest option you can go with.

    But it doesn't work if there are more than 20, say for example 40 Red Boxes....but if that's not needed in you project, no problem with your solution.

    What I don't understand though, is:

    1) why does your Redcount variable go up when one instance is destroyed? Didn't you want to create one blue box for every 20 red created?

    2) Why don't you just use RedBox.count? That would make the variable redundant.

    And if you might explain your second issue (with the random stuff) again, more detailed (or create a new thread for it), I'm sure I can help you with that too.

  • randomly

    Ok, try and follow, i hope to explain this right.

    Globals: RedboxCount=0, BlueboxCount=0, GreenboxCount=0

    On the start of the layout there are 30 or so Redboxes, 1/3 of that is blue, 1/3 of that are green (math may be off- just as an example)

    Redbox destroyed > add 1 to RedboxCount

    RedboxCount = random (3,6)

    (SubEvent) every random (2,6) secs

    (Sub) trigger once > create Bluebox

    BlueBox on destroyed > subtract 2 from RedboxCount

    Add 1 to BlueBoxCount

    BlueBoxCount = random (4,8)

    (Sub) every random (4,7) secs

    (Sub) trigger once > create Greenbox

    Greenbox on destroyed > subtract 2.1 from BlueBoxCount

    This may not be the easiest (not too good with variables, functions... All the hard stuff, but it works.

    I watched for maybe 5-6 mins, Bluebox and Greenbox had different number of instances created, respectively.

    My only big problem is trying to create a random number of each box randomly across the layout on Start of Layout. So it can be different each time.

  • I don't follow the last post, it looks different than what was originally asked. Here's my solution for the op.

    Global redboxcounter=0

    On redbox created

    --- add 1 to redboxcounter

    Redboxcounter >= 20

    --- subtract 20 from redboxcounter

    --- create bluebox

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