# Problem with two object and for each.

Discussion and feedback on Construct 2

### » Thu Mar 30, 2017 9:40 pm

Hi, can anyone explain?

This formula works with an object, but if you have two objects then the second object escapes, why ?.

https://www.dropbox.com/sh/ehrch6zye1tuq33/AACqlv_6UnWbOaJSLhy8n3Era?dl=0
B
35
S
14
G
22
Posts: 916
Reputation: 15,100

### » Thu Mar 30, 2017 9:55 pm

If the condition in event 2 is true for the first sprite, then it has to be untrue before it can run again due the 'trigger once while true'. As a consequence, when the condition is true for the second sprite(after it was true for the first one) it will not run.

Why is the 'trigger once while true' needed ?
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Thu Mar 30, 2017 9:59 pm

99Instances2Go wrote:If the condition in event 2 is true for the first sprite, then it has to be untrue before it can run again due the 'trigger once while true'. As a consequence, when the condition is true for the second sprite(after it was true for the first one) it will not run.

Why is the 'trigger once while true' needed ?

Okay, now I understand what you say.
If you remove the trigger the sprites are trapped in the tile, the trigger changes them once only until they re-enter the other tile and change direction again.

Thanks, I'll have to find a solution.
B
35
S
14
G
22
Posts: 916
Reputation: 15,100

### » Thu Mar 30, 2017 10:50 pm

Dont understand situation.

I suppose 'velocity' is the amount pixels/tick that you move the sprite.
'velocity' can be positive or negative (event 3 & 4).
There is no Y position changed, so i suppose the sprite move only horizontal.

The sprite can approach the tile from the left and from the right. (neg and pos velocity)
We look in the future (x+velocity) and when sprite overlaps a tile, it gets positioned just outside the tile.
Then its velocity is mirrored. So far the basics. As far as i understand them.

When the sprite is approaching from the right (negative velocity) it gets positioned outside the tile on the right side of the tile.
But. When the sprite is approaching from the left, it gets ALSO positioned outside the tile on the right side of the tile (tile index * 32 PLUS 16). Now it is flying trough the tile. The positive velocity is set to negative, as a result it returns (starts moving to the left) inside the tile (AGAIN) the next tick. And is indeed locked up.

I think it should be (again if i understand it right, i dont see the layout, so sorry if i am again wrong)

For each Sprite
______________ Set sprite to the future
___ Sub event :The tile check condition
____________Sub event : velocity > 0 <--- means it is approaching coming from the left.
___________________action: set outside tile .. = (tile index * 32) - 16 <------ on the left side
___________________action: make velocity positive
____________Sub: Else <--- means it is approaching coming from the right.
___________________action: set outside tile .. = (tile index * 32) + 16 <------ on the right side
___________________action: make velocity negative
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Thu Mar 30, 2017 10:59 pm

I guess that's another way to do it.
I just removed the trigger and added a bool variable in the object and now it works.

https://www.dropbox.com/sh/ehrch6zye1tu ... n3Era?dl=0
B
35
S
14
G
22
Posts: 916
Reputation: 15,100