arknoid Posted November 18, 2017 Share Posted November 18, 2017 Hi , I m new user of phaser and i develop a retro shoot them up for the GameOFF jam . i have a problem in my game state : enemyTween: function(){ this.enemyPool.forEachAlive(function(enemy){ enemyTween = this.game.add.tween(enemy) }) TypeError: this.game is undefined [En savoir plus] why i cant acces to game and other event into function of group? (sorry for my bad english) Link to comment Share on other sites More sharing options...
mattstyles Posted November 18, 2017 Share Posted November 18, 2017 I'm not sure how `enemyPool` or the `forEachAlive` function works but often this sort of thing is a scoping issue (although the error you've reported is slightly wrong for a scope issue). The problem is that JS is sometimes a little unexpected for scoping and so any time you use a class or object you have to work with `this`, which is a common cause of issue. In your case I think that (maybe) you should just scope the callback function, you have some options here: this.enemyPool.forEachAlive(function(enemy){ enemyTween = this.game.add.tween(enemy) }, this) Notice the following `this` after the function declaration, usually the `forEachAlive` function can accepts another parameter that defines scope. You can also do this explicitly by calling `bind` yourself: this.enemyPool.forEachAlive(function(enemy){ enemyTween = this.game.add.tween(enemy) }.bind(this)) `bind` sets the `this` of a function. You have another potential issue though: enemyTween: function(){ this.enemyPool.forEachAlive(function(enemy){ enemyTween = this.game.add.tween(enemy) }) } You're overwriting `enemyTween`, hard to tell without seeing the rest of the code but its probably bad. Link to comment Share on other sites More sharing options...
arknoid Posted November 18, 2017 Author Share Posted November 18, 2017 Thank you very much, it works well with "this". desolate if I ask stupid questions but I started with javascript and phaser, furthermore I find that the documentation lacks concrete example and detail. I will soon have new questions to ask you but for the moment I try to unraveled myself Link to comment Share on other sites More sharing options...
mattstyles Posted November 18, 2017 Share Posted November 18, 2017 2 hours ago, arknoid said: desolate if I ask stupid questions Deffo not a stupid question, JS scoping can be a little misleading, or, surprising, particularly if you come with any knowledge from real classical languages. There is another option using arrow functions: .forEach(item => { // do something }) However, some libraries will try to scope your function (using .bind usually) so it won't always work as expected. Also also, arrow function scoping is even more confusing. Link to comment Share on other sites More sharing options...
samme Posted November 18, 2017 Share Posted November 18, 2017 2 hours ago, arknoid said: furthermore I find that the documentation lacks concrete example and detail. https://photonstorm.github.io/phaser-ce/Phaser.Group.html#forEachAlive http://phaser.io/examples/v2/category/groups Link to comment Share on other sites More sharing options...
arknoid Posted November 18, 2017 Author Share Posted November 18, 2017 thank for all i dont know this Api : https://photonstorm.github.io/phaser-ce/index.html Link to comment Share on other sites More sharing options...
arknoid Posted November 21, 2017 Author Share Posted November 21, 2017 I have a problem again, I want to setup group in create event and call number of each later (with timer) setupEnemies:function(){ this.asteroidPool = this.add.group(); this.bigAsteroidPool = this.add.group(); this.tiePool = this.add.group(); this.interceptorPool = this.add.group(); this.enemyPool = this.add.group(); this.enemyPool.enableBody = true; this.enemyPool.physicsBodyType = Phaser.Physics.ARCADE; }, addEnemies: function(nbrTie,nbrAsteroid,nbrBigAsteroid,nbrInterceptor) { /*************************************************************************** * Asteroid * ***************************************************************************/ this.asteroidPool.createMultiple(nbrAsteroid, 'asteroid'); this.asteroidPool.createMultiple(nbrAsteroid, 'asteroid2'); this.asteroidPool.createMultiple(nbrAsteroid, 'asteroid3'); this.asteroidPool.forEach(function(child) { child.name = "asteroid" child.animations.add('idle', [0]); child.animations.add('hit', [0, 1, 0, 1], 20, false); child.events.onAnimationComplete.add(function(e) { e.play('idle'); }, this); child.name = "asteroid"; }); /*************************************************************************** * bigAsteroid * ***************************************************************************/ this.bigAsteroidPool.createMultiple(nbrBigAsteroid, 'bigAsteroid'); this.bigAsteroidPool.forEach(function(child) { child.name = "bigAsteroid" child.animations.add('idle', [0]); child.animations.add('hit', [0, 1, 0, 1], 20, false); child.events.onAnimationComplete.add(function(e) { e.play('idle'); }, this); }); /*************************************************************************** * tie * ***************************************************************************/ this.tiePool.createMultiple(nbrTie, 'tie'); this.tiePool.forEach(function(child) { child.name = "tie" child.animations.add('idle', [0, 1, 2, 3], 20, true); child.animations.add('hit', [0, 4, 0, 4], 20, false); //quand c'est finie retour sur idle child.events.onAnimationComplete.add(function(e) { e.play('idle'); }, this); }); /*************************************************************************** * interceptor * ***************************************************************************/ this.interceptorPool.createMultiple(nbrInterceptor, 'interceptor'); this.interceptorPool.forEach(function(child) { child.name = "interceptor"; child.animations.add('idle', [0, 1, 2], 20, true); child.animations.add('hit', [0, 3, 0, 3], 20, false); //quand c'est finie retour sur idle child.events.onAnimationComplete.add(function(e) { e.play('idle'); }, this); }); this.enemyPool.addMultiple(this.asteroidPool); this.enemyPool.addMultiple(this.tiePool); this.enemyPool.addMultiple(this.interceptorPool); this.enemyPool.addMultiple(this.bigAsteroidPool); this.enemyPool.setAll('anchor.x', 0.5); this.enemyPool.setAll('anchor.y', 0.5); this.enemyPool.setAll('outOfBoundsKill', true); this.enemyPool.setAll('checkWorldBounds', true); }, Create: function() { /************************************************************************************************ *Initialize * ************************************************************************************************/ this.setupBackground(); //Initialize Background (A completter) this.setupScaleMode(); //Initialize Sclaling and no blur this.setupEnemiesShot(); this.setupEnemies(); //Initialize Enemies this.setupPlayerShot(); //Initialize Player Shot this.setupExplode(); //Initialize Explosion effets this.setupText(); //Initialize Text Screen this.setupPlayer(); //Initialize Player this.setupScore(); //Initialize text score this.setupLives(); //Initialize text lives this.addEnemies(1,10,2,1); But nothing ... Full source code here : https://gitlab.com/Arknoid/The-lost-jedi/tree/Olivier Link to comment Share on other sites More sharing options...
Recommended Posts