Jump to content

Frustrating issue with Sprite positioning at 'spawn'


FriarBabs
 Share

Recommended Posts

Alright, I have racked my brain so hard over the past couple of hours. Why does my sprite position itself at a new position every time it is spawned? I am passing a pair of literal coordinates to the constructor for this Player class inherited from Phaser.Sprite.

 

I have been looking at this issue too closely, and I think that I'm overlooking some really stupid mistake.

 

My impression is that this issue might have something to do with the sprite scaling that I apply, but if I disable all of the scaling on the sprite, the issue seems to still persist. Additionally, I have suspicion that it might have to do with the this.spawnX variables being incorrectly set. I'm a JavaScript noob, I apologize.

 

Here is the code for my FriarDude.Player class (sorry for the dump, but I honestly have no idea where in the code the issue lay):

FriarDude.Player = function (game, x, y) {	// Class variables	this.facing = 'idle';	this.cursors = null;	this.dead = false;	this.invincible = true;	this.lives = 3;	this.restLevel = 1;	this.toggleVisibilityTimerEvent = null;	// Clone starting position	this.spawnX = x;	this.spawnY = y;	//	this.RUN_VELOCITY = 300;	this.SPRITE_SCALE = 4;	this.INVINCIBILITY_DURATION = 1000;	//	// Input	this.cursors = game.input.keyboard.createCursorKeys();	//	// Constructor	Phaser.Sprite.call(this, game, x, y, 'sprite_sheet', 'foreground/friar/2_simple/idle/0000');	game.physics.arcade.enable(this);	this.body.bounce.y = .5;	this.body.setSize(24,32,0,0);	this.body.gravity.y = 300;	this.animations.add('idle', [	    'foreground/friar/2_simple/idle/0000',	    'foreground/friar/2_simple/idle/0001',	    'foreground/friar/2_simple/idle/0002'	], 4, true, false);	this.animations.add('run', [	    'foreground/friar/2_simple/run/0000',	    'foreground/friar/2_simple/run/0001'	], 8, true, false);	this.anchor.setTo(.5,.5);	this.scale.setTo(this.SPRITE_SCALE);	this.spawn();    return this;    //};FriarDude.Player.prototype = Object.create(Phaser.Sprite.prototype);FriarDude.Player.prototype.constructor = FriarDude.Player;FriarDude.Player.prototype.update = function() {	if (this.dead) return;	this.body.velocity.x = 0;	if (this.cursors.left.isDown) {        this.body.velocity.x = -this.RUN_VELOCITY;        if (this.facing != 'left') {            this.scale.x = -this.scale.x;            this.facing = 'left';            this.animations.play('run');        }    } else if (this.cursors.right.isDown) {        this.body.velocity.x = this.RUN_VELOCITY;        if (this.facing != 'right') {            this.scale.x = this.scale.y;            this.facing = 'right';            this.animations.play('run');        }    } else {        if (this.facing != 'idle') {        	this.animations.play('idle');            this.facing = 'idle';        }    }}FriarDude.Player.prototype.spawn = function() {	// Resetting properties that can be affected by dying	this.dead = false;	this.body.collideWorldBounds = true;	this.animations.play('idle');	//	// Reset velocity	this.body.velocity.x = 0;	this.body.velocity.y = 0;	//	// Reset position	this.x = this.spawnX;	this.y = this.spawnY;	//	// Temporary invincibility	this.invincible = true;    this.toggleVisibilityTimerEvent = this.game.time.events.loop(100, this.toggleVisibility, this);    this.game.time.events.add(this.INVINCIBILITY_DURATION, this.endInvincibility, this);    //}FriarDude.Player.prototype.kill = function() {    this.dead = true;    this.lives--;    this.body.collideWorldBounds = false;    this.animations.stop();    this.frameName = 'foreground/friar/2_simple/die';    // Launch the Player    this.body.velocity.setTo(this.game.rnd.realInRange(-150, 150), -150);    //}FriarDude.Player.prototype.endInvincibility = function() {    this.invincible = false;    this.game.time.events.remove(this.toggleVisibilityTimerEvent);}FriarDude.Player.prototype.toggleVisibility = function() {    if (this.alpha > 0) this.alpha = 0;    else this.alpha = 1;}
Link to comment
Share on other sites

Is it that you are setting the Sprite coords directly rather than the body coordinates? It's been a while since I coded so I can't remember what happens to the body when a Sprite is killed but, if I recall correctly, the body needs to be set eg:

this.body.reset(this.spawnX,this.spawnY);

I think this also resets the velocity to 0.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...