Something is wrong with this loop

Started by arj0n, Wed 28/07/2010 12:54:02

Previous topic - Next topic

arj0n

I have an if, else statement for 1 object.
But I have several objects for the same if, else statement,
so that statement now needs to be copy-pasted for each object.

How can I group them first, for example as X and then run the if, else statement
only once for X in stead of running the statement all objects1, then running it for object2, etc.?

I hope my explanation makes any sense  :P

Arj0n.

Joe

Well, you "can't" do that, but... what about a loop?

Code: ags

int i=0;

while(i<NUM_OBJECTS){
  object[i].stuff;
  //more stuff 
  i++;
}


NUM_OBJECTS is a constant value which you must define
Copinstar © Oficial Site

Khris

There are two options I can think of right now.

For the sake of the explanation, I'll assume you have a room full of crates that can be pushed.

1.
Code: ags
function crate_push(Object*crate) {
  if (crate.X > 100) {
    ...
  }
  else ...
    ...
}

function oCrate1_Interact() {
  crate_push(oCrate1);
}

function oCrate2_Interact() {
  crate_push(oCrate2);
}

...


That's the straightforward way. crate is a pointer pointing at the clicked crate object.

The other is to assign the crate_push() function to every crate object's "player interacts with" event by entering "crate_push" into the textfield, then use
Code: ags
  Object*crate = Object.GetAtScreenXY(mouse.x, mouse.y);
  if (crate == null) return;

at the beginning of the function.

arj0n

Quote from: Joe Carl on Wed 28/07/2010 12:57:24
Well, you "can't" do that, but... what about a loop?

Code: ags

int i=0;

while(i<NUM_OBJECTS){
  object[i].stuff;
  //more stuff 
  i++;
}


NUM_OBJECTS is a constant value which you must define

Getting a parse error having this:

