Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Im struggling to under the "destroy();"


Recommended Posts

function spikekills (player,spike){




I tried a few formats hoping it can work however, I am not getting any result. 

If more code is needed I will gladly show. 




typo with the tittle sorry please disregard my typo. I meant to say I am struggling to understand destroy();

Link to comment
Share on other sites



player.destroy() sems ok (phaser example). When you are asking for help, try to be more specifi and bring the most you can, for example code is not working helps as much as oranges are not blue. What kind of error did you get, which line was the error on and what is that line and what is before it. And so on, try to bring more details please. :-)


Right no we have no way to see what could be wrong, not only you didn't specify an error message but your code is missing as well. You don't need to post your whole code, just prepare sample stripped off everything and keep only two vars player and spike and one function where you destroy the player and then just call the function, its just perhaps 10 lines of code (see no need to copy paste your whole code) which can represent your problem and which you test on your system and then we can talk about what is causing problem in your case, is it ok?

Link to comment
Share on other sites

Well Im not getting an error in my code  :(. Im using chrome inspect element console.

phaser.min.js:13'webkitAudioContext' is deprecated. Please use 'AudioContext' instead.
phaser.min.js:9    Phaser v2.1.2 | Pixi.js v1.6.1 | WebGL | WebAudio     http://phaser.io    â™¥â™¥â™¥ 
That is the only thing that shows up but, that shouldn't mess up my code.
    the following is the code you might need.
game.load.image('spike', './assets/spike.png');
game.load.spritesheet('chicken', './assets/chicken.png', 45,48, 2);}
var player;
var spike
function spikekills (player, spike){

Not sure if you need more.  


edit out destroy for kill

I am still not getting any result 

Link to comment
Share on other sites

Well you don't have any sprite here.


You need to set you player and spike as sprites to do anything with them. Currently your vars are just empty.


Set it as:

var spike = game.add.sprite(spikeXPosition, spikeYPosition, 'spike');

(spikeXPosition and spikeYPosition set as you need I just used placeholder to let you know what is supposed to be there)

Player in similar way, well because you are looking for kil method on player you should at least define player as a sprite to make the code work.


Btw in your code you should at least call your funtion spikekills to show that you are not just defining it, it's just a silly little thing but jsut to be sure that you actually do it ;-).

Link to comment
Share on other sites

well here is my whole code.

<html><head><style type="text/css">  body {            margin: 0;        }</style><script type="text/javascript" src="js/phaser.min.js"> </script><script type="text/javascript" src="pixi.js"> </script><script type="text/javascript">      var game = new Phaser.Game(801, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });  //game.physics.startSystem(Phaser.Physics.ARCADE);function preload() {  // I have loaded my assests heregame.load.image('explosions', './assets/explosions.png');game.load.image('mushroom','./assets/mushroom96x32px.png');game.load.image('platform', './assets/turtle.png');game.load.image('sky', './assets/nightsky.png');game.load.image('spike', './assets/spike.png');game.load.image('mountain','./assets/moutain.png');game.load.image('moon','./assets/moon2.png');//game.load.audio('lz', ['assets/Shop - The Legend of Zelda- Ocarina of Time.mp3']);game.load.spritesheet('chicken', './assets/chicken.png', 45,48, 1);}var player;var platforms;var platform;var cursors;var spike;var audio;var s;var moon;var mushroomvar frame;//var text;//text = game.add.text(0, 0, 'Congratulions', { fontSize: '16px', fill: '#FF0000' });function spikekills (player, spike){       player.kill();}function create(){    game.physics.startSystem(Phaser.Physics.ARCADE);    game.physics.arcade.collide('chicken','platform');    game.physics.arcade.collide('chicken','spike');    cursors = game.input.keyboard.createCursorKeys();    game.add.sprite(0,0, 'sky');    //game.add.sprite(500,150, 'moon');    //moon.scale.setTo(0.5,0.5); mysprite = this.game.add.sprite(15, 30, 'chicken'); mysprite.frame = 3; player = game.add.sprite(100, game.world.height - 200, 'chicken');          game.physics.arcade.enable(player); player.animations.add('left', [0,1], 3, true); player.animations.add('right', [2,3], 3, true);            // moon            moon = game.add.group();            var circle = moon.create(705,1, 'moon');            circle.scale.setTo(.4,.4);              // spike sprite              // spike group        spike = game.add.group();        spike.enableBody = true;  //  var floor = spike.create(500, game.world.height - 120, 'spike');    //    floor.scale.setTo(0.75,0.75);      //  floor.body.immovable = true;              // spike create    var bottom = spike.create(600,550,'spike');        bottom.body.immovable = true;    var deathd = spike.create (345,550,'spike');        deathd.body.immovable = true;    var button = spike.create(420,480,'spike');        button.body.immovable = true;     var deathb = spike.create ( 385,550,'spike');        bottom.body.immovable = true;    var deathd = spike.create ( 550,550,'spike');        deathd.body.immovable = true;    var deathe = spike.create (650,550, 'spike');        deathe.body.immovable = true;    var deathf = spike.create (700,550, 'spike');        deathf.body.immovable= true;    var deathg = spike.create (750,550,'spike');        deathg.body.immovable = true;        // platform sprite        platform = game.add.group();        platform.enableBody = true;          // creating platform belongs here   var ground = platform.create(0, game.world.height - 64, 'platform');        ground.scale.setTo(1.9,2);        ground.body.immovable = true;   var ledge = platform.create(-150,50,'platform');        ledge.body.immovable = true;   var rside = platform.create(150,250, 'platform');        rside.body.immovable = true;   var morerside = platform.create(400,500, 'platform');        morerside.body.immovable = true;   var rsidea = platform.create( 750,280, 'platform');        rsidea.body.immovable = true;           // mushroom      mushroom = game.add.group();      mushroom.enableBody = true;      var lushroom = platform.create( 0, game.world.height - 0,'mushroom');      var push = platform.create(650,400,'mushroom');           push.body.immovable = true;      var lmush = platform.create(50,150,'mushroom');          lmush.body.immovable = true;          player.animations.add['left', [1,2],4, true];          player.animations.add['right', [3,4],4, true];            // music        //music = game.add.audio('lz');        // music.play();}function update() {    game.physics.arcade.collide(player,platform);    game.physics.arcade.collide(player,spike);    game.physics.arcade.collide(player,mushroom);     game.physics.arcade.overlap(player, spike, spikekills, null, this);    cursors = game.input.keyboard.createCursorKeys();         game.physics.arcade.enable(player);          player.body.bounce.y = .2;          player.body.gravity.y = 50;          player.body.collideWorldBounds = true;          // animations          player.frame = 3;    player.body.velocity.x = 0;    if (cursors.left.isDown)    {        //  Move to the left        player.body.velocity.x = -140;        player.animations.play('left');    }    else if (cursors.right.isDown)    {        //  Move to the right        player.body.velocity.x = 125;        player.animations.play('right');    }    else    {        //  Stand still        player.animations.stop();        player.animations.add('left', [0,1],true);        player.animations.add('right', [3,4],true);    }    //  Allow the plawyer to jump if they are touching the ground.    if (cursors.up.isDown && player.body.touching.down)    {        player.body.velocity.y = -110;    }}function spikekills (player, spike){console.log('player')       player.kill();}</script></head><body></body></body></html>
Link to comment
Share on other sites

