Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Egmundo Huevoz on Mon 25/12/2017 14:58:23

Title: Making a moving archery target using 2 circular objects.
Post by: Egmundo Huevoz on Mon 25/12/2017 14:58:23
Hello! In the game I'm making, I have a room where the character has to shoot (click) the center of a moving target that looks like this:
(https://i.imgur.com/FKuIycT.png)
The thing is, I want it to only be a valid shot when they click the center (the yellow circle). So I figured I'd use 2 objects, like this:

(https://i.imgur.com/SypXSVI.jpg)

(Blanco1)

(https://i.imgur.com/4Ode8o4.png)

(Blanco2)

It looks like this in the room:

(https://i.imgur.com/2QkrAZ7.jpg)

The 2nd object is set as unclickable in the room script. Then I plan to make them move randomly in a walkable area.

Code (ags) Select
function room_RepExec()
{
if (!oBlanco.Moving) {
  int walkx = Random (401);
  int walky =  Random (641);
  if (GetWalkableAreaAt (walkx, walky) == 1)
    oBlanco.Move (walkx, walky, 1);
    oBlanco2.Move (walkx, walky, 1);
}
}


It works. But the smaller target (Blanco1) moves elsewhere for some reason, before coordinating its movement with Blanco2.

HERE'S A VIDEO OF IT (https://www.youtube.com/watch?v=2V1whwW5dQM)

Spoiler
(https://i.imgur.com/4VnJhIG.jpg)
[close]

I've read the whole Object section in the manual, I've tried to adjust the first object by adding and substracting the difference in pixels with the 2nd object... All to no avail. Thanks in advance for any help provided.


Title: Re: Making a moving archery target using 2 circular objects.
Post by: Cassiebsg on Mon 25/12/2017 15:11:04
Just move one of them, then make the second object "follow exactly" in late-rep-exe.
Title: Re: Making a moving archery target using 2 circular objects.
Post by: Khris on Mon 25/12/2017 16:51:38
Objects are positioned using their bottom left corner, not their center.
Which means you'll need

function room_RepExec()
{
  if (!oBlanco.Moving) {
    int walkx = Random(System.ViewportWidth - Game.SpriteWidth[oBlanco2.Graphic]);
    int h = Game.SpriteHeight[oBlanco2.Graphic];
    int walky = Random(System.ViewportHeight - h) + h;
    if (GetWalkableAreaAt (walkx, walky) == 1) {
      oBlanco.Move (walkx + 73, walky - 73, 1);
      oBlanco2.Move (walkx, walky, 1);
    }
  }
}

(I also wrapped both Move.Commands in your GetWalkableAreaAt check, you only moved oBlanco conditionally)

Finally: using Pythagorean math, you can easily calculate the distance of the click from the bull's eye, determining the exact ring they hit while using a single sprite.
  float dx = IntToFloat(oBlanco.X + 89 - mouse.x);
  float dy = IntToFloat(oBlanco.Y - 89 - mouse.y);
  int dist = FloatToInt(Maths.Sqrt(dx*dx + dy*dy), eRoundNearest);
  if (dist < 17) Display("yellow");


Edit: fixed Math/s typo.
Title: Re: Making a moving archery target using 2 circular objects.
Post by: Egmundo Huevoz on Tue 26/12/2017 17:55:13
Thanks a lot, Khris! But I've encountered a problem. When I try to run it, it says "undefined symbol: Math". Maybe I pasted that last bit on the wrong function? I pasted it on the room's "on_mouse_click".
Title: Re: Making a moving archery target using 2 circular objects.
Post by: dayowlron on Tue 26/12/2017 19:29:24
I believe it is actually Maths.
Title: Re: Making a moving archery target using 2 circular objects.
Post by: Matti on Tue 26/12/2017 20:28:13
Yes it is. It's also something you can easily look up by pressing F1 while using AGS (roll)
Title: Re: Making a moving archery target using 2 circular objects.
Post by: Snarky on Tue 26/12/2017 21:34:18
Also, if you type "Math" it should bring up an autocomplete dropdown list with "Maths" highlighted.