"Repeat for each instance" only happens once

Here is my situation: I have code which creates an object (moveDest) a set distance away from another object (TestMove), in which TestMove then moves towards moveDest until they both come in contact with each other. Then, the exact same process happens again, but in the opposite direction, and what I WANT is the blocks to be going on a loop back and forth.

I specified in my code that this set-up should repeat for each instance of TestMove, but as a result, it’s causing the code to only run once for some reason. By this, I mean that once it finishes traveling in the opposite direction, TestMove just stops in place, and the cycle doesn’t continue anymore. I’m clueless as to why this is the case, and I’ve been stuck all week on it. I’d be grateful if anyone could help me out on this.



– “posNum” dictates whether it’s traveling in the main direction or in the opposite direction. After it finishes going in the main direction, the value of the variable increases and it starts to travel in the opposite direction. After it finishes going in the opposite direction, the value of the variable goes back to what it originally was, and the cycle is suppose to continue.
– “posDistanceX” dictates how far away moveDest is spawned from TestMove (aka, how far the block is going to travel left and right).

I hope this all makes sense. Thank you if you’re able to lend me a hand… :slight_smile:

1 Like

Try removing the Trigger Once in the Foreach

I’ve notice that in a foreach you’re checking if TestMove posNum = 1, but initially the value assigned in screenshot 2,3 is 0 for posNum, maybe you have to change one of them to 0 and the other to 1.

Plus all that events can be achieved just adding Tween to the object you want to meve. then just call Tween to position.
https://editor.gdevelop-app.com/?project=example://tween-animations

Hope this helps.

1 Like

Thank you for responding, btw!

The main reason I am not using Tween is because I plan on replacing the current force commands I have here with the ones similarly found in the Physics Engjne, as these moving parts are suppose to be pushing a circle with Physics2.0. IIRC, you can’t really use Tween if you have Physics 2.0… or maybe I’m wrong, hahaha. I’ll check when I get on my PC next.

posNum checking to be 1 is what allows the opposite direction movement to be possible. It starts at 0, but after it reached moveDest for the first time, it then changes to 1, making it go in the opposite direction. And the trigger once is there so there aren’t like 20 million copies of TestMove spawned in.

Sorry if my language is confusing or jumbled… I’ve taken a long break from GDevelop and I’m a bit rusty. :sweat_smile:

1 Like