Trying to map obstacles using a single object.
Originally scattered randomly across the map, now I want to hard code X & Y coordinates with an array of numbers. However, TypeScript is only using the last value of the loop for the X coordinate. How can I assign specific numbers to each obstacle?
Randomly Generate
module objects {
export class Obstacles extends objects.GameObject {
public Start():void {
this.x = Math.floor(Math.random() * (640) - this.width) + this.halfWidth;
this.y = Math.floor(Math.random() * (480) - this.width) + this.halfWidth;
console.log("x " + this.x);
console.log("y " + this.y);
}
}
https://i.sstatic.net/5EBdA.jpg
Specify X Value With Array
private _obstX: number [] = [200, 250, 300, 350, 400];
public Start():void {
for (let count = 0; count < this._obstX.length; count++) {
this.x = this._obstX[count];
}
this.y = Math.floor(Math.random() * (480) - this.width) + this.halfWidth;
console.log("x " + this.x);
console.log("y " + this.y);
}
https://i.sstatic.net/zrD22.jpg
Only one x value is being assigned, while the other x values in the array are ignored.
Code in Main File
private _obstPlanes: objects.Obstacles[];
private _obstNum: number;
public Start(): void {
this._obstPlanes = new Array<objects.Obstacles>();
this._obstNum = 5;
for (let count = 0; count < this._obstNum; count++) {
this._obstPlanes[count] = new objects.Obstacles(this.assetManager);
}
}
public Main(): void {
this._obstPlanes.forEach(obstPlane => {
this.addChild(obstPlane);
});
}