[solved]How do I work with while loops?

0 favourites
  • 11 posts
From the Asset Store
40 ambient sound loops. A wide selection of looping ambient sounds for different locations.
  • Whatever I do I get some "Unresponsive script" error. Is there some kind of example for using while loops?

  • Could you share the events or describe them?

  • A while loop needs an exit condition or a "break" statement (stop loop). Otherwise it'll loop infinitely and cause your script to hang.

  • I want to move the green dot ("player" sprite) to move until the left end of the background sprite (or layout).

  • I was also thinking it's an infinite loop but I don't know where to put the stop loop

  • Try to put the condition "player is overlapping background" directly into the "while" event instead of a sub-event. Right now the while loop doesn't have any conditions associated with it.

  • It works now but it only "teleports" from the center of layout to the left. I want it to move instead...like some kind of animation!

  • Use a boolean instance variable called "Sliding" or something like that. When you click on it, set Sliding to true. Then do another event, if Sliding is true, move it a little bit like you have now. Then every time through your game loop it will move just a bit, making it like an animation.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I have finally made it using the Bullet and BoundToLayout behavior:

    Thanks guys and girls!!! You are THE BEST!!!

  • It works now but it only "teleports" from the center of layout to the left. I want it to move instead...like some kind of animation!

    I think it's because all loop running at the same time

  • Hi. I believe you can reach that effect by a very short and simple method, without using the While Loop or Bullet behaviour.

    First, create the object you want to move (I created a sprite and called it obj_red_dot) and give it an inital false Boolean instance variable, you may call it "goal", to indicate whether it has reached its "goal", which is to get to the desired position or not.

    After that, create a sprite called "obj_line", just to indicate where you want the first object to get to, which will be like you "goal line". And finally create a "trigger", which I chose to be a sprite that I will click using the left mouse button, I called this "obj_btn".

    Fine. Make sure you have the Mouse plugin added and now all you need to do is writing this simple code:

    Mouse > On Left button Clicked on obj_btn > obj_red_dot > set goal to True.

    obj_red_dot > is goal > obj_red_dot > Set X to lerp(Self.X,obj_line.X,2.5*dt)

    The velocity in which the "obj_red_dot" will lerp is set by the factor "2.5*dt", which you may increase or decrease by your wish.

    One last important detail is about the origin points set to the objects "obj_red_dot" and "obj_line". Depending on the angle of motion or your desired effect you must change it to match correctly. In my example, I set the origin points for both as "right", so when the "obj_red_dot" finally finishs its move, it fits right into the "obj_line" width.

    Hope I have helped somehow.

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