FAQ: Learn Phaser: Basics - Input

This community-built FAQ covers the “Input” exercise from the lesson “Learn Phaser: Basics”.

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

Learn Phaser

FAQs on the exercise Input

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!

Hello can I please have the hint code for lesson 9 question 3? There isn’t any and I’m stuck

With this, you want to assign rect1 as interactive and fill in the color values as the default, so as you click rect2 you’ll get a switch and clicking rect1 will get you back to the normal colors if that makes sense.
You don’t have to change anything in the scene.

function create() {
gameState.rect1 = this.add.rectangle(200, 100, 100, 100, gameState.onColor);
gameState.rect2 = this.add.rectangle(200, 300, 100, 100, gameState.offColor);

// add the switchedOn state here
gameState.switchedOn = ‘rect1’;

// set gameState.rect2 as interactive here
gameState.rect2.setInteractive();

// create the pointerup listener for rect2 here
gameState.rect2.on(‘pointerup’, function() {
gameState.rect1.fillColor = gameState.offColor;
gameState.rect2.fillColor = gameState.onColor;
})
gameState.rect1.setInteractive();

gameState.rect1.on(‘pointerup’, function(){
** gameState.rect1.fillColor = gameState.onColor;**
** gameState.rect2.fillColor = gameState.offColor;**
** })**
}

1 Like

Hi uniq188, thx for your answer…but somehow, when I copy your code the preview stays white…same happened with my code ( I just didn’t have the switchedOn-state because it wasn’t really explained what this does in the task).

I have seen no displays while doing ANY of the Phaser lessons. It sounds like mikewee is experiencing the same thing. the right hand window, labeled 'http://localhost:8000/,’ gives no display AT ALL. As I said in an earlier post, it’s hard to learn this if you don’t show the results of our work.

In this lesson it again states specifically ‘observe the two colors switch’. We can’t do that!

I’ve gone to the Phaser.io website and gone through their tutorials. It was the only way I could come back to codecademy and actually progress through the lesson, since I get NO VISUAL FEEDBACK on what I’m doing.

You really need to get this working before the course has any real value at all. It seems you’ve released the lesson before putting hints in place, verifying display, etc. Can’t give you any kudos on this lesson so far . . .

Well TJ “Henry” Yoshi ^^

Can someone please inform me where you’re getting the switchedOn function? There’s no explanation whatsoever about why this was included, and it’s not in the phaser docs.

2 Likes

I have the same issue as jford1485

The switchedOn “state” doesn’t come with any instructions or explanation. It is also referred to as a “state” which according to the Phaser discourse group has been replaced by the “scenes” system.

I ran the code without including it and passed to the next level. Is this just an attempt to make us dig into the docs or a mistake? Its confusing.

  • when including gameState.switchedOn = ‘rect1’; and running the code it removes the quotes from rect1…

I ran into a similar issue as the others, just copied all the code to my clipboard, reset the exercise, and it seemed to work fine after that. Had to do a few times, likely from leaving the page open and idle for an extended amount of time.

Anyhow, could anyone explain what the gameState.switchedOn does, or what it should do?
My code seems to work the same whether or not I comment out line 9 where I have the gameState.switchedOn =‘rect1’.

Here is my code:
const gameState = {
onColor: 0xaaffaa,
offColor: 0xffaaaa
}

function create() {
gameState.rect1 = this.add.rectangle(200, 100, 100, 100, gameState.onColor);
gameState.rect2 = this.add.rectangle(200, 300, 100, 100, gameState.offColor);
gameState.switchedOn =‘rect1’;
gameState.rect2.setInteractive();
gameState.rect1.setInteractive();
gameState.rect2.on(‘pointerup’,
function(){
gameState.rect1.fillColor = gameState.offColor;
gameState.rect2.fillColor = gameState.onColor;
});

gameState.rect1.setInteractive();

gameState.rect1.on(‘pointerup’,function(){
gameState.rect1.fillColor = gameState.onColor;
gameState.rect2.fillColor = gameState.offColor;

})

// add the switchedOn state here

// set gameState.rect2 as interactive here

// create the pointerup listener for rect2 here

}

const config = {
type: Phaser.AUTO,
width: 400,
height: 450,
backgroundColor: 0x333333,
scene: {
create
}
}

