FAQ: Learn Phaser: Physics - Adding Enemies

This community-built FAQ covers the “Adding Enemies” exercise from the lesson “Learn Phaser: Physics”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Phaser

FAQs on the exercise Adding Enemies

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why doesn’t function declaration work in this exercise?
AND
Is something remembered when I use a function declaration instead of arrow function syntax?

what i first did was:

  const bugs = this.physics.add.group();
  function bugGen()  {
    const xCoord = Math.random() * 450;
    bugs.create(xCoord, 10, 'bug1');
  }

  bugGen();
  bugGen();

but i didn’t get past instruction 5 even though there where bugs falling down the screen.

then i copied the solution and reseted the exercise. first writing the same solution as before and when again instruction 5 wasn’t accepted, I pasted the solution (which uses arrow function syntax) but instruction 5 still wasn’t accepted.
Then when I reloaded the page and only used the arrow function syntax instruction 5 was accepted?!?

I used the function syntax as in the instructions (not the arrow syntax). I also got stuck on step 5 till I made the edit as below.
bugGen(); wasn’t accepted.
bugGen() was accepted.

Hi! i tried it again but now it works fine. i can’t get the same error as back then.

I have a question.

  const bugs = this.physics.add.group();
  function bugGen()  {
    const xCoord = Math.random() * 450;
    bugs.create(xCoord, 10, 'bug1');
  }

  bugGen();
  bugGen();

Why did this code makes 2 bugs?I think this 2nd “bugGen()” update 1st “bugGen()”'s Product.
I predicted that this code will make one production.
I think I missing knowledge about javascript.
Can someone please tell me what knowledge I am lacking?
(And sorry for my bad Endlish.)

 function bugGen() {
 // ....
}

This is the function definition. It lays out what the function is supposed to do, but it doesn’t execute any statement. It is like writing a recipe in a cookbook.

When we write the statement
bugGen();
now, the function is called and the statements we wrote in the function definition are executed. You can think of it as cooking the recipe we wrote earlier.
A random xCoord is generated and and then a bug is created.
When we again write the statement
bugGen();
again the function is called and the statements we wrote earlier are again executed in sequence. Another random xCoord is generated and a new bug is created. There is nothing in the function definition to suggest that we are targeting the same bug for updating.