| Neuer User
Registriert seit: Jan 2002 Ort: LüdenscheiD
Beiträge: 148
| array Inhalt mit setInterval abarbeiten
Hi,
ich hänge grade beim Ablauf von setInterval fest, setInterval soll immer nach dem Ablaufen der Zeit neu ausgeführt werden solange sich Elemente in einem Array befinden.
Hab mir jetzt schon alles mögliche tracen lassen aber irgendwo hängt es noch, der fehler macht sich erst bemerkbar wenn ich um die 10 elemente in dem array habe (movieclips ruckeln).
Hier mal der Schnipsel der das Chaos verursacht. PHP-Code: btn.onPress = function() {
this.anz++;
nextArray.push(this.typ);
timeArray.push(this.dauer);
startTimer();
};
}
}
//------------------------------------------------- buildTimer ------------------------------------------------//
var ax:Number = 0;
var iid:Number;
function buildTimer() {
ax--;
trace(ax);
if (ax == 0) {
trace(interArray);
clearInterval(interArray[0]);
createUnitr(nextArray[0]);
nextArray.shift();
timeArray.shift();
interArray.shift();
trace(interArray[0]);
if (timeArray[0] == undefined) {
trace("abbruch");
} else {
startTimer();
}
}
}
function startTimer() {
if (timeArray.length>0 && ax != 0) {
trace("wait");
} else {
trace(timeArray);
ax = timeArray[0];
iid = setInterval(buildTimer, "startu", timeArray[0]);
interArray.push(iid);
}
}
Ich denke daß clearInterval nicht richtig aufgerufen wird oder daß > PHP-Code: interArray.push(iid);
nicht funktioniert.
Hier mal der gesamte Quellcode, copy/paste > anschauen > verstehen > kopfschütteln 
@60 fps PHP-Code: Stage.scaleMode = "noScale";
Stage.align = "TL";
Stage.showMenu = false;
_quality = "BEST";
this.createEmptyMovieClip("main", 0);
this.createEmptyMovieClip("menue", 1);
this.createEmptyMovieClip("list", 2);
this.createEmptyMovieClip("game", 3);
game.createEmptyMovieClip("map", 0);
game.createEmptyMovieClip("nodes", 1);
game.createEmptyMovieClip("army", 2);
var mouse:Object = new Object();
Mouse.addListener(mouse);
//------------------------------------------------- Textformatierung ------------------------------------------------//
var format:TextFormat = new TextFormat();
with (format) {
font = "Verdana";
color = 0x202020;
size = 12;
leftMargin = 5;
rightMargin = 5;
}
//------------------------------------------------- Movieclipfunktion ------------------------------------------------//
function createClip(clip:MovieClip, titel:String, x:Number, y:Number, w:Number, h:Number, d:Number, c:Number) {
clip.createEmptyMovieClip(titel, d);
with (clip[titel]) {
beginFill(c);
lineTo(0, h);
lineTo(w, h);
lineTo(w, 0);
endFill();
_x = x;
_y = y;
}
}
//------------------------------------------------- Textfieldfunktion ------------------------------------------------//
function createText(clip:MovieClip, titel:String, x:Number, y:Number, w:Number, h:Number, d:Number, t:String, txt) {
clip.createTextField(titel, d, x, y, w, h);
if (t == "dynamic") {
clip[titel].selectable = false;
} else {
clip[titel].selectable = true;
clip[titel].onKillFocus = function() {
if (clip[titel].text == "") {
clip[titel].text = txt;
}
};
clip[titel].onSetFocus = function() {
if (clip[titel].text == txt) {
clip[titel].text = "";
}
};
}
with (clip[titel]) {
type = t;
multiline = true;
wordWrap = true;
condenseWhite = true;
html = true;
maxChars = 30;
border = false;
background = false;
backgroundColor = 0xE5E5E5;
setNewTextFormat(format);
text = txt;
}
}
//------------------------------------------------- Arrays ------------------------------------------------//
var houseArray:Array = ["Base", "Baracks", "Garage", "Airport"];
var namesArray:Array = ["Soldier", "Hummer", "Tank", "Plane"];
var unitsArray:Array = ["soldier", "hummer", "tank", "plane"];
var troopArray:Array = [["soldier", .4, 2000, 0], ["hummer", .8, 3000, 0], ["tank", .6, 4000, 0], ["plane", 1, 5000, 0]];
var nextArray:Array = [];
var timeArray:Array = [];
var interArray:Array = [];
var colorArray:Array = ["0x00CCFF", "0x0099FF", "0x0066FF", "0x0033FF"];
//------------------------------------------------- createMenue ------------------------------------------------//
function createMenue() {
for (var i:Number = 0; i<namesArray.length; i++) {
createClip(menue, "btn_"+i, 0, i*25, 120, 20, i, 0xCCCCCC);
createText(menue["btn_"+i], "total", 0, 0, 30, 20, 0, "dynamic", 0);
createText(menue["btn_"+i], "txt", 30, 0, 60, 20, 1, "dynamic", namesArray[i]);
createText(menue["btn_"+i], "timer", 90, 0, 30, 20, 2, "dynamic", 0);
unitsArray[i].dauer = 0;
menue["btn_"+i].typ = troopArray[i][0];
menue["btn_"+i].dauer = troopArray[i][2];
menue["btn_"+i].anz = troopArray[i][3];
menue["btn_"+i].onPress = function() {
this.anz++;
nextArray.push(this.typ);
timeArray.push(this.dauer);
startTimer();
};
}
}
//------------------------------------------------- buildUnits+Timer ------------------------------------------------//
var ax:Number = 0;
var iid:Number;
function buildTimer() {
ax--;
trace(ax);
if (ax == 0) {
trace(interArray);
clearInterval(interArray[0]);
createUnit(nextArray[0]);
nextArray.shift();
timeArray.shift();
interArray.shift();
trace(interArray[0]);
if (timeArray[0] == undefined) {
trace("abbruch");
} else {
startTimer();
}
}
}
function startTimer() {
if (timeArray.length>0 && ax != 0) {
trace("wait");
} else {
trace(timeArray);
ax = timeArray[0];
iid = setInterval(buildTimer, "startu", timeArray[0]);
interArray.push(iid);
}
}
//------------------------------------------------- createNodes+Unit ------------------------------------------------//
function faerben(objekt:Object, farbwert:Number) {
var neueFarbe:Color = new Color(objekt);
neueFarbe.setRGB(farbwert);
}
var startNodes:Array = [[60, 120], [180, 120], [250, 120], [320, 120]];
var active:Boolean;
var anz:Number = 0;
function createUnit(unitType:String) {
anz++;
game.army.createEmptyMovieClip("unit_"+anz, anz);
game.nodes.createEmptyMovieClip("node_"+anz, anz);
createClip(game.nodes["node_"+anz], "gfx", -5, -5, 10, 10, 0, 0xCCCCCC);
createClip(game.army["unit_"+anz], "gfx", -10, -10, 20, 20, 1, 0x000000);
game.army["unit_"+anz].typ = unitType;
game.army["unit_"+anz].node = game.nodes["node_"+anz];
if (game.army["unit_"+anz].typ == troopArray[0][0]) {
faerben(game.army["unit_"+anz], colorArray[0]);
game.army["unit_"+anz].speed = troopArray[0][1];
game.army["unit_"+anz]._x = startNodes[0][0];
game.army["unit_"+anz]._y = startNodes[0][1];
}
if (game.army["unit_"+anz].typ == troopArray[1][0]) {
faerben(game.army["unit_"+anz], colorArray[1]);
game.army["unit_"+anz].speed = troopArray[1][1];
game.army["unit_"+anz]._x = startNodes[1][0];
game.army["unit_"+anz]._y = startNodes[1][1];
}
if (game.army["unit_"+anz].typ == troopArray[2][0]) {
faerben(game.army["unit_"+anz], colorArray[2]);
game.army["unit_"+anz].speed = troopArray[2][1];
game.army["unit_"+anz]._x = startNodes[2][0];
game.army["unit_"+anz]._y = startNodes[2][1];
}
if (game.army["unit_"+anz].typ == troopArray[3][0]) {
faerben(game.army["unit_"+anz], colorArray[3]);
game.army["unit_"+anz].speed = troopArray[3][1];
game.army["unit_"+anz]._x = startNodes[3][0];
game.army["unit_"+anz]._y = startNodes[3][1];
}
activateUnit(game.army["unit_"+anz]);
}
//------------------------------------------------- MoveToPosition ------------------------------------------------//
function activateUnit(unit:MovieClip) {
unit.onPress = function() {
active = true;
if (active) {
this.node._x = game._xmouse;
this.node._y = game._ymouse;
startDrag(this.node);
checkDistance();
}
};
unit.node.onPress = function() {
if (active) {
unit.mTo(unit.node, unit.speed);
stopDrag();
active = false;
}
};
}
//------------------------------------------------- move Funktion ------------------------------------------------//
MovieClip.prototype.mTo = function(a:MovieClip, speed:Number) {
this._rotation = Math.atan2(_xmouse-this._x, this._y-_ymouse)*180/Math.PI;
var xmove:Number = speed*Math.sin((this._rotation)*Math.PI/180);
var ymove:Number = speed*Math.cos((this._rotation)*Math.PI/180);
this.onEnterFrame = function() {
this._x += xmove;
this._y -= ymove;
if (this.hitTest(a)) {
delete this.onEnterFrame;
}
};
};
//------------------------------------------------- start ------------------------------------------------//
createMenue();
Bitte den restlichen code nicht kommentieren.
Schonmal Danke im Voraus für eure Hilfe.
Geändert von Gadon (31-05-2011 um 11:15 Uhr)
|