{
Object* block [3];
block[1] = oBlock1;
block[2] = oBlock2;
block[3] = oBlock3;
int i=0;
while(i<[1]){
if (((Game.SpriteHeight[object.Graphic] == 60) && myobject.X+120==object.X && myobject.Y-60==oBlock1.Y-60) && myobject.X <=360 &&  myobject.ClickedRightHalf()) myobject.Move(myobject.X+10, myobject.Y, 10, eNoBlock, eAnywhere);
//more stuff
i++;
}

I'm sure there's more wrong with my 'loop'....

tzachs

The parsing error is due to the while line, it should be:

Code: ags

while (i<3)


Besides that, the indices for the array start with 0, not with 1, so you need to put:

Code: ags

Object* block [3];
block[0] = oBlock1;
block[1] = oBlock2;
block[2] = oBlock3;


And last thing, I don't see that you use the array inside the while loop, you might want to replace the reference you made there to oBlock1 to block[ i ] (and maybe not, because I don't really know what you're trying to do in there).

arj0n

#5
@tzachs
oBlock1 a.o. needed to change to block[ i] indeed but it wasn't changed to show where the aray should be used.

Saving the room script with this loop goes ok
Running the game also goes ok.
But it doesn't do what it's suppose to do.


Code: ags

// room script file
  bool ClickedRightHalf(this Object*) { //call when the object has been clicked
  if (mouse.x + GetViewportX() > (this.X + (Game.SpriteWidth[this.Graphic]/2))) return true;
  }
  bool ClickedLowerHalf(this Object*) { //call when the object has been clicked
  if (mouse.y + GetViewportY() > (this.Y - (Game.SpriteHeight[this.Graphic]/2))) return true;
  }
  
function Block_left_right()
{
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
 
//Move to the left
  Object* block [2];
  block[0] = oBlock1;
  block[1] = oBlock4;

  int i=0;
  while(i<2){
 
  if (Game.SpriteHeight[object[i].Graphic] == 60) 
     {myobject.Move(myobject.X+00, myobject.Y, 10, eNoBlock, eAnywhere);
     Display ("can't move");}

  else if   (myobject.X <=360 && 
             myobject.ClickedRightHalf()) {myobject.Move(myobject.X+60, myobject.Y, 10, eNoBlock, eAnywhere);
             aClick.Play();}
  i++;          
  } 



Now it runs the "If" to check if myobject can't move (which works fine, I know by using "Display"),
but then it runs the "Else If" and let myobject also move.
It should be one of the two options instead of doing both.
Can't find what is wrong with the loop now  ???

Joe

i++ should be inside the loop at the end.
Copinstar © Oficial Site

arj0n

Quote from: Joe on Sat 31/07/2010 11:24:47
i++ should be inside the loop at the end.

I guess like this?
But now the loopscript hangs while running the game and clicking the "myobject"...

Code: ags

// room script file
  bool ClickedRightHalf(this Object*) { //call when the object has been clicked
  if (mouse.x + GetViewportX() > (this.X + (Game.SpriteWidth[this.Graphic]/2))) return true;
  }
  bool ClickedLowerHalf(this Object*) { //call when the object has been clicked
  if (mouse.y + GetViewportY() > (this.Y - (Game.SpriteHeight[this.Graphic]/2))) return true;
  }
  
function Block_left_right()
{
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
 
//Move to the left
  Object* block [2];
  block[0] = oBlock1;
  block[1] = oBlock4;

  int i=0;
  while(i<2){
 
  if (Game.SpriteHeight[object[i].Graphic] == 60) 
     {myobject.Move(myobject.X+00, myobject.Y, 10, eNoBlock, eAnywhere);
     Display ("can't move");

  i++; }  

  else if   (myobject.X <=360 && 
             myobject.ClickedRightHalf()) {myobject.Move(myobject.X+60, myobject.Y, 10, eNoBlock, eAnywhere);
             aClick.Play();}
      
  } 

Joe

Now you're missing a brace after this: Display ("can't move");
Copinstar © Oficial Site

arj0n

#9
That's right, my mistake  ;)

Code: ags

// room script file
  bool ClickedRightHalf(this Object*) { //call when the object has been clicked
  if (mouse.x + GetViewportX() > (this.X + (Game.SpriteWidth[this.Graphic]/2))) return true;
  }
  bool ClickedLowerHalf(this Object*) { //call when the object has been clicked
  if (mouse.y + GetViewportY() > (this.Y - (Game.SpriteHeight[this.Graphic]/2))) return true;
  }
  
function Block_left_right()
{
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
 
//Move to the left

Object* block [6];
  block[0] = oBlock1;
  block[1] = oBlock2;
  block[2] = oBlock4;
  block[3] = oBlock5;
  block[4] = oBlock6;
  block[5] = oBlock7;

  int i=0;
  while(i<6){
 
  if ((Game.SpriteHeight[object[i].Graphic] == 60 && myobject.X+120==object[i].X && 
             myobject.Y-60==object[i].Y-60) && myobject.X <=360 &&  myobject.ClickedRightHalf())
             {myobject.Move(myobject.X+00, myobject.Y, 10, eNoBlock, eAnywhere);
             Display ("can't move");}
 
  else if  (myobject.X <=360 && 
             myobject.ClickedRightHalf()) {myobject.Move(myobject.X+60, myobject.Y, 10, eNoBlock, eAnywhere);
             aClick.Play();}
i++;}   
} 

OK, the IF should prevent 'myobject' to move, if not the ELSE IF should move 'myobject'.
With this code it displays "can't move" but after that it still moves 'myobject'?


Joe

Try this:

Code: ags

  while(i<6){
    if ((object[i].Height == 60 && myobject.X+120==object[i].X && 
    myobject.Y-60==object[i].Y-60) && myobject.X <=360 &&  myobject.ClickedRightHalf()){
      Display ("can't move");
      myobject.StopMoving(); 
    }
    
    else if  (myobject.X <=360 &&  myobject.ClickedRightHalf()) {
      myobject.Move(myobject.X+60, myobject.Y, 10, eNoBlock, eAnywhere);
      aClick.Play();
    }

    i++;
  }   
Copinstar © Oficial Site

SMF spam blocked by CleanTalk