Ok, I've come across a weird bug with the Rewind function in my game and I don't quite get how it's going wrong.
Anyway, the problem is that when I have more than one enemy and then try to Rewind them, they all kind of join together position-wise and then start switching between each others' positions in their Rewind data. Basically, for each of them their data for where they have been is getting screwed up somehow. By outputting the Y position stored in the first part of the array at different intervals, I've pinpointed the code that's the problem, although I was pretty sure it was that anyway.
Here's the code of what I do to all the enemies each loop:
//here we adjust the enemies- all the AI stuff + scroll and check for hit with player
for (g = 0; g < 100; g++) {
if (enemy[g].used) {
enemy[g].coll = false;
if (tim_body.hitTest(enemy[g].sp)) {
if (hurt == 0) {
health -= 5;
jumping = true;
dir = enemy[g].dir;
yVel = 10;
xVel = 15 * dir;
hurt = 50;
}
coll = true;
enemy[g].coll = true;
}
if (tRewind > 500) {
enemy[g].Rewind(tRewind);
}
if (tPause < 250) {
enemy[g].walk(lvl);
if (tRewind < 500) {
enemy[g].memorise(g);
//trace("Post-Memory" + g + ":" + enemy[0].rY[249]);
}
}
enemy[g].sp._x += XOff;
enemy[g].sp._y += YOff;
enemy[g].XOff += XOff;
enemy[g].YOff += YOff;
}
}
And here's the Enemy class which has the function that's the problem, memorise():
class Enemies {
public var sp:MovieClip, used:Boolean, eType:Number, XOff:Number = 0, YOff:Number = 0;
public var j:Number = 0;
public var coll:Boolean = false;
private var rX:Array = new Array(250), rY:Array = new Array(250), rF:Array = new Array(250);
private var rD:Array = new Array(250);
private var dir:Number = -1;
public function Enemies(eUsed, eSp, eeType) {
this.used = eUsed;
this.sp = eSp;
this.eType = eeType;
}
public function walk(lvl) {
if (lvl[int((this.sp._y - this.YOff) / 50)][int((this.sp._x + (30 * this.dir) - this.XOff) / 50)] < 9 &&
lvl[int((this.sp._y - this.YOff + this.sp._height) / 50)][int((this.sp._x + (30 * this.dir) - this.XOff) / 50)] >= 9) {
if (!this.coll) {
this.sp._x += 3 * this. dir;
}
}else {
this.dir = -this.dir;
}
this.sp._xscale = 100 * this.dir;
}
public function memorise() {
for (j = 0; j < 249; j++) {
this.rX[j] = this.rX[j + 1];
this.rY[j] = this.rY[j + 1];
this.rF[j] = this.rF[j + 1];
this.rD[j] = this.rD[j + 1];
}
this.rX[249] = this.sp._x - this.XOff;
this.rY[249] = this.sp._y - this.YOff;
this.rF[249] = this.sp._currentframe;
this.rD[249] = this.dir;
}
public function Rewind(tRewind) {
this.sp._x = this.rX[tRewind - 501] + this.XOff;
this.sp._y = this.rY[tRewind - 501] + this.YOff;
this.sp.gotoAndStop(rF[tRewind - 501]);
this.dir = this.rD[tRewind - 501];
}
}
From what I was outputting with the trace right after running the enemy.memorise() function, I found that the same variable of one of the enemies was coming out different after running the memorise() function of the OTHER enemy. However, since I'm using "this" with all my references (except j), I don't see how this is happening.
I also outputted all of the previous Y positions of both enemies at one point, and got this:
0:
252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352
1:
252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352,252,352
You'll notice that both of the arrays are exactly the same (although considering the Rewind results I probably should have guessed that). It's almost as though the rY, rX and so on arrays are being static, but I didn't declare them that way so I don't see why they would be.
Anyway, any help on this would be appreciated.