Issue with monster moving left and right

Started by xboxown, Sun 18/06/2023 06:57:14

Previous topic - Next topic

xboxown

     Hello guys,

I need help in a basic scenario for me. I have a sea monster with two tentacles one on the left and one on the right. Their position is as follow:

Main monster head: 103, 107
Right tentacle: 158,111
Left tentacle: 56,112

This is the current code for the monster moving to the right:

Code: ags
if (!cSecondTentacle.Moving)
    
    cSecondTentacle.Move( 238, cSecondTentacle.y, eNoBlock, eAnywhere);
  if (!cSeaMonster.Moving)
  
    cSeaMonster.Move(190, cSeaMonster.y, eNoBlock, eAnywhere);
  if(!cTentacle.Moving)
   
    cTentacle.Move(145, cTentacle.y, eNoBlock, eAnywhere);

Right now the speed of the monster is super fast. How do I slow it down correctly? Second of all, how do I make it that monster move randomly between 0 to 238 with their tentancles and body in corrent distance between each other. i want it..when it reaches a certain waypoint or point it stops for like 5 seconds before picking another random position to go too. thanks in advance.

AndreasBlack

if you write random inside a script and push f1 you will see what the manual says about how randomness works! As for the monsters speed you can type forexample

Code: ags
cMonsters.SetWalkSpeed(1,1);

Khris

First of all, put this in repeatedly_execute_always:

Code: ags
  cTentacle.x = cSeaMonster.x - 47;
  cSecondTentacle.x = cSeaMonster.x + 57;

This way you don't have to worry about the tentacles at all.

Now implement a timer, like
Code: ags
  if (IsTimerExpired(1)) {
    int newX = cSeaMonster.x;
    // move at least by 30 pixels
    while (newX - SeaMonster.x < 30 || SeaMonster.x - newX < 30) {
      newX = Math.Random(160) + 40; // range 40 - 200
    }
    cSeaMonster.Move(newX, cSeaMonster.y, eNoBlock, eAnywhere);
    SetTimer((Math.Random(5) + 5) * GetGameSpeed()); // 5 - 10 seconds
  }

This code goes inside Room_RepExec, and you need to kick it off by calling SetTimer(1, 1); in room_Load.

xboxown

Thank you guys so much! I will implement the code today and try it out.

xboxown

#4
There is a bug in this line of code:

Code: ags
 SetTimer((Math.Random(5) + 5) * GetGameSpeed()); // 5 - 10 seconds

room3.asc(73): Error (line 73): Not enough parameters in call to function


There where other bugs as well. I changed the code to like this:

Code: ags
cTentacle.x = cSeaMonster.x - 47;
  cSecondTentacle.x = cSeaMonster.x + 57;
  if (IsTimerExpired(1)) {
    int newX = cSeaMonster.x;
    // move at least by 30 pixels
    while (newX - cSeaMonster.x < 30 || cSeaMonster.x - newX < 30) {
      newX = Random(160) + 40; // range 40 - 200
    }
    cSeaMonster.Move(newX, cSeaMonster.y, eNoBlock, eAnywhere);
    SetTimer((Random(5) + 5) * GetGameSpeed(), 1); // 5 - 10 seconds
    
  }

However, I don't know how to solve the last bug with the SetTimer saying not enough parameter.

xboxown

I ran the code and I got this error message


Khris

#6
Yeah, I typed this up pretty quickly. Try this version:

Code: ags
  if (IsTimerExpired(1)) {
    int newX, diff;
    do {
      newX = Random(160) + 40; // range 40 - 200
      diff = newX - cSeaMonster.x; if (diff < 0) diff = -diff;
    } while (diff < 30); // move by at least 30 pixels
    cSeaMonster.Move(newX, cSeaMonster.y, eNoBlock, eAnywhere);
    SetTimer(1, (Math.Random(5) + 5) * GetGameSpeed()); // 5 - 10 seconds
  }

Edit: code fixed (for good, I hope)

xboxown


xboxown

I removed Math from .Random and it works beautifully! THANK YOU FOR THIS AMAZING SOURCE CODE! THANK YOU THANK YOU THANK YOU! You did it! I appreciate all your help! THANK YOU!

Khris

Sorry, JavaScript-Brain :)
Glad it's working now.

SMF spam blocked by CleanTalk