goide Posted July 30, 2015 Share Posted July 30, 2015 The problem is as described in the following video. https://db.tt/NAvsrdVD I have two timers, one to generate a new enemy. and the other to generate the shot.this.addTimer(3000, this.addEnemy.bind(this), true); //Call method for generate enemysthis.agregarEnemigo(); //update rotation angle for enemythis.addTimer(400, this.eBullets.bind(this), true);addEnemy: function(){ this.enemigo = new game.Enemigo(game.width.random(), game.height.random()); //call class enemy}eBullets: function(){ if(this.enemigo.life === true){ this.enemigo.Shoot(); } }This is my code to work as this in the video. The goal is to generate enemies and shoot them my hero. So far the problem that I have is that when you build the next enemy. the former enemy stops shooting. And I want to shoot the enemies simultaneously. Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 1, 2015 Share Posted August 1, 2015 It seems like the problem is due to you missing an enemies array to contain all the enemies. you keep overriding this.enemigo. Quote Link to comment Share on other sites More sharing options...
goide Posted August 1, 2015 Author Share Posted August 1, 2015 Yes, I thought that, that every time a new enemy called, acts as if you were creating a new object, and returns and calls the two methods. why not keep shooting the former enemy. I thought of all the enemy build through an array and cross it with a for loop. instead of using the timer. Can you help me how to build what you're saying. Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 1, 2015 Share Posted August 1, 2015 I would add the eBullets function and timer to the Enemigo class and then in the addEnemy function I would change this.enemigo to an array push. So something like this:this.enemigos = [];addEnemy: function(){ this.enemigos.push( new game.Enemigo(game.width.random(), game.height.random()) ); //call class enemy} goide 1 Quote Link to comment Share on other sites More sharing options...
goide Posted August 2, 2015 Author Share Posted August 2, 2015 Thanks!! Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 3, 2015 Share Posted August 3, 2015 you're welcome. Quote Link to comment Share on other sites More sharing options...
goide Posted August 4, 2015 Author Share Posted August 4, 2015 Try as you explained to me. My enemy class, add the eBullet method, and this method has a timer calls. Ok, my main.js file and call the class enemy as you indicate me in your example. look at the video, I still have the same result. when an enemy is added, the previous stop shooting. Video: https://db.tt/xlo4Xlyq Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 4, 2015 Share Posted August 4, 2015 I need to see a working code example, the video doesn't help much. Are you setting the timer from within the init method of the Enemigo class? like this:game.createClass('Enemigo', { init: function(){ this.ShootTimer = game.scene.addTimer(400, this.eBullets.bind(this), true); }, eBullets: function(){ //--- do something --- }}); Quote Link to comment Share on other sites More sharing options...
goide Posted August 5, 2015 Author Share Posted August 5, 2015 My Code. Class EnemigoRazo game.createClass('EnemigoRazo',{ vida: true, init:function(x,y){ this.cuerpoEnemigo = new game.Body(); this.cuerpoEnemigo.collisionGroup = 7; this.cuerpoEnemigo.position.set(x,y); this.cuerpoEnemigo.collideAgainst = [0]; this.sprite = new game.Sprite('enemigo.png'); this.sprite.anchorCenter(); this.sprite.position.set(x,y); this.sprite.visible = false; this.sprite.addTo(game.scene.contenedor); this.animateSprite = new game.Animation([ 'enemigo/enemigoRun-0.png', 'enemigo/enemigoRun-1.png', 'enemigo/enemigoRun-2.png', 'enemigo/enemigoRun-3.png', 'enemigo/enemigoRun-4.png', 'enemigo/enemigoRun-6.png', 'enemigo/enemigoRun-7.png', 'enemigo/enemigoRun-8.png', 'enemigo/enemigoRun-9.png' ]); this.animateSprite.addAnim('correr',[0,1,2,3,4,3,2,1,0,5,6,7,8,7,6,5,0],{speed:7}); this.animateSprite.width = 40; this.animateSprite.height = 19; this.animateSprite.anchorCenter(); this.animateSprite.position.set(x,y); this.animateSprite.addTo(game.scene.contenedor); this.animateSprite.play('correr'); var shape = new game.Rectangle(this.sprite.width,this.sprite.height); this.cuerpoEnemigo.addShape(shape); this.cuerpoEnemigo.addTo(game.scene.world); this.enemigoCorriendo(); game.scene.addTimer(400, this.disparaEnemigo.bind(this), true); }, enemigoCorriendo: function(){ game.scene.balaBuscaSoldado(this.cuerpoEnemigo,game.scene.sprite,20,0); }, removeAnimation: function(){ this.animateSprite.remove(); }, disparaEnemigo: function(){ this.balas = new game.Disparar(); this.balas.rotation = game.scene.balaBuscaSoldado(this.balas.bodyBala,game.scene.sprite,300,0);//The bullet seeking the position of my hero. }, update: function(){ this.animateSprite.position.copy(this.cuerpoEnemigo.position); this.sprite.position.copy(this.cuerpoEnemigo.position); } });This is my enemy class and this works, as you see in the video. Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 5, 2015 Share Posted August 5, 2015 I am not sure what is causing the issue. It might be a problem with your game.Disparar class but I don't know. What I can suggest you is to debug your code with console.log() and find where it stops returning the expected values. example:game.createClass('Enemigo', { init: function(id){ this.id = id; game.scene.addTimer(400, this.eBullets.bind(this), true); }, eBullets: function(){ //--- do something --- console.log('enemigo '+this.id+' is shooting!'); }});new game.Enemigo(1);new game.Enemigo(2);This should return a concurrent shooting text from both objects every 400ms. If this works it means the problem is in your game.Disparar class I think. Quote Link to comment Share on other sites More sharing options...
goide Posted August 5, 2015 Author Share Posted August 5, 2015 This is my result. Otherwise, only the last enemy shoots screen, the above does not fire. My class game.Disparar.game.createClass('Disparar', { init: function(){ this.bodyBala = new game.Body(); this.bodyBala.collisionGroup = 5; this.bodyBala.velocity.set(10,10); this.bodyBala.collideAgainst = [0,3]; for(var i=0; i<game.scene.enemigosRazo.length; i++){ this.bodyBala.position.set(game.scene.enemigosRazo[i].sprite.position.x, game.scene.enemigosRazo[i].sprite.position.y); } this.bodyBala.collide = this.colisionBarricada.bind(this); this.bala = new game.Sprite('bala.png'); this.bala.anchorCenter(); this.bala.addTo(game.scene.contenedor); var shape = new game.Circle(this.bala.width, this.bala.height); this.bodyBala.addShape(shape); this.bodyBala.addTo(game.scene.world); }, removeBala: function(){ this.bodyBala.remove(); this.bala.remove(); game.scene.removeObject(this); }, update: function(){ this.bala.position.copy(this.bodyBala.position);//Update position bullet } });You speak Spanish? Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 5, 2015 Share Posted August 5, 2015 I am not 100% sure but I think the problem might be with these few lines:for(var i=0; i<game.scene.enemigosRazo.length; i++){ this.bodyBala.position.set(game.scene.enemigosRazo[i].sprite.position.x, game.scene.enemigosRazo[i].sprite.position.y);}If I understand this correctly I don't know why you chose to update the bullets starting position on every one of the Enemigos every time a bullet is created. every time you call new game.Disparar() it's a completely new instance of game.Disparar there's no point in setting all the bullets position in every instance of it you should update only the current bullet position I think. no unfortunately I do not speak Spanish. I wish I did... Quote Link to comment Share on other sites More sharing options...
goide Posted August 6, 2015 Author Share Posted August 6, 2015 Watch the video, if I miss these lines of code, the bullet starts from zero. What this does is update the bullet pocision, where is the enemy. look: https://db.tt/BMdB0BL8 Quote Link to comment Share on other sites More sharing options...
LinkTree Posted August 6, 2015 Share Posted August 6, 2015 You shouldn't omit them just change them so it doesn't set all of the bullet's positions every time you call game.Disparar. in the Disparar init method request another argument for the Enemigo who shot the bullet and then get the bullet starting position from that Enemigo position. so something like this:game.createClass('Disparar', { init: function(src){ this.bodyBala = new game.Body(); this.bodyBala.collisionGroup = 5; this.bodyBala.velocity.set(10,10); this.bodyBala.collideAgainst = [0,3]; this.bodyBala.position.set(src.sprite.position.x, src.sprite.position.y); this.bodyBala.collide = this.colisionBarricada.bind(this); this.bala = new game.Sprite('bala.png'); this.bala.anchorCenter(); this.bala.addTo(game.scene.contenedor); var shape = new game.Circle(this.bala.width, this.bala.height); this.bodyBala.addShape(shape); this.bodyBala.addTo(game.scene.world); }, removeBala: function(){ this.bodyBala.remove(); this.bala.remove(); game.scene.removeObject(this); }, update: function(){ this.bala.position.copy(this.bodyBala.position);//Update position bullet } });and in game.Enemigo do this:disparaEnemigo: function(){ this.balas = new game.Disparar(this); this.balas.rotation = game.scene.balaBuscaSoldado(this.balas.bodyBala,game.scene.sprite,300,0);//The bullet seeking the position of my hero. }, goide 1 Quote Link to comment Share on other sites More sharing options...
goide Posted August 11, 2015 Author Share Posted August 11, 2015 Many thanks brother, if you ran the code that describistes me, thanks. 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.