While loops

Yes it is possible, but it requires more logic. Someone different than me should be able to help you.

so
who is this someone else

i didnā€™t look super hard at this, but in short, unless you have a break somewhere in your code to exit the loop, NEVER use while(true).

iā€™m guessing what you want is that every frame, something happens. for example, a number gets 1 bigger every frame.

in a while(true), you might think that every loop cycle is one frame. for example, after 5 frames, the loop has looped 5 times.

what is actually happening is that the code waits for all the code to ā€œendā€ before going to the next frame. so if a very big loop takes 5 seconds to loop once, a frame will last that whole 5 seconds.

so with a while(true), you are stalling the frame for infinitely long. since the loop didnā€™t stop and the code didnā€™t ā€œendā€, the project keeps waiting until all the code is done. but in that loop, it never ends. therefore the code keeps going and your page eventually crashes.

so what do you do instead? you use the ā€œupdateā€ script. this will run once every frame. so if you want to increment a variable by 1 every frame, put variableName++;. this means that every frame(update script), variableName will increment by 1 (the code you put in).

do not put a while loop inside the ā€œupdateā€ script to increment by 1. youā€™ll get the same problem as before.

thanks for listening to my ted talk.

2 Likes

ok so
how does this solve my problem

I think that your approach to solve a problem is incorrect. This is a design problem more than anything elseā€¦ you need to use another method. you need to calculate the distance between the two objects and make desicions based on how many pixles you want to move within a frame. Lets say that your two objects are 50pxls away in distanceā€¦ and you move the object 30pixels per frameā€¦ the first frame you will be moving 30pixels completely, but then at your second frame, now your two objects are at 20pxls of distance, but you want to move 30pxls, then you should be able to move your object only 20pxlsā€¦ of course you have to add logic for the width of the objects etcā€¦

(You should be able to implement what I just wrote above). Sorry I canā€™t implement that for youā€¦ It will require significant amount of timeā€¦ Hope it helps.

i know my solution is wrong
how do i make one thats right

how do you calculate the distance

Sorry this is not the kind of help that we should provideā€¦ The loop problem is solved. Iā€™ll close this topic. Just google itā€¦ ā€œHow to find distance between two rectanglesā€