I am making a fighting game and based on the different things you choose (maps and charecters) the fight scene will be different. for example if (map == ‘swamp’) { this.add.image(400, 400, ‘swampTree’); }. So after one of the players wins, I send them to a screen saying which player won and then they can click a button that sends them back to the menu. Then unless they pick the same charecters and the same map, the fight scene wont work. I am very confused on why this is.
Can you show all the code in the fight scene?
What happens?
so the music plays but the screen still looks like it is on the scene right before the fight scene where you pick the map you want to play on and if you move your mouse or press a button nothing happens
I cant show you all the code in the fight scene because there are hundreds and hundreds of lines of code in it but I will show you the basic code.
class Fight extends Phaser.scene
{
constructor()
{
super("Fight")
}
preload()
{
//preload all stuff here
}
create()
{
if (map == 'swamp')
{
//create all the things needed in the swamp
}
if (map == 'puppetShow')
{
//create all the things needed in the puppetShow
}
//do the same thing for the eight other maps
//create players based on the charecter variable like so
if (charecter == 'frog')
{
this.player1 = this.physics.add.sprite(200, 200, 'frog');
}
//do the same thing for the five other charecters and for player 2
//code collisions in each map for each player
if (map == 'swamp')
{
this.physics.add.collider(this.swampTree, this.player1);
}
//do the same thing for the other nine maps
}
update()
{
//updating for all my maps like so
if (map == 'swamp')
{
//make the log go back and forth in the water
}
//do the same for the other nine maps
//now just make the players move and able to attack
}
}
I really hope this makes sense, if it doesent tell me and I will explain it better
there are also if statements in the preload section so you dont load stuff that you dont need for a particular map. ex: if (map == ‘palace’) { this.load.image(‘palace’, ‘palace.png’) }
console log gets very mad at me…
Uncaught TypeError: Cannot read properties of undefined (reading ‘velocity’)
at ArcadeSprite.setVelocityX (phaser.js:127881:19)
at Fight.update (script.html:2234:26)
at Systems.step (phaser.js:27164:26)
at SceneManager.update (phaser.js:46776:21)
at Game.step (phaser.js:109330:20)
at TimeStep.step (phaser.js:106091:14)
at step (phaser.js:66488:19)
this is saying that both the player1 and the player2 sprites are undefined, but I define them in the beggining of the code inside an if statement like so if (charecter == ‘frog’) { this.physics.add.sprite(100, 100, 'frog); }
what I need is for the game to just forget everything and restart. even if that means you have to go to the first scene (the first scene is the one before the menu so its okay)
I got it, I can just do location.reload and it reloads the game just like it would if you were to restart the browser tab, this makes it forget everything and restart from the beggining. this works great but I am wondering if it would work if I published my game to a gaming website?
The error is saying that body
is undefined, so reading body.velocity
is impossible.
You need to make sure this.player1
is assigned a new sprite each time create()
is called.
If it’s not, then this.player1
is the sprite from the previous scene cycle, which was destroyed when the scene was stopped, and its body
was removed (so undefined
).