Jump to content

creating multiple enemies


latynos
 Share

Recommended Posts

I'm new on this forum so welcome. I'm trying to create game using js but i stepped on a problem that i dont know how to resolve. I can create enemy, but i dont know how to create multiple enemies. I tried place them in array and then .push them on the screen but it's not working. The problem is in function tworz(), i want to draw there single enemy and after some time this function should draw another untill array is empty. Does anyone have a tip how to make it work? I would be very greatefull for help.

 

cheers

var przec = document.getElementById("przeciwnik").getContext("2d");var img = new Image();img.src = "../obrazki/przeSprite.png";    for (i=0; i<lPot; i++){   tabPot.push(new AnimacjeOb(przec, img, 60, 60,5));}function tworz(){       var liczba = 15;      //var licznik = 1 ;      licznik ++;     // document.write(licznik);      if (licznik >= liczba){                  for (i = 0; i<tabPot.length; i++){            tabPot[i].animacja();        // przPrawo.animacja();      //  }           }     licznik = 1;      }       // potworki.animacja;         //  przPrawo.animacja();    };  // };          function utPrz() {       var mobek = document.getElementById("przeciwnik");       var gctx = mobek.getContext("2d");       mobek.width = 640;       mobek.height = 480;        dom.dodKlase (mobek, "mobki");                  setInterval(tworz, 100);                                dom.dodKlase(mobek, "mobki");        return mobek;            };
Link to comment
Share on other sites

  • 3 months later...

(function(){

    'use strict';

    window.addEventListener('load',init,false);

    var canvas=null,ctx=null,canvas2=null,ctx2=null;

   var maxEnemies=10;

   

   var enemies=[];

    

function enemi(){

this.mode=false;

this.x=0;

this.y=0;

}

enemi.prototype.exist=function(){

this.x++;

}

enemi.prototype.paint=function(){

}

   function launhEnemi(){

  /*

call this function when you want to launch a new enemy,

This method is called "object pool"

please do NOT create new objects in the game loop (not in operations, not paint)

*/

  for(var i=1, i<= maxEnemies; i++){

if(!enemies.mode){

enemies.mode=true;

break;

}

}

   }

    function init(){

    

            

       

        canvas=document.getElementById('canvas');

ctx=canvas.getContext('2d',{antialias:false});

canvas.width=ancho;

        canvas.height=alto;

if(navigator.platform=="Win32"){canvas.style.position='fixed';}else{canvas.style.position='static';}

//canvas.style.position='absolute';

canvas.style.backgroundColor='transparent';

canvas.style.top='0';

        canvas.style.left='0';

        canvas.style.width='100%';

        canvas.style.height='100%';

canvas.style.zIndex="10";

//create your enemies

       for(var i=1, i<= maxEnemies; i++){

  enemies=new enemi();

  }

        run();

    }//end of  init()

    function run(){

        requestAnimationFrame(run);

        

        var now=Date.now();

        var deltaTime=now-time;

        if(deltaTime>1000)deltaTime=0;

        time=now;

        

        act();

        paint(ctx);

    }

    function act(){

       //all operations

  frame++;

        for(var i=1, i<= maxEnemies; i++){ if(enemies.mode)enemies.exist();}

    }//end of  act

    function paint(){

        //ctx.fillStyle='#000';

        ctx.clearRect(0,0,ancho,alto);

ctx.fillStyle="rgba(255,255,255,1)";

ctx.fillText(frame,50,50);

        //all paint objects

for(var i=1, i<= maxEnemies; i++){ if(enemies.mode)enemies.paint();}

    }//end of paint

   window.requestAnimationFrame=(function(){

        return window.requestAnimationFrame || 

            window.webkitRequestAnimationFrame || 

            window.mozRequestAnimationFrame || 

window.oRequestAnimationFrame || 

            function(callback){window.setTimeout(callback,100);};

    })();

})();//end of autoexecutable (anonimous function) game

Link to comment
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...
 Share

  • Recently Browsing   0 members

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