0

the following is my code,

function hostile(x, y) {
        this.speed = 1;
        this.health = 100; 
        this.x = x; 
        this.y = y; 
        this.height = 32;
        this.width = 32;
        this.isDead = false;
        this.direction = 0;

        this.move = function(){
             context.clearRect(0,0,canvas1.width,canvas1.height); 
             if (this.x > canvas.width - 64) {

                 this.y += 10;
                 this.direction = 0;
             }
             if (this.x < 0) {
                 this.y += 10;
         }

             if (this.direction === 1) {
                 this.x += this.speed;
         } else {
               this.x -= this.speed;
         }
             if (this.x < 0) {
               this.direction = 1;
             }

             if (this.y > 420) {
             //this might have to be changed
                 this.x = 600;
             }
         }  
    };
//CREATING AN INSTANCE OF HOSTILE, THIS ISN'T WORKING FOR MULTIPLE INSTANCES, BUT WHY?

var hostile = new hostile(20,20);
var hostileA = new hostile(20,20);

I have hostile created and I have this instance being called in the update method, hostile.move() however the var hostile works, the var hostile does not, I have checked the code hostile is the only reference in the file.

1
  • BTW, jshint.com would have caught this for you. Commented Oct 23, 2013 at 14:09

2 Answers 2

2
var hostile = new hostile(20,20);

You just overwrote the hostile variable to refer to that instance rather than the constructor.

This is one of the reasons that constructors are UpperCamelCase by convention

Sign up to request clarification or add additional context in comments.

Comments

1

You're erasing your constructor with

var hostile = new hostile(20,20);

Then you can't create other hostile objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.