Sign in to follow this  
goide

Prototype of a game

Recommended Posts

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.
 
shoots.png

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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}

Share this post


Link to post
Share on other sites
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.

 


Share this post


Link to post
Share on other sites

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 ---    }});

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

This is my result.

 

log.png

 

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?

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.        },

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.