Character change room when hit by object

Started by DoctorDan, Thu 26/06/2014 22:19:43

Previous topic - Next topic

DoctorDan

A very basic question. I'm trying to make my character change room when hit by an object. So when oArrow (object0 in the room) "hits" charlie he is moved back to room 1. Currently the arrow flies past charlie with no effect. To be honest, I'm unsure as how to define the character charlie in the AreThingsOverlapping function. Is that were I'm going wrong?

Code: ags


// room script file

function room_AfterFadeIn()
{
  oArrow.Move(700, 560, 10, eNoBlock, eAnywhere);


if (AreThingsOverlapping(1000, CHARLIE)){
  cCharlie.ChangeRoom(1);
}
}


Eastwing

#1
'AreThingsOverlapping' uses CharID, which is just number of your character. For example, if cCharlie was created first, his ID is 0. You can see CharID right in the Editor.

DoctorDan

I've tried that too. Using "3", the character number in place of "CHARLIE" but still no success.

Eastwing

#3
Oh, I got it.

You check colliding when your character and an arraow are NOT colliding yet

Try this:
Code: ags
function room_AfterFadeIn()
{
  oArrow.Move(700, 560, 10, eNoBlock, eAnywhere);
 
 while (oArrow.Moving)
    if (AreThingsOverlapping(1000, CHARLIE))
    {
      oArrow.StopMoving();
      cCharlie.ChangeRoom(1);
    }
}

(Not tested)

DoctorDan

#4
Thanks for your help, but this still isn't working. This causes an error stating "the script appears to be hung. A while loop ran 150001 times"

Eastwing

#5
Unfortunately I can't test scripts right now.

Try to move checking to RepExec.

Code: ags
function room_AfterFadeIn()
{
  oArrow.Move(700, 560, 10, eNoBlock, eAnywhere);
}

function room_RepExec()
{
  if (oArrow.Moving)
    if (AreThingsOverlapping(1000, CHARLIE))
    {
      oArrow.StopMoving();
      cCharlie.ChangeRoom(1);
    }
}

Khris

Calling AreThingsOverlapping() in room_AfterFadeIn will run the check exactly once, immediately after fading in, then never again. It only checks the current situation, right when it is called, it does not set up behavior that extends into the future.

Like Eastwing eventually suggested, checks like that have to go into the room's repeatedly execute event, where the condition is checked 40 times per second.

To make the code a bit more readable, you can do this:
Code: ags
    if (AreThingsOverlapping(cCharlie.ID, oArrow.ID + 1000)) {
      ...
    }

DoctorDan

Ah, this makes sense about it only checking it once on the room's load. I have a better idea of how it's working now.

Thank you both!

SMF spam blocked by CleanTalk