const game = new Phaser.Game(config)

1 Like

Leaving this reply here for anyone confused about the “switched on state”,

gameState.switchedOn = ‘rect1’;

The instructions don’t mention it at all. The comment referring to it as a ‘state’ is not using the term ‘state’ in the same context as a “Game State”. The line of code above adds a new property to our gameState object called “switchedOn”.

The switchedOn property holds a string value corresponding to the name of the rectangle whose color is currently set to “onColor”, one of the two color properties defined in the gameState object (green in the case of this exercise).

The purpose of including this property is to nudge you in the right direction for part 4 of the exercise --adding additional code to allow the player to interact with rect1 so that they can switch the colors back and forth between the rectangles.

To do so, you’ll use the switchedOn property in a conditional (if) statement in the function call of the ‘pointerup’ event listener in the ‘create’ function for each rectangle, as follows:

function create() {
gameState.rect1 = this.add.rectangle(200, 100, 100, 100, gameState.onColor);
gameState.rect2 = this.add.rectangle(200, 300, 100, 100, gameState.offColor);

// Add switchedOn property, default to rect1
gameState.switchedOn = ‘rect1’;

// Set both rectangles as interactive
gameState.rect1.setInteractive();
gameState.rect2.setInteractive();

// pointerup listener for rect1
gameState.rect1.on(‘pointerup’, function() {
if (gameState.switchedOn === ‘rect2’)
{
gameState.rect2.fillColor = gameState.offColor;
gameState.rect1.fillColor = gameState.onColor;
gameState.switchedOn = ‘rect1’
}
})

// pointerup listener for rect2
gameState.rect2.on(‘pointerup’, function() {
if (gameState.switchedOn === ‘rect1’)
{
gameState.rect1.fillColor = gameState.offColor;
gameState.rect2.fillColor = gameState.onColor;
gameState.switchedOn = ‘rect2’;
}
})
}

4 Likes

Hi All,

Thanks for sharing your thoughts, I got the code working.

blayneayersman700998 - Your explanation helped me alot. But I modified the code a little so that it allows me to switch regardless of which rectangle I choose first.

const gameState = {
onColor: 0xaaffaa,
offColor: 0xffaaaa
}

function create() {
gameState.rect1 = this.add.rectangle(200, 100, 100, 100, gameState.onColor);
gameState.rect2 = this.add.rectangle(200, 300, 100, 100, gameState.offColor);

// add the switchedOn state here
gameState.switchedOn = ‘rect1’;
// set gameState.rect2 as interactive here
gameState.rect1.setInteractive();
gameState.rect2.setInteractive();
// create the pointerup listener for rect1 here
gameState.rect1.on(‘pointerup’, function()
{
if (gameState.switchedOn === ‘rect1’){
gameState.rect1.fillColor = gameState.offColor;
gameState.rect2.fillColor = gameState.onColor;
gameState.switchedOn = “rect2”;
}
})
// create the pointerup listener for rect2 here
gameState.rect2.on(‘pointerup’, function()
{
if (gameState.switchedOn === ‘rect2’){
gameState.rect2.fillColor = gameState.offColor;
gameState.rect1.fillColor = gameState.onColor;
gameState.switchedOn = “rect1”;
}
})
}

const config = {
type: Phaser.AUTO,
width: 400,
height: 450,
backgroundColor: 0x333333,
scene: {
create
}
}

const game = new Phaser.Game(config)

1 Like

Isn’t the switchedOn state a little unnecessary? I just added a separate pointerup for rect1 that did the opposite of what rect2’s did.

I suppose the inclusion of it is for optimization purposes, which may be increasingly important for larger and larger games, though for this specific case I don’t think it matters.

what’s the purpose adding the gameState.switchedOn?

This is my code, and it works if I click on rect1 and then rect2, but I can’t click on rect2 twice and have it change colors. What am I doing wrong?

const gameState = {
  onColor: 0xaaffaa,
  offColor: 0xffaaaa
}

function create() {
  gameState.rect1 = this.add.rectangle(200, 100, 100, 100, gameState.onColor);
  gameState.rect2 = this.add.rectangle(200, 300, 100, 100, gameState.offColor);
  
  // add the switchedOn state here
  //gameState.switchedOn = 'rect1';
  // set gameState.rect2 as interactive here
  gameState.rect2.setInteractive();
  // create the pointerup listener for rect2 here
  gameState.rect2.on('pointerup', function() {
    gameState.rect1.fillColor = gameState.offColor;
    gameState.rect2.fillColor = gameState.onColor;
    gameState.switchedOn = 'rect1';
  })
  gameState.rect1.setInteractive();
  gameState.rect1.on('pointerup', function() {
    gameState.rect1.fillColor = gameState.onColor;
    gameState.rect2.fillColor = gameState.offColor;
  })
}

const config = {
  type: Phaser.AUTO,
  width: 400,
  height: 450,
  backgroundColor: 0x333333,
  scene: {
    create
  }
}

const game = new Phaser.Game(config)

I even tried adding:

 if (gameState.switchedOn) {
      gameState.rect1.on('pointerup', function() {
        gameState.rect1.fillColor = gameState.offColor;
        gameState.rect2.fillColor = gameState.onColor;
}

@ netmaster15635 Whenever I run your code I get a blank white viewer.

i need help on game development workspace nine nine please

Guys switchedOn is just a random name for a variable or a property to create in order to store the state of colors (they have been switched, they have not been switched). Look this code as an example, I commented the different blocks for better understanding:

//define what to create
function create() {

//create two rectangles
  gameState.rect1 = this.add.rectangle(200, 100, 100, 100, gameState.onColor);
  gameState.rect2 = this.add.rectangle(200, 300, 100, 100, gameState.offColor);

  // I initialize my switched state as a gamestate property, I set it to false because at creation phase the rectangles cannot have been switched yet
  gameState.switched = false;
  /* You can name the switched state whatever you like, 
     you could even create a variable "let switched = anything" and manipulate this 
     instead of creating a new gameState property as I did */

  // set rect1 and rect2 interactive
  gameState.rect1.setInteractive();
  gameState.rect2.setInteractive();
  
  // listen for pointerup on rect1
  gameState.rect1.on("pointerup",
    function(){
      if(gameState.switched===true){ //if the rectangles colors have been switched then switched them back
        gameState.rect1.fillColor = gameState.onColor;
        gameState.rect2.fillColor = gameState.offColor;
        gameState.switched = false;  //set back to initial switched state
      }
    }
  );

  // listen for pointerup on rect2
  gameState.rect2.on("pointerup",
    function(){
      if(gameState.switched===false){ //if the rectangles colors have not been switched then switched them 
        gameState.rect1.fillColor = gameState.offColor;
        gameState.rect2.fillColor = gameState.onColor;
        gameState.switched = true;    //set to a new switched state
      }
    }
  )
};

Once I add the code to make the colors switch back:

gameState.switchedOn = 'rect1';

  // set gameState.rect2 as interactive here

  gameState.rect2.setInteractive();

  // create the pointerup listener for rect2 here

  gameState.rect2.on('pointerup', function() {

    if (gameState.switchedOn = 'rect1') {

    gameState.rect1.fillColor = gameState.offColor;

    gameState.rect2.fillColor = gameState.onColor;

    gameState.switchedOn = 'rect2';

    }

  })

  gameState.rect1.setInteractive();

  gameState.rect1.on('pointer' function() {

    if (gameState.switchedOn = 'rect2') {

      gameState.rect1.fillColor = gameState.onColor;

      gameState.rect2.fillColor = gameState.offColor;

      gameState.switchedOn = 'rect1';

    }

  }) 

};

The whole game disappears, and I don’t know why. However the the checkmark for question 4 still goes on anyway. When I delete the added code, the page comes back, but I cant switch the colors anymore. Am I doing something wrong or is this a bug?

There are a few issues which may or may not be the cause of the behavior you are seeing.
You have
gameState.rect2.on('pointerup', function() {
in one place and then in the other you have
gameState.rect1.on('pointer' function() {

Secondly your if conditions aren’t checking for equality. You have used the assignment operator = instead of the equality operator ===

1 Like

Oh yeah this did it, I fixed the rect 1 event handler and the if statement and it came back. Too bad that instead of an error message the page will just vanish and not tell you why. Thanks for the help!

1 Like