Jump to content

Enable Sprites Body At Specific Animation Help


Recommended Posts

Hi, as the title suggests I am attempting to enable the body of a sprite at a specific animation frame in order to prevent the player from passing. I have attempted many ways after reading documentation and have not be able to find a solution. The code is written in OOP and the classes are separated. 

I've used collision function to no avail, apparently I cannot access .sprite. I must be missing something or the functionality is not there.

The sprite, which will be many as it is part of a group, contains a method called checkHit & addCollision both of these methods retrieve the frame by using this.frame and if it is two the add collision method enables the body and the checkHit method is used as a getter as I would like there only to be a collision when the sprite is on animation frame two.


Attached are three classes referenced I am hoping someone will be able to see my mistake as each example I have follow in the examples phaser section has not worked. 
Thank you.


Trap = function (game, x, y) {
	Phaser.Sprite.call(this, game, x, y, 'spikes');
	game.physics.arcade.enable(this, Phaser.Physics.ARCADE);
	this.anchor.setTo(0.5, 0.5);	
	this.animations.add('open', [1,0,2,0], 1, true);
	this.body.allowGravity = false;
	this.body.enable = true;
Trap.prototype = Object.create(Phaser.Sprite.prototype);
Trap.prototype.constructor = Trap;
Trap.prototype.checkHit = function(){
	if(this.frame == 2){	
		return true;
	return false;
Trap.prototype.addCollision = function(){
	if(this.frame == 2){	
		this.body.enable = true;
	} else {
		this.body.enable = false;

Trap.prototype.update = function(){


myGame.MainGameScreen = function(game){ 
var mummys;
var mummysTotal;
var mummysAlive;

var gameHeight;
var gameWidth;


myGame.MainGameScreen.prototype = {
	create: function(){
		this.game = this;
		this.physics.arcade.gravity.y = 300;	
		this.world.setBounds(0, 0, 5000, 2500);
		this.map = this.add.tilemap('level');
		this.map.addTilesetImage('worldsheet', 'tiles');
		this.layer = this.map.createLayer('Tile Layer 1');	

		this.trap = this.game.add.physicsGroup();
		//Retrieves Object Layer From TileMap. Creates Instance Of Extended Phaser Sprite Call -> Trap Class
		this.map.createFromObjects('Object Layer 1', 13, 'spikes', 0, true, false, this.trap, Trap);
		//this.trap.forEach(this.setupObject, this);		
		//this.trap.callAll('animations.add', 'animations', 'animate', [1,0,2,0], 2, true);		
		//this.trap.callAll('animations.play', 'animations', 'animate');
		//this.test = <ObjectEntity>this.map.objects["Object Layer 1"][0];
		//this.map.createFromObjects('Object Layer 1', 1, 'Mummy', 0, true, false, Mummy);
		//this.map.createFromObjects('Object Layer 1', 13, 'spikes', 0, true, false, this.trap, Trap(this, 70, 2100));
		this.character = new Character(0, this, this.layer, this.trap);				
	setupObject: function(object){
		if(object.key == "spikes"){
			//Trap(this, object.world.x, object.world.y);
			new Trap(this.game, object.world.x, object.world.y);
	}, */
	update: function(){	
			if(trap.checkHit() == true){
					console.log(trap + ' ' + this.character);
		if(game.physics.arcade.collide(this.character, this.trap)){


Character = function (index, game, layer, trap){
		this.jumpTimer = 0;
		this.game = game;
		this.layer = layer;
		this.trap = trap;

		this.sprite = game.add.sprite(70, 2100, 'character');
        this.game.physics.arcade.enable(this.sprite, Phaser.Physics.ARCADE);
		this.sprite.body.collideWorldBounds = true;
		this.sprite.animations.add('idle', Phaser.Animation.generateFrameNames('Idle', 1, 10, '', 2), 10, true);
		this.sprite.animations.add('run', Phaser.Animation.generateFrameNames('Run', 1, 8, '', 2), 8, true);
		this.sprite.anchor.setTo(0.5, 0.5);
		this.leftKey = this.game.input.keyboard.addKey(Phaser.Keyboard.A);
		this.rightKey = this.game.input.keyboard.addKey(Phaser.Keyboard.D);
		this.spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

Character.prototype.update = function(){
	this.game.physics.arcade.collide(this.sprite, this.layer);
	if (this.leftKey.isDown)
		this.sprite.scale.setTo(-0.11, 0.11);
		this.sprite.body.velocity.x = -200;
    else if (this.rightKey.isDown)
		this.sprite.scale.setTo(0.11, 0.11);
		this.sprite.body.velocity.x = 200;
		this.sprite.body.velocity.x *= 0.8;
		if(this.sprite.body.onFloor() && this.game.time.now > this.jumpTimer){
					this.sprite.body.velocity.y = -300;
					this.jumpTimer = this.game.time.now + 750;


Link to comment
Share on other sites

3 hours ago, Connormcwood said:

I've used collision function to no avail, apparently I cannot access .sprite. I must be missing something or the functionality is not there.

What were you trying to do in the collision function?

Link to comment
Share on other sites

7 minutes ago, samme said:

What were you trying to do in the collision function?

I am just wanting the player to not be able to past the spike when it is in a certain frame. Thats reminants of me following tutorials unsuccessfully. Was hoping that there was a glaring error within my code which would be why I cannot make the spikes body to be toggleable depending on its current frame.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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