I don't even know where to begin with.


It's over midnight over here so I'll keep it short and check it in the morning or even better someone else will check it for you.


1/ I don't think you need spikeskill defined twice (not a problem for JS but it just doesn't need to be there twice)


2/ What's this (why strings in keys?) and why is it in create function?


3/ This doesn't belong to your update but your create function.

cursors = game.input.keyboard.createCursorKeys();game.physics.arcade.enable(player);player.body.bounce.y = .2;player.body.gravity.y = 50;player.body.collideWorldBounds = true;// animationsplayer.frame = 3;

4/ You are doing collision check and then overlap check, logically if you do physic collision then the system won't let the objects overlap, so your overlap call never happens. Either add your spikekills to your collision call or remove the collision code entirely what suits your needs.

game.physics.arcade.collide(player,platform);game.physics.arcade.collide(player,spike);  // remove or this way: game.physics.arcade.collide(player,spike, spikekills, null, this);game.physics.arcade.collide(player,mushroom);game.physics.arcade.overlap(player, spike, spikekills, null, this); // remove this or keep it but remove collide as commented above

5/ I'm almost certain that you don't want to do this in update function.

else { //  Stand stillplayer.animations.stop();player.animations.add('left', [0,1],true);  // remove this from update fnplayer.animations.add('right', [3,4],true);  // remove this from update fn}

You add animations once in your create function and then only call play and stop in update where it's needed, don't keep adding the same thing it doesn't change anything unless you destroy the player every update iteration.


6/ Shouldn't this be placed in else part from step 5/ above?

player.body.velocity.x = 0;

Well I might have missed few things here and there and perhaps not everything is valid (I doubt that though) but I would go through a few more tutorials if I were in your place.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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