SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to stop instance methods running multiple times

    i am creating a shooting game on canvas, i have a projectile object - when a user clicks a new instance of this object is made. and the method to move the projectile is called - projectiles[i].move();

    I have added the instances to an array so i can count through it each time to keep each instance moving. however it seems when the method for one instance is called twice that projectiles speed is increased. how can i stop this from happening. here is the code

    Code JavaScript:
    function fire(){ //trigger projectile object and create new
    	window.onmousedown = function(e){
    		trigger = true;
    		if(e.offsetX){
    			mouseX = e.offsetX;
    			mouseY = e.offsetY;
    		}else 
    		if(e.layerX){
    			mouseX = e.layerX;
    			mouseY = e.layerY;
    		}	
    		projectiles[pC] = new projectile((top_canvas.width/2) + main_x, (top_canvas.height/2) + main_y, mouseX - main_x - (top_canvas.width/2), mouseY - main_y - (top_canvas.height/2), turn);	
    		pC++;
    		zero = 0;
     
    	}
    	if(trigger == true){
    		for(i = 0; i<pC; i++){
     
    				projectiles[i].move();
    		}	
    	}
    }
     
    //construct bullet object
    function projectile(x,y, mx, my, turn){
    	this.x_speed = 1;
    	this.y_speed = 1;
    	this.x = x;
    	this.y = y;
    	this.turn = turn;
     
    	ctx_2.fillStyle = "#000";
    	this.move = move;
    	function move(){
    		ctx_2.save();
    			ctx_2.clearRect(0,0,top_canvas.width,top_canvas.height);
    			ctx_2.translate(this.x, this.y);			
    			ctx_2.rotate(this.turn - 1.570796325);
    			ctx_2.fillRect(-25, zero+=this.y_speed, 50, 50);
    		ctx_2.restore();
    		document.getElementById("info").innerHTML = "pc:" + pC + " i:" + i + " yspeed:" + this.y_speed;
    	}
    }

    sorry if i am not clear enough, i will try and explain more if needed

    Thanks!

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function fire(){ //trigger projectile object and create new
    	window.onmousedown = function(e){
    		trigger = true;
    		if(e.offsetX){
    			mouseX = e.offsetX;
    			mouseY = e.offsetY;
    		}
            else if(e.layerX){
    			mouseX = e.layerX;
    			mouseY = e.layerY;
    		}
            if (!projectiles[pC]){
            projectiles[pC] = new projectile((top_canvas.width/2) + main_x, (top_canvas.height/2) + main_y, mouseX - main_x - (top_canvas.width/2), mouseY - main_y - (top_canvas.height/2), turn);
    	     pC++;
    		 zero = 0;
            }
           }
           if(trigger == true){
    		for(i = 0; i<pC; i++){
             projectiles[i].move();
    		}
           }
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •