Problem with tracking variable(Solved)

Started by Maverick, Sun 10/12/2006 20:13:58

Previous topic - Next topic

Maverick

I have 3 objects that must change every time you click on them.
I've set up a view (2) with a loop(1) and 10 sprites. I used the code below but it is obvious that even though it will work 100% for the fist object you click on i.e. start at the first frame, it will jump to the next frame in the loop (where the previous object stopped) when you click on the next object.
Code: ags

int Sprite = 0;
function ChangeNumbers(int block){
Ã,  
Ã,  if(mouse.Mode == eModeInteract){
Ã,  object[block].SetView(2, 1, Sprite);
Ã,  Sprite++;
Ã,  }
Ã,  if(Sprite ==11){
Ã,  Ã,  Sprite = 0;
Ã,  }

}


I know it's something stupid but I just cannot figure it out.

Ashen

#1
You've got nothing to set it back after you run it (unless it's reached 11), to stop it advancing EVERY object's sprite.

I don't quite see what you're trying to achieve here.
Could adding Sprite = 0; to the start of the function help? That would (obviously) reset the value every time it was called, which might not be what you want (Sprite would never get above 1, so what would be the point?).

Alternatively, what if you make Sprite an array, with one entry for each possible object:

Code: ags

int Sprite[20]; // Max number of objects per room

function ChangeNumbers(int block){
 
  if(mouse.Mode == eModeInteract){
  object[block].SetView(2, 1, Sprite[block]);
  Sprite[block]++;
  }
  if(Sprite[block] == 11){
    Sprite[block] = 0;
  }

}


So, ChangeNumbers(0); will increase the frame for Object 0, but Objects 1 - 19 should remain untouched untill you call the function for them.

Like I said, not really sure what you're doing, so I don't know if this would work.

EDIT: Bah. SSH posted virtually the same code except he got the Object limit right, Damn him.
I know what you're thinking ... Don't think that.

Maverick

Thanks Ashen.

I knew that I would have to track it in an array but I always get confused with with them for some reason. I ended up with this code late last night but obviously I messed up:
Code: ags


#define NUMBER_OF_OBJECTS 10
int Sprite[NUMBER_OF_OBJECTS]; 
int Sprite // had to do this to save
function ChangeNumbers(int block){

Ã,  if(mouse.Mode == eModeInteract){
Ã,  Sprite++;//messed up (needs to increase on first click so the placement is correct)
Ã,  object[block].SetView(2, 1, Sprite[block]);
Ã,  
Ã,  }
Ã,  if(Sprite == 11){//messed up
Ã,  Ã,  Sprite = 0;//messed up
Ã,  }

}



I can read the damn stuff but I always screw it up when I have to do it. Its like being able to understand a foreign language but not being able speak it!

Anyway, your code is just what I needed thanks.

As for the application, I'm using it to spin the 3 dails on a panel (to unlock a slide on a gate)


Ashen

#3
So it's working now? That's the non-functioning code, which you've replaced with the working version or do you still need help getting that to work? (I guess it works, since you've marked this Solved and all...)

Given what you want to do you could maybe use the Object.Frame property and save yourself the trouble with arrays:
Code: ags

function ChangeNumbers(int block){
  if(mouse.Mode == eModeInteract){    
    if(object[block].Frame < 11) object[block].SetView(2, 1, object[block].Frame+1);
    else object[block].SetView(2, 1, 0);
  }
}


You might need to change the if(object[block].Frame < 11) condition, depending on how many frames there are in the loop. It should be OK for 10 frames, which makes sense if it's a numeric combination. Also, why do you need the if(mouse.Mode == eModeInteract) condition - why not just call the function from the 'Interact with Object' interaction?
I know what you're thinking ... Don't think that.

Maverick

Quote from: Ashen on Mon 11/12/2006 11:30:50
So it's working now? That's the non-functioning code, which you've replaced with the working version or do you still need help getting that to work? (I guess it works, since you've marked this Solved and all...)


It's working fine.
Thanks for the alternative code. It's a lot "cleaner".

SMF spam blocked by CleanTalk