Hey guys…
@ivan
I’m stuck when I try to animate the knight using idle. I will post my full code, but basically I only the get error message when I include the “true”
else{
knight.setVelocityX(0);
knight.play(“knight_idle”, true);
}
if I do the code without ‘true’, I get no error.
Not sure what’s wrong… Here is my full code:
<title>My First Blockchain Game</title>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
<h1>Blockchain Game</h1>
<script>
var cursors;
var knight;
var crates;
// configure the game(height, width, render-type, game)
var config = {
width:800,
height:500,
type:Phaser.AUTO,
scene:{
preload: gamePreload,
create: gameCreate,
update: gameUpdate
},
physics:{
default:"arcade",
arcade:{
gravity:{y:800},
debug:false
}
}
}
function gamePreload(){
// loading assets
console.log("game is loading assets");
this.load.image("knight","assets/knight.png");
this.load.image("crate","assets/crate.png");
this.load.image("background","assets/background.png");
// load the run animation frame
this.load.image("run_frame_1","assets/knight/run/Run (1).png");
this.load.image("run_frame_2","assets/knight/run/Run (2).png");
this.load.image("run_frame_3","assets/knight/run/Run (3).png");
this.load.image("run_frame_4","assets/knight/run/Run (4).png");
this.load.image("run_frame_5","assets/knight/run/Run (5).png");
this.load.image("run_frame_6","assets/knight/run/Run (6).png");
this.load.image("run_frame_7","assets/knight/run/Run (7).png");
this.load.image("run_frame_8","assets/knight/run/Run (8).png");
this.load.image("run_frame_9","assets/knight/run/Run (9).png");
this.load.image("run_frame_10","assets/knight/run/Run (10).png");
// load the idle animation frame
this.load.image("idle_frame_1","assets/knight/idle/Idle (1).png");
this.load.image("idle_frame_2","assets/knight/idle/Idle (2).png");
this.load.image("idle_frame_4","assets/knight/idle/Idle (4).png");
this.load.image("idle_frame_5","assets/knight/idle/Idle (5).png");
this.load.image("idle_frame_7","assets/knight/idle/Idle (7).png");
this.load.image("idle_frame_8","assets/knight/idle/Idle (8).png");
this.load.image("idle_frame_9","assets/knight/idle/Idle (9).png");
this.load.image("idle_frame_10","assets/knight/idle/Idle (10).png");
}
function gameCreate(){
// initial setup logical on the asset and others setups
console.log("game is setting up the assets etc");
// create background
this.add.image(300,320,"background");
// create the knight
knight = this.physics.add.sprite(200,100,"knight");
knight.body.setSize(170,610, 10, 0);
knight.scaleX = 0.15;
knight.scaleY = knight.scaleX;
// create the create
crates = this.physics.add.staticGroup();
// floor
crates.create(40,460,"crate");
crates.create(120,460,"crate");
crates.create(200,460,"crate");
crates.create(280,460,"crate");
crates.create(360,460,"crate");
crates.create(440,460,"crate");
crates.create(760,460,"crate");
// second floor
crates.create(440,340,"crate");
crates.create(520,260,"crate");
crates.create(250,260,"crate");
crates.create(150,220,"crate");
crates.create(750,170,"crate");
crates.create(600,340,"crate");
// create animations
this.anims.create({
key: "knight_run",
frames:[
{key: "run_frame_1"},
{key: "run_frame_2"},
{key: "run_frame_3"},
{key: "run_frame_4"},
{key: "run_frame_5"},
{key: "run_frame_6"},
{key: "run_frame_7"},
{key: "run_frame_8"},
{key: "run_frame_9"},
{key: "run_frame_10"},
],
frameRate: 10,
repeat: 1
});
this.anims.create({
key: "knight_idle",
frames:[
{key: "idle_frame_1"},
{key: "idle_frame_2"},
{key: "idle_frame_3"},
{key: "idle_frame_4"},
{key: "idle_frame_5"},
{key: "idle_frame_6"},
{key: "idle_frame_7"},
{key: "idle_frame_8"},
{key: "idle_frame_9"},
{key: "idle_frame_10"},
],
frameRate: 10,
repeat: 1
});
this.physics.add.collider(crates, knight);
cursors = this.input.keyboard.createCursorKeys();
}
function gameUpdate(){
// monitoring inputs and telling the game how to update
if(cursors.left.isDown){
knight.setVelocityX(-200);
knight.play("knight_run", true);
knight.flipX = true;
}
else if(cursors.right.isDown){
knight.setVelocityX(200);
knight.play("knight_run", true);
knight.flipX = false;
}
else{
knight.setVelocityX(0);
knight.play("knight_idle", true);
}
if(cursors.up.isDown && knight.body.touching.down){
knight.setVelocityY(-500);
}
}
var game = new Phaser.Game(config);
</script>