Hi there!! I faced with my phaser program, I made some obstacles moving in tweens when the player stand in this obstacles only the obstacles are moving, I tried adding friction methods but it didn’t work. this is my code you can see my player config in // player config and obstacles config in // obstacles config.
class GameScene1 extends Phaser.Scene
{
constructor()
{
super({ key: 'GameScene1' });
}
preload()
{
this.load.spritesheet('player', './assets/player.png', { frameWidth: 100 , frameHeight: 100 })
this.load.image('ground', './assets/ground.png');
this.load.spritesheet('goal', './assets/goal.png', { frameWidth: 100, frameHeight: 100 })
}
create()
{
// controls config
gameState.mvSpeed = 400;
gameState.cursors = this.input.keyboard.createCursorKeys();
gameState.control = true
// player conig:
const player = this.physics.add.group({ gravity:700 });
gameState.player = player.create(50, config.height - 100, 'player');
gameState.playerAngle = Math.floor(gameState.player.angle)
gameState.player.setCollideWorldBounds(true);
gameState.player.setScale(.5)
gameState.player.depth = 1;
// player animation
this.anims.create({
key: 'playerFace',
frameRate: 12,
frames: this.anims.generateFrameNumbers('player', { Start: 0, end: 3 }),
yoyo: true,
repeat: -1
});
this.anims.create(
{
key: 'scaryFace',
frameRate: 7,
frames: this.anims.generateFrameNumbers('player', { start:4 , end:7 }),
yoyo: true,
repeat: -1
}
)
// goal config
const goals = this.physics.add.group
({
immovable: true,
allowGravity: false
});
gameState.goal = goals.create(100, 100, 'goal');
// goal animation and tweens
this.anims.create
(
{
key: 'goalAnimation',
frameRate: 12,
frames: this.anims.generateFrameNumbers('goal', { start:0 , end: 6}),
yoyo: true,
repeat:-1
}
)
gameState.goal.anims.play('goalAnimation', true);
this.tweens.add
({
targets: gameState.goal,
x: 500,
duration: 1800,
ease: Phaser.Math.Easing.Quartic.InOut,
callbackScope: this,
repeat: -1,
yoyo: true
})
// ground config
const platforms = this.physics.add.staticGroup();
gameState.ground = platforms.create(config.width/2, config.height, 'ground').setScale(config.width, 1).refreshBody();
// obstacle config
const obstacles = this.physics.add.group
({
allowGravity: false,
immovable: true
})
gameState.obs1 = obstacles.create(1100, 600, 'ground').setFrictionX(1).setScale(1.5, 1);
gameState.obs2 = obstacles.create(700, 500, 'ground').setFrictionX(1);
this.tweens.add
({
targets: gameState.obs2,
y: 100,
duration: 1000,
ease: Phaser.Math.Easing.Quartic.InOut,
callbackScope: this,
yoyo: true,
repeat: -1
})
// colliders and ovelaps
// collider of player and platforms
this.physics.add.collider(gameState.player, platforms, () => {
});
// overlap of player and goal
this.physics.add.overlap(gameState.player, gameState.goal, () =>
{
this.physics.pause();
gameState.control = false;
gameState.player.anims.pause();
gameState.timeLoop.destroy();
this.time.addEvent(
{
delay: 25,
callback: ()=>
{
gameState.player.alpha -= 0.05;
gameState.player.x += 5;
gameState.player.y += 1;
},
callbackScope: this,
loop: true
}
)
this.add.text(config.width/2 - 80, config.height/2, 'You win!',
{
fontSize:'50px' ,
fill:'0x000000' ,
})
this.input.on('pointerup', () =>
{
this.scene.restart();
this.anims.resumeAll();
gameState.counter = 0;
this.scene.stop('GameScene');
this.scene.start('GameScene1')
})
}
)
// collision(player, obstacles):
this.physics.add.collider(gameState.player, obstacles);
// screen config --------
gameState.level = 1;
gameState.counter = 0;
// level text
this.add.text(20, 10, 'Level ' + gameState.level,
{
fontSize: '30px', fill: '0x000000'
});
// timer text
gameState.timer = this.add.text(config.width - 200, 10, 'Time: ' + gameState.counter,
{
fontSize: '30px', fill: '0x000000'
});
gameState.setTimeText = function()
{
gameState.counter += 1;
gameState.timer.setText('Time: ' + gameState.counter);
}
if(gameState.counting = true)
{
gameState.timeLoop = this.time.addEvent(
{
delay: 1000,
callback: gameState.setTimeText,
callbackScope: this,
loop: true
}
);
}
}
update()
{
// mouvement with cursors
if(gameState.control)
{
if (gameState.cursors.up.isDown && gameState.player.body.touching.down)
{
gameState.player.anims.play('scaryFace', true);
gameState.player.setVelocityY(-gameState.mvSpeed*2);
gameState.player.rotation += 0.1;
}
if(gameState.cursors.left.isDown)
{
gameState.player.setVelocityX(-gameState.mvSpeed);
gameState.player.rotation -= 0.2
if (gameState.player.body.touching.down)
{
gameState.player.anims.play('playerFace', true);
}
else
{
gameState.player.anims.play('scaryFace', true);
}
}
else if (gameState.cursors.right.isDown)
{
gameState.player.setVelocityX(gameState.mvSpeed);
gameState.player.rotation += 0.2;
if(gameState.player.body.touching.down)
{
gameState.player.anims.play('playerFace', true);
}
else
{
gameState.player.anims.play('scaryFace', true);
}
}
else
{
gameState.player.setVelocityX(0);
if( Math.floor(gameState.player.angle) > 0 && Math.floor(gameState.player.angle) < 90 )
{
gameState.player.rotation += 0.1
}
else if (Math.floor(gameState.player.angle) > 90 && Math.floor(gameState.player.angle) < 178)
{
gameState.player.rotation += 0.1
}
else if (Math.floor(gameState.player.angle) > -90 && Math.floor(gameState.player.angle) < 0 )
{
gameState.player.rotation -= 0.1
}
else if (Math.floor(gameState.player.angle) > -178 && Math.floor(gameState.player.angle) < -90)
{
gameState.player.rotation -= 0.1
}
if(gameState.player.body.touching.down)
{
gameState.player.anims.play('playerFace', true);
}
else
{
gameState.player.anims.play('scaryFace', true);
}
}
};
}
}