Jump to content

How to create random objects and destroy them?


menzoni
 Share

Recommended Posts

Hey, Guys. I'm new to Canvas and javascript and I'm making a endless runner like Traffic Racer. I'm already able to move my player and I created a object, which is supposed to become an obstacle. But since it is an endless runner I need to be able to create a bunch of random objects at time and destroy them when they're no longer visible. 

Screen Shot 2016-07-30 at 2.54.08 AM.png

I wanted the objects to be able to destroy themselves, but I can't find a way how to do this. This is my class "Banana

function Banana() {
	this.height = 1.96;
	this.width = 3.955;
	this.pos_x = 300;
	this.pos_y = -30;
	this.banana_image = new Image();
	this.banana_image.src = 'img/banana.png';
};

I was thinking about creating an array of objects that are on screen, but I still don't know how to create them on runtime.

Anything helps.

Thanks. 

Link to comment
Share on other sites

You could implement a pool of obstacle objects: When you need a new obstacle you request it from the free pool. If the free pool is empty it should create a new obstacle and return it. When an obstacle is no longer needed you send it back to the free pool where it is ready to be reused.

Link to comment
Share on other sites

Thank you for your help, @BobF

What would this pool of objects be? And array? I tried some stuff, and I got stuck with this (which doesn't work):

Game.update = function() {

	  this.player.move();
	  
	  //creating bananas
	  if (objs.length <= 0) {
		  this.banana = new Banana();
	  } else {
		  for (i = 0; i < 10; i++) {
			  objs.push(new Banana());
		  }  
	  }
	  
	  //moving bananas
	  for (i = 0; i < objs.length; i++) {
		  this.objs[0].move();
	  }
	  

};

I created an Objects array and I made a For loop to create the objects, but besides not working it only would be able to create 10 objets, and I don't want it. I never used a pool of objects. Do you have any tip on where to start? 

Link to comment
Share on other sites

Here's an example of a very simple object pool and an incomplete example of its use:


var pool = (function () {
    var obstacles = [];

    return {
        get: function () {
            return obstacles.pop() || new Obstacle();
        },

        free: function (obstacle) {
            obstacles.push(obstacle);
        }
    };
})();


function moveBananas() {

    if (needNewBanana) {
        var banana = pool.get();

        banana.move();
    }

    if (bananaOutOfView) {
        pool.free(banana);
    }

}

 

Link to comment
Share on other sites

  • 3 weeks later...

Put a life on  your banana object :)

 

var Banana = (function () {

        function Banana( pTime, pLife ) {
	    this.height = 1.96;
	    this.width = 3.955;
	    this.pos_x = 300;
	    this.pos_y = -30;
	    this.banana_image = new Image();
	    this.banana_image.src = 'img/banana.png';

	    this._TimeToDestroy = pTime + pLife; 
        }

	Banana.prototype.Destroy = function () {

                this.banana_image     = null;
                this.banana_image.src = null;
            };        

        Rect.prototype.IsTimeToKill = function (pTime) {
                return pTime >= this._TimeToDestroy ? true; false;
            };
}




Game.update = function() {

            var mTime = new Date().getTime();
          
	    if (objs.length <= 0) {
		  this.banana = new Banana( mTime, 3000 ); // Lives 3 Seconds
	    } else {
		  for (i = 0; i < 10; i++) {
			  objs.push(new Banana( mTime, 3000 )); // Lives 3 Seconds
		  }  
	    }

	    var mItr  = 0;
            var mLen  = this.objs.length;
            var mObj  = null;

            for (mItr = mLen - 1; mItr >= 0; mItr--) {

                mObj = this.objs[ mItr ];                

                //--> Move object
                //
                mObj.move();                

		//--> Inquire if time to destroy object
                //
		if mObj.IsTimeToKill( mTime ) {

                     // Destroy object
		     mObj.Destroy();
		     mObj = null;

                     // Remove from list
                     this.objs[mItr] = null;
                     this.objs[mItr].splice( mItr, 1 );
                }
            }
}

 

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