Connormcwood Posted March 4, 2017 Share Posted March 4, 2017 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.play('open'); 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(){ this.addCollision(); }; MainScreen: myGame.MainGameScreen = function(game){ var mummys; var mummysTotal; var mummysAlive; var gameHeight; var gameWidth; this.map; this.layer; }; myGame.MainGameScreen.prototype = { create: function(){ this.game = this; this.physics.startSystem(Phaser.Physics.ARCADE); 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.layer.resizeWorld(); this.map.setCollision([1,2,3,5,6,7,9,10,11,12]); 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); console.log(this.character); }, /* setupObject: function(object){ console.log(object); if(object.key == "spikes"){ console.log('spikes'); //Trap(this, object.world.x, object.world.y); new Trap(this.game, object.world.x, object.world.y); } }, */ update: function(){ this.character.update(); this.trap.forEach(function(trap){ if(trap.checkHit() == true){ console.log(trap + ' ' + this.character); } }); if(game.physics.arcade.collide(this.character, this.trap)){ console.log('this'); } } } Character 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.scale.setTo(0.11); this.sprite.anchor.setTo(0.5, 0.5); this.sprite.animations.play('idle'); this.game.camera.follow(this.sprite); 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; this.sprite.animations.play('run'); } else if (this.rightKey.isDown) { this.sprite.scale.setTo(0.11, 0.11); this.sprite.body.velocity.x = 200; this.sprite.animations.play('run'); } else { this.sprite.body.velocity.x *= 0.8; this.sprite.animations.play('idle'); } if(this.spaceKey.isDown){ 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 More sharing options...
samme Posted March 4, 2017 Share Posted March 4, 2017 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 More sharing options...
Connormcwood Posted March 4, 2017 Author Share Posted March 4, 2017 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 More sharing options...
samme Posted March 4, 2017 Share Posted March 4, 2017 Move all the arcade.collide calls to the start of MainGameScreen#update. You need to collide character.sprite: game.physics.arcade.collide(this.character.sprite, this.trap) Link to comment Share on other sites More sharing options...
Recommended Posts