Jump to content

Javascript setTimout question


TheCrock
 Share

Recommended Posts

I am following the excellent book Foundation Game Design with HTML5 and Javascript.

I have a piece of code, which works but i'm not sure why.



function destroyAlien(alien)
{
//Change the alien's state and update the object
alien.state = alien.EXPLODED;

alien.update();

//Remove the alien after 1 second
setTimeout(removeAlien, 1000);

function removeAlien()
{
removeObject(alien, aliens);
removeObject(alien, sprites);
}


As you can see it is a function which uses setTimout to call another function. It only calls removeAlien function when it is inside the main function. If I move callAlien function out of the main function it does not work. I am puzzled as to why this is.

Can any sage member here tell me why this is?
Thanks in advance
Crock
TheCrock
 
Posts: 2
Joined: 12 Nov 2020, 16:02
  •  
 
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...