Jump to content


Photo

Problem with looping variable


  • Please log in to reply
4 replies to this topic

#1 v0xc

v0xc

    GMC Member

  • New Member
  • 82 posts

Posted 16 March 2011 - 03:34 PM

I'm making a door which opens when touched and then closes when touched again and opens when touched again and on and on.
But there's a problem, it loops, first it checks if the 'open' value is 0 then turns it into 1, then after that it checks if it is 1 then changes it back to 0.
if (open=0)
{
    solid=false;
    open=1;
    sprite_index=spr_door;
    image_index=2;
    image_speed=0;
}
if (open=1)
{
    solid=true;
    open=0;
    sprite_index=spr_door;
    image_index=1;
    image_speed=0;
}

How do I fix this?
  • 0

#2 iPhoenix

iPhoenix

    GMC Member

  • New Member
  • 20 posts

Posted 16 March 2011 - 03:53 PM

There are multiple ways to solve this. The most "clean" one is probably using a switch-statement:
switch open
{ case 0:
    solid=false;
    open=1;
    sprite_index=spr_door;
    image_index=2;
    image_speed=0;
    break;

  case 1:
    solid=true;
    open=0;
    sprite_index=spr_door;
    image_index=1;
    image_speed=0;
    break; }

  • 0

#3 LSnK

LSnK

    NaN

  • GMC Member
  • 1188 posts

Posted 16 March 2011 - 03:53 PM

Use if-else.

if (open==0)
{
    solid=false;
    open=1;
    sprite_index=spr_door;
    image_index=2;
    image_speed=0;
}
else
{
    solid=true;
    open=0;
    sprite_index=spr_door;
    image_index=1;
    image_speed=0;
}

  • 0

#4 Davve

Davve

    Procrastinator

  • GMC Member
  • 3665 posts
  • Version:GM8.1

Posted 16 March 2011 - 03:58 PM

More compact version:
open!=open
sprite_index=spr_door
solid=open
image_index=1+!open
image_speed=0

  • 0

#5 v0xc

v0xc

    GMC Member

  • New Member
  • 82 posts

Posted 16 March 2011 - 04:01 PM

Thanks, both worked.
Follow-up question: how do I make it not solid for a certain object but solid for others?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users