Loop the loop; advice sought

Game Editor comments and discussion.

Loop the loop; advice sought

Postby akhad » Fri Jul 18, 2008 1:37 pm

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, "");
}
User avatar
akhad
 
Posts: 43
Joined: Tue Jan 16, 2007 1:59 pm
Location: U.K
Score: 0 Give a positive score

Re: Loop the loop; advice sought

Postby DST » Fri Jul 18, 2008 10:19 pm

who is performing the checks? an external actor, or the cubes themselves? Same thing, who contains the moveto command?
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Loop the loop; advice sought

Postby feral » Fri Jul 18, 2008 11:03 pm

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
}

???
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score

Re: Loop the loop; advice sought

Postby akhad » Sat Jul 19, 2008 8:52 pm

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.
Attachments
MoveT.zip
simple MoveTo
(208.66 KiB) Downloaded 112 times
User avatar
akhad
 
Posts: 43
Joined: Tue Jan 16, 2007 1:59 pm
Location: U.K
Score: 0 Give a positive score

Re: Loop the loop; advice sought

Postby DST » Sat Jul 19, 2008 10:11 pm

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.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Loop the loop; advice sought

Postby feral » Sat Jul 19, 2008 11:49 pm

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
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron