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 ^^

1 Like

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)

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’;
}
})
}

1 Like

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)

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?