Background object move loop aka patrolling?

Started by Licht61, Sun 12/09/2021 20:30:57

Previous topic - Next topic

Licht61

Hello again!

I has room with cars on the background and I need to make them move in a loop. I've made few objects with sprite that has multiple flying cars on it. I've need them to move from one side of the screen to another on repeat. For example here's the one of the attempts that looks basic for me, but i've failed

Code: ags

function room_RepExec()
{
  oFlyCars2.Move (-30, 26, 10, eNoBlock, eAnywhere);
  oFlyCars2.SetPosition (330, 26);
  
}



Slasher

#1
You need to change Move to AddWaypoint command which is also non blocking in the background...

arj0n

#2
Quote from: Slasher on Mon 13/09/2021 03:43:48
You need to change Move to AddWaypoint command which is also non blocking in the background...
AddWaypoint is for characters, not for objects.

Below is an example:
First it sets 3 cars at initial startppoint in the leftside, outside the screen (in room_Load).
Then a loop starts to move 1 car (out of 3 in this example) each time (in room_RepExec):
(1) pick one random car out of the 3
(2) move that car to the rightside outside screen
(3) set moved car back to start position
(4) wait 3 seconds (change that with the waitUntilNextCarStarts value) and loop again

Try something like this (not tested, pseudo code):
Code: ags

// room script file
bool carIsMoving;
int waitUntilNextCarStarts;
  
function room_Load()
{
  oFlyCars1.X = -50;
  oFlyCars1.Y = 26;
  
  oFlyCars2.X = -50;
  oFlyCars2.Y = 26;
  
  oFlyCars3.X = -50;
  oFlyCars3.Y = 26;
  
  carIsMoving = false;
  waitUntilNextCarStarts = 120; //40 is 1 second
}

function room_RepExec()
{
  
  if (carIsMoving == false) int ranCar = Random (2);
  
  if (ranCar == 0)
  {
    oFlyCars1.Move (380, 26, 10, eNoBlock, eAnywhere);
    carIsMoving = true;
  }
  else if (ranCar == 1)
  {
    oFlyCars2.Move (380, 26, 10, eNoBlock, eAnywhere);
    carIsMoving = true;
  }
  else if (ranCar == 2)
  {
    oFlyCars3.Move (380, 26, 10, eNoBlock, eAnywhere);
    carIsMoving = true;
  }
  
  if ((oFlyCars1.X == 380) || (oFlyCars2.X == 380) || (oFlyCars3.X == 380))
  {
    oFlyCars1.SetPosition (-50, 26);
    oFlyCars2.SetPosition (-50, 26);
    oFlyCars3.SetPosition (-50, 26);
    SetTimer (1, waitUntilNextCarStarts); 
  }
  
  if (IsTimerExpired (1)
  {
    carIsMoving = false;
  }
}

Slasher

Quote from: arj0n on Mon 13/09/2021 08:13:28
Quote from: Slasher on Mon 13/09/2021 03:43:48
You need to change Move to AddWaypoint command which is also non blocking in the background...
AddWaypoint is for characters, not for objects.
Yes, of course..  (nod)

Matti

#4
You don't need a variable to check whether the cars are moving, you can use Object.Moving. You can also shorten the code if you make the car objects consecutive and use their IDs.

Untested:

Code: ags

  // this goes into the RepExec, you need to set the car's coordinates in room_load like arj0n did
  for (int i = 1; i < 4;  i++) // assuming that the object numbers are 1,2,3
  {
    if (object[i].Moving == false)
    {
      if (object[i].X == -30) object[i].X = 330;
      r = random(100); // randomizing the new loop for each car instead of having a fixed timer
      if (r == 10)
        object[i].Move (-30, 26, 10, eNoBlock, eAnywhere);
    }
  }


Quote from: Licht61 on Sun 12/09/2021 20:30:57
For example here's the one of the attempts that looks basic for me, but i've failed

That's because you're constantly resetting the car's position so it never has a chance to actually move.

deadsuperhero

This is just my 2 cents, but...given that you want to have multiple things moving around in the background, would it maybe make sense to use characters instead of objects?
The fediverse needs great indie game developers! Find me there!

SMF spam blocked by CleanTalk