I'm having an issue with my JavaScript setup function and array of ghost objects called ghosts. When I try to call the setup function on the last ghost object in the array, I receive this error message: Uncaught TypeError: ghosts[(ghosts.length - 1)].setup is not a function
The currentWords variable contains an array of strings that is used within the setup function for creating a new Ghost at set intervals.
var Ghost=function()
{
this.ghostPic='<img src="ghost.png">';
this.x=Math.floor(Math.random()*500);
this.y=Math.floor(Math.random()*500);
this.speed=5;
this.width=230;
this.height=180;
this.firstH=2000;
this.dead = false;
this.word = currentWords[Math.floor(Math.random()*currentWords.length)];
}
Ghost.prototype.setup=function()
{
this.ghostElement=$(this.ghostPic);
console.log('1');
this.ghostElement.css({
height:this.height-=2000,
width:this.width-=2000,
position:"absolute",
zIndex:-1,
left:this.x,
top:this.y
});
console.log("2");
$('body').append(this.ghostElement);
}
To initialize the setup function, I am using the following code:
ghosts.concat(new Ghost());
ghosts[ghosts.length-1].setup();
If anyone has any insights or solutions to this issue, I would greatly appreciate it. Thank you!