How do I use "While"?

0 favourites
  • 12 posts
From the Asset Store
Air War 1941
$5.60 USD
60% off
Full game Construct 2 and Construct 3 to post on Google Play
  • Hi. I can't understand, why does it work in the first case, but not in the second

  • If you want to add 1 to the variable every 0.5s until it reaches 10, you don't need "While" condition at all. Change your code like this:

    Every 0.5s
    Variable<10 : Variable add 1
    

    While is used for loops which run in one tick. For example "While Car not overlapping Wall, move Car 1px"

  • Yes! I know. This is just a simple example. I want to understand, why a subevent doesn't work with "while" loop?

  • Timed subevents, e.g. every X seconds, don't work well in loops.

    As an example, you could do:

    While

    Sprite.X < 200

    Set Sprite.X to Sprite.X +1

  • I want to understand, why a subevent doesn't work with "while" loop?

    Your event #1 on the second screenshot runs on every tick. There is only one condition (variable<10) which is always true. As a result this loop becomes infinite and the game freezes or crashes.

    In the first screenshot there are two conditions in the event. The first condition is true, but "every 0.5s" condition is false most of the time, that's why the loop doesn't freeze and the code works.

    But I would say both examples are wrong.

  • thank you very much. I have never used "while loop" before, I will study further.

  • You have to remember that Construct is a Giant loop already.

    Everything is always happening While each condition is true. "While" loops just open the potential to delve deeper into the same event until a condition is met.

  • could you guys look at this example?

    When I start the layout, I can see the process of creating of the sprites. But I'd like to create them instantly.

    Maybe you can advise how else I can make such an example?

    green sprites will be trajectory prediction points. And when they hit the obstacle, then stop creating, and the trajectory have to become shorter when the obstacle moves.

    The construction of the trajectory is very complicated, so I made a simplified version.

    drive.google.com/file/d/1234QoHW_sLxo-d2NV6qVSBtiX1ltYdtg/view

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Replace event 2 with:

    + System: For "" from 1 to int(distance(Sprite.X, Sprite.Y, Sprite2.X, Sprite.Y)÷Sprite.Width)

    + System: Pick last created Sprite

    + Sprite: [X] Is contact

    -> System: Create object Sprite on layer 0 at (Sprite(-1).X+Sprite.Width÷2×LoopIndex, Sprite.Y), create hierarchy: False, template: ""

  • Replace event 2 with:

    + System: For "" from 1 to int(distance(Sprite.X, Sprite.Y, Sprite2.X, Sprite.Y)÷Sprite.Width)

    + System: Pick last created Sprite

    + Sprite: [X] Is contact

    -> System: Create object Sprite on layer 0 at (Sprite(-1).X+Sprite.Width÷2×LoopIndex, Sprite.Y), create hierarchy: False, template: ""

    the thing is that my trajectory will not be straight. That's why I can't use distance.

  • Search for "trajectory" or "arc" on this forum, this is a very popular question and has been discussed many times.

  • thank you all for your help. I think I should prepare a more detailed question.

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