How do I make my door open once only?

Started by Dr.Alban, Fri 06/06/2008 01:49:02

Previous topic - Next topic

Dr.Alban

So, this seems very simple, but it is turning out to be difficult to execute.

I want to make it so that interacting with a hotspot opens a door.  Further, I want to make sure that interacting with that hotspot a second time makes the door close, rather than open a second time.  So I created a variable that keeps track of whether the door is open or closed:


function hButton_Interact()

{
  if (myDoor == 0)

  {
    cAlban.LockView(2);
    cAlban.Walk(171, 124, eBlock);
    cAlban.FaceLocation(148, 102);
    oDoor.SetView(4, 0, 0);
    oDoor.Animate(0, 5, eOnce, eBlock, eForwards);
    cAlban.UnlockView();
    myDoor ++;
   }

if (myDoor == 1)

  {
    cAlban.LockView(2);
    cAlban.Walk(171, 124, eBlock);
    cAlban.FaceLocation(148, 102);
    oDoor.SetView(4, 0, 0);
    oDoor.Animate(0, 5, eOnce, eBlock, eBackwards);
    cAlban.UnlockView();
    myDoor --;
   }
}


But now, when the character interacts with the hotspot, the door opens and then immediately closes.  Looking at the code, I understand why this is happening, but I can't figure out a way to script around it.  Am I not nesting things correctly?

Thanks in advance for the help.

skuttleman

Change
Code: ags

if (myDoor == 1)


to:

Code: ags

else if (myDoor == 1)

Dr.Alban

Thanks.  I knew it had to be a simple solution.

Khris

You can shorten this considerably:
Code: ags
function hButton_Interact() {
  cAlban.LockView(2);
  cAlban.Walk(171, 124, eBlock);
  cAlban.FaceLocation(148, 102);
  oDoor.SetView(4, 0, 0);
  if (myDoor) oDoor.Animate(0, 5, eOnce, eBlock, eBackwards);
  else oDoor.Animate(0, 5, eOnce, eBlock, eForwards);
  myDoor = 1-myDoor;
}

magintz

A tip I find helpful is when dealing with simple yes / no things is to declare it as a bool instead of an int. That way you can have a variable called 'doorOpen', or whatever, and assign it to 'true' or 'false'. I find this way easier to read when viewing the code, but it's completely up to you.

Code: ags

bool doorOpen;
if(doorOpen == true) {
  // Close door
}
else {
  // Open door
}
When I was a little kid we had a sand box. It was a quicksand box. I was an only child... eventually.

Khris

Or just
  if (doorOpen) { ...
Works with ints, too. Plus, an int can be switched using
  doorOpen = 1-doorOpen;

Pumaman

Well, a bool can be switched with:

doorOpen = !doorOpen;

which is a bit easier to read :P

SMF spam blocked by CleanTalk