Page 1 of 1

Loop the loop; advice sought

PostPosted: Fri Jul 18, 2008 1:37 pm
by akhad
I need to move 10 actor clones named `Cube` across the screen, individually,moving slowly across 40 pixels, not in 40 pixel jumps.
Only the selected Cube is to move. Note that my code works, I've left out other code and variable declares to
keep it compact so syntax etc isn't the problem here.. I just need some extra code to repeat the code correctly.
Explanation:
The array CubeMove[10] holds a `1` for the corresponding cube actor that is to move.
If 1 is found, a check is then
made to ensure the Cube clone is not already moving (xvelocity==0)
The Cube actors name is then formatted with the correct clone index number (i.e Cube.1) with
sprintf and the correct clone then is moved with MoveTo. Fine. My problem is that if the above code is
triggered, it works just once. I've tried to put the above into an outer, nested loop to make it
carry out the MoveTo command say 5 times in 1 loop but it won't. Or if it does the screen doesn't update
the movement with the outer loop. The only way is to trigger it, say with a keypress, by pressing the key
5 times. I want to repeat it say 5 times, with a variable value like e.g Numberofmoves=5 automatically.
Hope this is clear enough.

example code:

for(i=0;i<10;i++) {

if ((CubeMove[i] ==1) && ( getclone2("Cube",i)->xvelocity==0))
{sprintf(cubemove, "Cube.%ld",i); MoveTo(cubemove, 40.0, 0, 2.0, cubemove, "");
}

Re: Loop the loop; advice sought

PostPosted: Fri Jul 18, 2008 10:19 pm
by DST
who is performing the checks? an external actor, or the cubes themselves? Same thing, who contains the moveto command?

Re: Loop the loop; advice sought

PostPosted: Fri Jul 18, 2008 11:03 pm
by feral
rather then setting cubemove[] to 1 and checking that... how about setting it to your number of steps variable.. eg: 5 steps then subtracting 1 each time it loops, so it runs as many times as needed until the var reaches zero

eg: set var=5;
CubeMove[i]=var; //set number of steps

then in the draw loop

if ((CubeMove[i] >0) && ( getclone2("Cube",i)->xvelocity==0))
{
//do actions

CubeMove[i]=CubeMove[i]-1; //subtract 1
}

???

Re: Loop the loop; advice sought

PostPosted: Sat Jul 19, 2008 8:52 pm
by akhad
Ok I know I didn't explain the situation too well. I've simplified the routine a lot and posted an example. In this code there's just one actor (Ball) that moves. Ball moves 20 pix across (using MoveTo) each time Space is pressed, then stops. It then moves another 20 pix when the Space bar is again pressed. It will repeat this movment X amount of times, X being the variable `move`. In the sample code, `move` variable is set to 7, so this Space Bar/move 20 pixels movement will work 8 times total (0-7). The Ball actor then moves no more, as move=0. I need an outer loop to make `Ball` move 20pix for the total 8 times, continuously until `move`=0. It needs to be 8*20 pix movements as I need to check values after each move.
I jus can't seem to update the `Ball` actor/MoveTo code correctly within the loop.
The attached code has some more remarks to clarify.

Re: Loop the loop; advice sought

PostPosted: Sat Jul 19, 2008 10:11 pm
by DST
One way would be to use 'move finish'.

like on the space down, say
Code: Select all
if(whatever){MoveTo("Event Actor", x+20, y, 6, "", "");
move--;

and on move finish,
Code: Select all
if (move>0){
MoveTo("Event Actor", X+20, y, 6, "", "");
move--;


On the last move finish move would be 0 and no more moves.

Re: Loop the loop; advice sought

PostPosted: Sat Jul 19, 2008 11:49 pm
by feral
I agree with DST the "move finish" sounds the easiest,


But, If the movefinish idea doesn't work for what ever reason, And I am suggesting this more because others may read this post for a solution and I wanted to suggest a more "generalized' idea that could solve other similar problems.

and not trying to out do DST at all... :lol: just thought a more general solution may also help others.. see attached example ged file below..

IMHO. I generally try to avoid putting code into key presses, because of the similar problems you are having, and that is controlling loops can be difficult, and the code in the keypress script cannot then be accessed/changed/stopped from other actors (can, but not easily)....

Instead, I usually only use the keypress to update/change a variable ,and/or set a "flag". and then use that variable/flag in the actor draw event.

eg: for this type of problem I would use the keypress to update the move variable, but have the code that does the move in the draw actor
eg
on keypress event
Code: Select all
if (move==0)  //only update is move steps complete
{
    move=8;
}


then move the "working code" out to the draw actor event ..

Code: Select all
if ((move >0) &&  ( Ball.xvelocity==0))
    {
      move--;
      MoveTo("Ball", 30.00, 0.00, 2.000000, "Ball", "");
    }




Another way is to use the keypress simply to update a "flag" variable.. this is useful when you want the action to stop for some reason but you want to keep the "number of remaining moves"

that is, the movement pauses, but requires a another keypress to continue the remaining number of moves

on keypress event set flag
spacebar=1;

now at anytime you can stop the action by saying
spacebar=0;
and still keep your remaining 'moves"

then in the draw actor (or anywhere now)

something like
Code: Select all
if (spacebar==1)   // this can be turned off by other events to pause movement,
                                    //and resets by a new keypress to continue
{
   if ((move >0) &&  ( Ball.xvelocity==0))
      {
      move--;
      MoveTo("Ball", 30.00, 0.00, 2.000000, "Ball", "");
      }
}

if (move<0) //if no moves left reset flag ready for new keypress
{
  spacebar=0;
}


attached updated code - press space to move, return to stop moving, space to continue OR to reset
keymove.zip
ged files
(201.14 KiB) Downloaded 86 times