## [ascoders] tilebasiertes game, rekursives wirrwar

• From: <mail@xxxxxxxxxxxx>
• To: <ascoders@xxxxxxxxxxxxx>
• Date: Sat, 23 Oct 2004 21:39:17 +0100

```Hallo list,

ich hab nen kleines problem mit nem rekursiven funktionsaufruf innerhalb
eines anderen funktionsaufrufes innerhalb einer klasse.

Prinzipiell rufe ich eine funktion auf, welche eine andere aufruft, die
rekursiv versucht ein freies tile in einem tilebasierten spiel zu finden.

Das sieht dann im quelltext so aus,

die funktion " searchForFreeTiles" wird vorher wo aufgerufen....

*code*

public function searchForFreeTiles(xTileTo:Number, xTileFrom:Number,
yTileTo:Number, yTileFrom:Number, aktDistanceToGoal:Number,
map:Array):Object
{
var tile = this.findNearestFreeTileToOccupiedOne(xTileTo,
xTileFrom, yTileTo, yTileFrom, map);
trace("return2: "+[tile.x, tile.y, tile.distance]);
return tile;
}

public function findNearestFreeTileToOccupiedOne(xTileTo:Number,
xTileFrom:Number, yTileTo:Number, yTileFrom:Number, map:Array):Object
{
var p1 = {};
var p2 = {};
var p3 = {};
var p4 = {};
var p5 = {};
var p6 = {};
var p7 = {};
var p8 = {};

var surroundingTiles = [];

//upper left
p1.x   = xTileTo-1;
p1.y   = yTileTo-1;
p1.distance = this.calculateEuclideanDistance(xTileFrom, p1.x,
yTileFrom, p1.y);
surroundingTiles.push(p1);

//up
p2.x   = xTileTo;
p2.y   = yTileTo-1;
p2.distance = this.calculateEuclideanDistance(xTileFrom, p2.x,
yTileFrom, p2.y);
surroundingTiles.push(p2);

//upper right
p3.x   = xTileTo+1;
p3.y   = yTileTo-1;
p3.distance = this.calculateEuclideanDistance(xTileFrom, p3.x,
yTileFrom, p3.y);
surroundingTiles.push(p3);

//left
p4.x   = xTileTo-1;
p4.y   = yTileTo;
p4.distance = this.calculateEuclideanDistance(xTileFrom, p4.x,
yTileFrom, p4.y);
surroundingTiles.push(p4);

//right
p5.x   = xTileTo+1;
p5.y   = yTileTo;
p5.distance = this.calculateEuclideanDistance(xTileFrom, p5.x,
yTileFrom, p5.y);
surroundingTiles.push(p5);

//upper left
p6.x   = xTileTo-1;
p6.y   = yTileTo+1;
p6.distance = this.calculateEuclideanDistance(xTileFrom, p6.x,
yTileFrom, p6.y);
surroundingTiles.push(p6);

//down
p7.x   = xTileTo;
p7.y   = yTileTo+1;
p7.distance = this.calculateEuclideanDistance(xTileFrom, p7.x,
yTileFrom, p7.y);
surroundingTiles.push(p7);

//down right
p8.x   = xTileTo+1;
p8.y   = yTileTo+1;
p8.distance = this.calculateEuclideanDistance(xTileFrom, p8.x,
yTileFrom, p8.y);
surroundingTiles.push(p8);

//sort on distance
surroundingTiles.sortOn( "distance", 16 );
//if the tile is occupied, search for a new one...
if(map[surroundingTiles[0].y][surroundingTiles[0].x][0])
{
this.findNearestFreeTileToOccupiedOne(surroundingTiles[0].x,
xTileFrom, surroundingTiles[0].y, yTileFrom, map);
}
else
{
var tile = surroundingTiles[0];
trace("return1: "+[tile.x, tile.y, tile.distance]);
return tile;
}
}

*code*

Interesant ist der trace, der folgendes ausspuckt:
"
return1: 24,4,20
return2: undefined,undefined,undefined
"

Irgendwie scheint mir der rückgabewert der function "
findNearestFreeTileToOccupiedOne" verloren zu gehen.

Es muss was ganz einfaches sein, ich rall es warscheinlich grad nur nicht
:-)

Jeder input wär willkommen...

Danke!

benjamin

------------------------------------------------------
Archiv   : //www.freelists.org/archives/ascoders/
Optionen : //www.freelists.org/list/ascoders
------------------------------------------------------
```