lars Posted October 28, 2015 Share Posted October 28, 2015 I have the following collide function in a class: collide: function(opponent) {this.pumkins.textures=this.poffedPumkins.textures;this.pumkins.currentFrame=0;this.pumkins.loop=false; this.pumkins.onComplete = function(){this.remove();};}I get the following error:Cannot read property 'updateTransform' of undefinedon the this.remove(); It work allright when I call the this.remove() directly in the collide function. Quote Link to comment Share on other sites More sharing options...
ansien123 Posted October 28, 2015 Share Posted October 28, 2015 Most likely because "this" has a different context in the onComplete function. Quote Link to comment Share on other sites More sharing options...
lars Posted October 28, 2015 Author Share Posted October 28, 2015 Ok. How to deal with that. Sorry im total new to Panda.js Quote Link to comment Share on other sites More sharing options...
Stephan Posted October 28, 2015 Share Posted October 28, 2015 yeah, since you have juast a single function to call on completion, you could use this:This.pumpkin.onComplete(this.remove.bind(this)); Quote Link to comment Share on other sites More sharing options...
lars Posted October 29, 2015 Author Share Posted October 29, 2015 Thank´s a lot for the answer Hhmmm. That did not do it :-) Im a little bit confused about the bind method ... here is the class im working on. I would be very thankful if you have the time to have a look: game.createClass('Pumpkins', { init: function() { var myPumkingsVelocityArray=[100,80,60,50,40,32,45,67,78,98,90]; var pumkingVInterval = myPumkingsVelocityArray[Math.floor(Math.random()*myPumkingsVelocityArray.length)]; var x = Math.random(16, game.system.width); //this.pumkins = new game.Sprite('pumkings'); this.pumkins = new game.Animation('media/pumkings.png'); this.pumkins .animationSpeed = 0.3; this.pumkins .position.set(230, 350); this.pumkins.anchor.set(0,1); this.pumkins.play(); this.poffedPumkins = new game.Animation( 'media/pum1.png', 'media/pum2.png', 'media/pum3.png', 'media/pum4.png', 'media/pum5.png' ); this.pumkins.anchor.set(0.5, 0.5); this.body = new game.Body({ position: { x: x , y: - this.pumkins.height }, velocity: { x: 0, y: pumkingVInterval }, collisionGroup: 1, collideAgainst: [0], mass: 0, }); this.body.collide = this.collide.bind(this); this.body.addShape(new game.Rectangle(20, 20)); game.scene.addObject(this); game.scene.world.addBody(this.body); game.scene.stage.addChild(this.pumkins); this.update(); }, collide: function() { this.pumkins.textures=this.poffedPumkins.textures;this.pumkins.currentFrame=0;//In this case just run it oncethis.pumkins.loop=false; //this.remove(); this.pumkins.onComplete(this.remove.bind(this)); // this is the one i have been trying before /* this.pumkins.onComplete = function(){ this.remove(); }; */ }, update: function() { this.pumkins.position.x = this.body.position.x; this.pumkins.position.y = this.body.position.y; if (this.pumkins.position.y - this.pumkins.height / 2 > game.system.height) this.remove(); //this.body.position.x += 50 * game.system.delta; }, remove: function() { game.scene.removeObject(this); game.scene.world.removeBody(this.body); game.scene.stage.removeChild(this.pumkins); } }); Quote Link to comment Share on other sites More sharing options...
lars Posted October 29, 2015 Author Share Posted October 29, 2015 Sorry did´nt say what error there was now: this.pumkins.onComplete is not a function Quote Link to comment Share on other sites More sharing options...
lars Posted October 30, 2015 Author Share Posted October 30, 2015 Solution:Adding a timer ... before remove ... found in Spaceshooter Example ... collide: function() { this.pumkins.textures = this.poffedPumkins.textures; this.pumkins.currentFrame = 0; //In this case just run it once this.pumkins.loop = false; this.pumkins.onComplete = function() { console.log("allo"); this.pumkins.visible = false; game.scene.addTimer(0, function() { this.remove();}.bind(this)); }.bind(this); }, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.