Skip to content

Instantly share code, notes, and snippets.

Created May 16, 2012 04:49
Show Gist options
  • Save anonymous/2707518 to your computer and use it in GitHub Desktop.
Save anonymous/2707518 to your computer and use it in GitHub Desktop.
a guest on May 15th, 2012 - this.x in setValues is a number. In update it's NaN
<!DOCTYPE html>
<html>
<head>
<title> test </title>
</head>
<body>
<canvas id="myCanvas" width="512" height="512" style="border:1px solid #c3c3c3;">Your browser is bad and you should feel bad (no canvas support)</canvas>
<br> testing
<script type="text/javascript">
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var WIDTH=c.width, HEIGHT=c.height;
function init(){
}
function clear() {
ctx.clearRect(0, 0, WIDTH, HEIGHT);
}
function particle() {
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
this.radius = 10;
this.color = "#0000FF";
this.currentTime = 0;
this.maxTime = 0;
this.alive = true;
this.setValues = function(x,y,vx,vy,r,color,life){
alert(this.x);
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = r;
this.color = color;
this.maxTime = life;
//alert(this.x);
}
this.draw = function() {
clear();
if(this.alive == true){
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.fillStyle="#FF0000";
ctx.fill();
//alert(this.x);
}
}
this.update = function() {
if(this.alive == true){
alert(this.x);
this.x = this.x + this.vx;
this.y = this.y + this.vy;
this.currentTime += 1;
if(this.maxTime!=0){
if(this.currentTime>=this.maxTime) {
this.alive = false;
}
}
}
this.draw();
}
}
var particles = [];
function initParticles(){
for(var i=0; i<10; i++){
particles.push(new particle());
particles[i].setValues(Math.random()*50,Math.random()*50,Math.random*10,Math.random()*10,"#0000FF",0);
}
}
function updateParticles() {
for(var i=0; i<10; i++){
particles[i].update();
}
}
initParticles();
intervalId=setInterval("updateParticles()",1000/30);
//updateParticles();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment