Making a moving archery target using 2 circular objects.

Started by Egmundo Huevoz, Mon 25/12/2017 14:58:23

Previous topic - Next topic

Egmundo Huevoz

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:

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:



(Blanco1)



(Blanco2)

It looks like this in the room:



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
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

Spoiler
[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.



Cassiebsg

Just move one of them, then make the second object "follow exactly" in late-rep-exe.
There are those who believe that life here began out there...

Khris

Objects are positioned using their bottom left corner, not their center.
Which means you'll need

Code: ags
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.
Code: ags
  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.

Egmundo Huevoz

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".

dayowlron

Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Matti

Yes it is. It's also something you can easily look up by pressing F1 while using AGS (roll)

Snarky

Also, if you type "Math" it should bring up an autocomplete dropdown list with "Maths" highlighted.

SMF spam blocked by CleanTalk