Hey there everyone
I'm a bit troubled with something I'm trying to construct, I'm trying to create an object that can reproduce itself, creating more objects that can be accessed also, to reproduce themselves. Each object would then be given a random name like: object1, object2 so they can be accessed to create object3, object4, object5 and object6.

I've come up with the following code, which has some segments that you might find weird but those are just testing.

function cMicrobe() {
	var prInterval = 1.2;
	var prNumber = 1;
	
    this.puX = 50;
    this.puY = 50;
    
    this.move = function() {
        this.puX += Math.floor(Math.random()*11);
        this.puY += Math.floor(Math.random()*11);
    }
    
    this.reproduce = function(name) {
		prNumber += prNumber;
		
		name = new cMicrobe();
	}
	
	this.report = function() {
		document.write("x: " + this.puX + "<br />y: " + this.puY + "<br /> <br />");
	}
}

oMicrobe = new cMicrobe();
oMicrobe.report();
oMicrobe.move();
oMicrobe.report();


oMicrobe.reproduce("test");
test.report();

I'm quite stuck here, I don't know the keywords to search on Google for this so I can't figure out how I can make an additional object, named test. If I can make test I can make object2, etc.

I'd appreciate some help on this,
thanks

this.reproduce = function(name) {
		prNumber += prNumber;
		
		window[name] = new cMicrobe();
	}

Hey thanks, that's great! It now creates extra instances of the same class. What I'd like to do next is for this to go automatically, say every 5 seconds or so an extra instance gets created of of every microbe. So it starts growing exponentially.

I now have this code:

/*
 * main.js
 * 
 * */

function cMicrobe() {
	var prInterval = 1.2;
	var prNumber = 1;
	
    this.puX = 50;
    this.puY = 50;
    
    this.move = function() {
        this.puX += Math.floor(Math.random()*11);
        this.puY += Math.floor(Math.random()*11);
        
        this.paint();
    }
    
    this.reproduce = function(name) {
		prNumber += prNumber;
		
		window[name+prNumber] = new cMicrobe();
	}
	
	this.paint = function() {
		document.write("<div style=\"position: absolute; margin: "+ this.puX +"px 0px 0px "+ this.puY +"px; \"><img src=\"bug.png\" /></div>");
	}
}

oMicrobe = new cMicrobe();
oMicrobe.move();


oMicrobe.reproduce("Microbe");
Microbe2.move();

oMicrobe.reproduce("Microbe");
Microbe3.move()
Microbe2.reproduce("Microbe");
Microbe4.move();

I can't really use setInterval(), is there anything I can use for this?

BreachJar,

I think you want something like the code below. It certainly grows exponentially, trouble being that it grabs my processor (less than 1GHz) so effectively that the onscreen counter becomes sluggish from about 1500 and the "stop" button takes quite a while to respond. And that's with all the movement commented out!

You will see I had to include a sudden-death limit determined by the variable maxCount.

You will do better with a faster machine but with some DOM manipulation to give a visual effect per microbe you may find that a javascript/dom based solution is impractical.

Java applet stands a better chance of giving decent performance.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Airshow :: Untitled</title>
<style type="text/css">
{}
</style>

<script>
var mCount = 0;
var maxCount = 3000;

var allowReproduction = true;
var allowMovement = true;
function cMicrobe() {
	var prInterval = 1.2;
	var prNumber = 1;
    this.puX = 50;
    this.puY = 50;
	this.move = function() {
		if(allowMovement){
			this.puX += Math.floor(Math.random()*11);
			this.puY += Math.floor(Math.random()*11);
			this.paint();
			var delay = Math.round(Math.random() * 2000) + 3000;
			setTimeout(function(){that.move()}, delay);
		}
    }
	this.paint = function() {
		document.write("<div style=\"position: absolute; margin: "+ this.puX +"px 0px 0px "+ this.puY +"px; \"><img src=\"bug.png\" /></div>");
	}
    this.reproduce = function(){
		if(allowReproduction){
			new cMicrobe();
			var delay = Math.round(Math.random() * 2000) + 3000;
			setTimeout(function(){that.reproduce()}, delay);
		}
	}
	var that = this;
	mCount += 1;
	if(mCount >= maxCount) { stop(); }
	document.getElementById('mCount').innerHTML = mCount;
	var delay = Math.round(Math.random() * 2000) + 3000;
	if(allowReproduction){ setTimeout(function(){that.reproduce()}, delay); }
//	if(allowMovement){ this.move(); }
}
function stop(){
	allowReproduction = false;
	allowMovement = false;
	document.getElementById('mCount').innerHTML = mCount;
}

onload = function(){
	new cMicrobe();
}
</script>
</head>

<body>

<div id="mCount"></div>
<input type="button" value="Stop" onclick="stop()">

</body>
</html>

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.