Jump to content

Kill items - forEach Problem


boulder
 Share

Recommended Posts

I would like to kill objects. I have a method that works, when the objects are no animations.
But now I have animations! The 'goldcoins' rotate in a Lopp. This Goldcoins I want to kill when 'ufo' and 'goldcoin' overlap.

 

A friend helped me yesterday in the Chat, but I lost his scripts through a Firefox crash.
 

The forEach-example does not really help me further. Does anyone perhaps hints?
 

Here is my script. The errors probably stuck in the lines: 53-54 in connection with 78-81

var game = new Phaser.Game(260, 320, Phaser.CANVAS, 'killCoins', { preload: preload, create: create, update: update });function preload() {    game.load.image('world', 'assets/images/welt.gif');    game.load.image('ufo', 'assets/images/ufo.png');    game.load.spritesheet('goldcoin', 'assets/images/goldcoins.png', 40, 40, 6);}var coinsvar ufo;var cursors;function create() {    var myFontStyle = { font: "15px Arial", fill: "#F2F06E" };    game.world.setBounds(0, 0, 780, 320);    game.add.sprite(0, 0, 'world');    ufo = game.add.sprite(40, 120, 'ufo');    game.physics.enable(ufo, Phaser.Physics.ARCADE);    ufo.body.collideWorldBounds = true;    game.camera.follow(ufo);		healthText = game.add.text(16, 16, 'Health: 100', { font: '15px Arial', fill: '#F2F06E' });	goldText = game.add.text(16, 30, 'Gold: 0', myFontStyle);		healthText.fixedToCamera = true;	goldText.fixedToCamera = true;        coins = game.add.group();    for (var i = 0; i < 7; i++) {        coins.create(170+i*70, 190, 'goldcoin', 0);    }    coins.callAll('animations.add', 'animations', 'spinYou', [0, 1, 2, 3, 4, 5], 10, true);    coins.callAll('animations.play', 'animations', 'spinYou');    cursors = game.input.keyboard.createCursorKeys();}function update() {	    // I think here is the Mistake    // With this two lines i become a "Black-Screen" in Browser    // Kill function is in Line 77    game.world.forEach(function(goldcoin) {    game.physics.arcade.overlap(ufo, goldcoin, doKillgoldcoin, null, this);	}    if (cursors.left.isDown)    {        ufo.x -= 4;    }    else if (cursors.right.isDown)    {        ufo.x += 4;    }       if (cursors.up.isDown)    {        ufo.y -= 4;    }    else if (cursors.down.isDown)    {        ufo.y += 4;    }}function doKillgoldcoin (ufo, goldcoin) {        // Should kill the goldcoin    goldcoin.kill();}
Link to comment
Share on other sites

Well, first of all, you haven't close the forEach call:

game.world.forEach(function(goldcoin) {     game.physics.arcade.overlap(ufo, goldcoin, doKillgoldcoin, null, this);}); // <- you missed both parenthesis and semicolon. 

Second, you must add the context for the call as second parameter:

game.world.forEach(function(goldcoin) {game.physics.arcade.overlap(ufo, goldcoin, doKillgoldcoin, null, this);}, this); // <- context.

I hope this helps. 

 

Link to comment
Share on other sites

Look at the Phaser exemple on Git, the space invaders like ;)

 

I know that Example, but this is not that what im looking for, at this Moment. The Invaders Variation i want study later.

But thanks you for your attention!

 

 

Well, first of all, you haven't close the forEach call:

game.world.forEach(function(goldcoin) {     game.physics.arcade.overlap(ufo, goldcoin, doKillgoldcoin, null, this);}); // <- you missed both parenthesis and semicolon. 

Second, you must add the context for the call as second parameter:

game.world.forEach(function(goldcoin) {game.physics.arcade.overlap(ufo, goldcoin, doKillgoldcoin, null, this);}, this); // <- context.

I hope this helps. 

 

 

Oouuch, the brackets I had not checked, my mistake!.

 

But...., I have found the way to kill the animated coins, exactly with these lines:

    game.physics.arcade.overlap(player, stars, collectStar, null, this);

and as desired:

function collectStar (player, star) {        // Removes the star from the screen    star.kill();}

It was no more and no less necessary!

 

The error had something to do with the callAll. There I used the wrong Variable and a few other little things. But the fact seems to be: forEach here is not mandatory.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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