Hey Guys I have a scene in my game where the player gets chased after collecting an item. This is the script so far I can get the crab to follow the player, but the animation plays straight away and the crab is still on the screen walking towards player while it plays.
function oObject0_Interact(Object *theObject, CursorMode mode)
{
cEgo.Walk(oObject0.X, oObject0.Y, eBlock, eWalkableAreas);
cEgo.LockView(EGOPICKUP);
cEgo.Animate(0, 7, eOnce, eBlock);
cEgo.UnlockView();
oObject0.Visible = false;
cEgo.AddInventory(iKey);
Display("You have found the key!");
// ////
cCrabboss.PlaceOnWalkableArea();
cCrabboss.FollowCharacter(cEgo, 5, true);
cEgo.LockView(76);
player.Animate(0, 7, eOnce, eBlock);
Display("CrabBoss caught you!");
}
In general it's helpful to change object names from Object0 to something like oPearl right after creating them; this will generate function names like oPearl_Interact, making the code more readable.
Anyway, the issue is that the crab catching the player is a separate event based on distance, so you need to move it to the room's repeatedly execute event.
Add the event in the room events pane; this will add the function to the end of the script.
// end of function oObject0_Interact
cCrabboss.FollowCharacter(cEgo, 5, true);
}
int DistanceFrom(this Character*, Character* other) {
float dx = IntToFloat(other.x - this.x), dy = IntToFloat(other.y - this.y) * 1.5; // perspective correction
return FloatToInt(Maths.Sqrt(dx * dx + dy * dy), eRoundNearest);
}
// generated function
function room_RepExec() {
if (cCrabboss.Following == player && cCrabboss.DistanceFrom(player) < 20) {
cCrabboss.FollowCharacter(null);
cCrabboss.StopMoving();
player.StopMoving();
player.LockView(76);
player.Animate(0, 7, eOnce, eBlock);
Display("CrabBoss caught you!");
}
}
Quote from: Khris on Yesterday at 13:17:07In general it's helpful to change object names from Object0 to something like oPearl right after creating them; this will generate function names like oPearl_Interact, making the code more readable.
Anyway, the issue is that the crab catching the player is a separate event based on distance, so you need to move it to the room's repeatedly execute event.
Add the event in the room events pane; this will add the function to the end of the script.
// end of function oObject0_Interact
cCrabboss.FollowCharacter(cEgo, 5, true);
}
int DistanceFrom(this Character*, Character* other) {
float dx = IntToFloat(other.x - this.x), dy = IntToFloat(other.y - this.y) * 1.5; // perspective correction
return FloatToInt(Maths.Sqrt(dx * dx + dy * dy), eRoundNearest);
}
// generated function
function room_RepExec() {
if (cCrabboss.Following == player && cCrabboss.DistanceFrom(player) < 20) {
cCrabboss.FollowCharacter(null);
cCrabboss.StopMoving();
player.StopMoving();
player.LockView(76);
player.Animate(0, 7, eOnce, eBlock);
Display("CrabBoss caught you!");
}
}
Thank you so much how would you code it so if they escape the room before being caught the chase stops?
I just noticed that you've passed true to the FollowCharacter (https://adventuregamestudio.github.io/ags-manual/Character.html#characterfollowcharacter) command; the third param is supposed to be an integer. Pass 0 to make the crab chase the player.
As for stopping the chase, use the "player leaves room after fade-out" event:
// generated function
function room_Unload()
{
cCrabboss.FollowCharacter(null);
}