Hi,
I’ve used this Phaser example and added an immovable collide sprite:
Stop at position
This works fine normally, but if I add a tween scale the sprite goes through the collider sprite.
Is there a way to stop this?
Reading many posts on the subject it’s something to do with the velocity I think?
Any help on this would be great. Thanks!
Code below of the mainscene:
import PhaserLogo from '../objects/phaserLogo'
var target = new Phaser.Math.Vector2();
var debug;
var maincharacter;
var distanceText;
var yText;
var obstacle;
var obstacleLeft;
export default class MainScene extends Phaser.Scene {
constructor() {
super({ key: 'MainScene' })
}
create() {
maincharacter = this.physics.add.sprite(900, this.cameras.main.width / 2 - 20, 'phaser-logo');
obstacleLeft = this.physics.add.sprite(750, 0, 'obstacle').setImmovable();
obstacleLeft.setOrigin(0,0);
obstacleLeft.displayHeight = this.cameras.main.height;
obstacleLeft.body.immovable = true;
obstacleLeft.body.moves = false;
obstacleLeft.body.allowGravity = false;
debug = this.add.graphics();
this.input.on('pointerdown', function (pointer) {
target.x = pointer.x;
target.y = pointer.y;
//move the character to an xy point
this.physics.moveToObject(maincharacter, target, 400);
//if Y is less than 400 – scale character down
if (target.y < 400) {
maincharacter.scene.tweens.add({
targets: maincharacter.body.velocity ,
ease: 'Linear',
duration: 500,
onComplete: function (tween, targets)
{
maincharacter.scene.tweens.add({
scale: .5,
targets: maincharacter,
ease: 'Linear',
duration: 500,
});
},
});
}
//if Y is more than 400 – scale character back to 1
if (target.y > 400) {
maincharacter.scene.tweens.add({
targets: maincharacter.body.velocity ,
ease: 'Linear',
duration: 500,
onComplete: function (tween, targets)
{
maincharacter.scene.tweens.add({
scale: 1,
targets: maincharacter,
ease: 'Linear',
duration: 500,
});
},
});
}
}, this);
//create collider with obstacleLeft
this.physics.add.collider(maincharacter, obstacleLeft, function (maincharacter, obstacle) {
maincharacter.setVelocity(0);
maincharacter.body.stop();
});
distanceText = this.add.text(10, 10, 'X Position:', { fill: '#ff004e' });
yText = this.add.text(10, 30, 'Y Position:', { fill: '#ff004e' });
}
update() {
var distance = Phaser.Math.Distance.Between(maincharacter.x, maincharacter.y, target.x, target.y);
if (maincharacter.body.speed > 0)
{
distanceText.setText('X Position: ' + target.x);
yText.setText('Y Position: ' + target.y);
// 4 is our distance tolerance, i.e. how close the source can get to the target
// before it is considered as being there. The faster it moves, the more tolerance is required.
if (distance < 4)
{
maincharacter.body.reset(target.x, target.y);
}
}
}
}