0

First time posting here, and first time creating a game in Javascript.

I'm trying to create collision between two objects in my game, a bullet and an enemy, but for now, just trying to make something happen with just the bullet, when it reaches the top of the screen.

When I press E, this happens:

if (69 in keysDown && timer == 0) //E
     {  
    var newbullet = Object.create(bullet);
    newbullet.posx = ship.playerx;
    newbullet.posy = ship.playery;
    projectiles.push(newbullet);
    ship.shoot = true;
     }

The bullet then moves upwards as it is stated in its update function.

I'm running this function constantly in my game loop, which checks for collisions, and that looks like this:

function Collision()
{
        for (i = 0; i <= projectiles.length; i++)
        {
            if (bullet.posy < 0 )
            {
                ctx.fillText("HIT" , 160, 340);
                ship.health -= 1;
            }
        }
}

but it doesn't work. I thought of substituting "bullet.posy" with "projectiles[i].posy, but it ends up saying that projectiles[i] is undefined.

projectiles is a global array.

var projectiles=[];

This is bullet:

var bullet = 
    {
        posx:0,
        posy:0,
        speed: 10,
        power: 2,

        draw: function()
        {
            ctx.fillStyle = "rgb(200,100,0)";
            ctx.beginPath();
            ctx.rect(((this.posx - 5)), (this.posy - 30), 10, 25);
            ctx.closePath();
            ctx.fill();
        },

        setup: function(ax, ay)
        {
            this.posx = ax;
            this.posy = ay;
        },
        update: function()
        {
            this.posy -= this.speed;
        }
    };

Any help or advice?

Here is the link, if you wish to try it out E to shoot.

Thank you.

8
  • 2
    what is the value of bullet? What is the value of projectiles? Does the value of bullet.posy change? Commented Apr 3, 2015 at 12:59
  • Please see "Should questions include “tags” in their titles?", where the consensus is "no, they should not"! Commented Apr 3, 2015 at 13:11
  • 2
    Can you create a quick demo with the issue? Commented Apr 3, 2015 at 13:14
  • Try changing var newbullet = Object.create(bullet); to var newbullet = {}; or maybe var newbullet = bullet; would be better. Object.create() expects a prototype, not an object literal, to be passed in. Commented Apr 3, 2015 at 13:15
  • 1
    for (i = 0; i < projectiles.length; i++) Commented Apr 3, 2015 at 13:19

1 Answer 1

2

The first part seems seemed clear: A new bullet Object called newbullet is invoked with the value of the keys .posx and .posy being set by ship.playerx and ship.playery. newbullet is then stored in the projectiles array.

The rest is not so clear. As you have it the if-condition of the for-loop in Collision() appears to be referencing the .posy of the new bullet() constructor, not the .posy of the object that's just been created (newbullet). You'll also want to iterate the for-loop projectiles.length - 1 times as arrays are zero indexed : so use the < operator and not the <= operator in the for-loop.

Assuming ship.playery assigns a number value, perhaps try assigning a variable to each item in the 'projectiles' array as it passes through Collision()'s for-loop...

function Collision() {
    var projLength = projectiles.length; // slight optimisation to for-loop
    for (i = 0; i < projLength; i++) {
        var thisBullet = projectiles[i]; // reassign variable each iteration
        if (thisBullet.posy < 0 ) {
            ctx.fillText("HIT" , 160, 340);
            ship.health -= 1;
        }
    }
}

EDIT: in light of updated question

Being as you have the projectiles array in which to store your bullets, you can easily turn the bullet literal object into a contructor and make as many as you like/need/allow. Your code will then follow the description in my first para and look something like...

var projectiles = [];

var Bullet = function(x, y) {
    this.posx: x,
    this posy: y,
    speed: 10,
    power: 2,
    draw: function() {
        ctx.fillStyle = "rgb(200,100,0)";
        ctx.beginPath();
        ctx.rect(((this.posx - 5)), (this.posy - 30), 10, 25);
        ctx.closePath();
        ctx.fill();
    },
    setup: function(ax, ay) {
        this.posx = ax;
        this.posy = ay;
    },
    update: function() {
        this.posy -= this.speed;
    }
};

/* --- */

if (69 in keysDown && timer == 0) { // E  
    var bullet = new Bullet(ship.playerx, ship.playery);
    projectiles.push(bullet);
    ship.shoot = true;
}

/* --- */

function Collision() {
    // as above ....
}

... it also means you won't have to wait for a bullet to reach y < 0 before another can be created. RAPID FIRE yay!!!

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

10 Comments

There isn't a new bullet constructor. He never wrote one. All he has is a single object literal. There's only ever one instance of it, which he's reusing.
Ah. OK. I just assumed the new Object() had a constructor because he was storing it in a pluaralised array (projectiles not projectile), and I see the question has been edited since I started typing.
Yep. This is one of those questions where there's a lot going on.
Ignore the above then. Whoever kindly upvoted it should probably downvote it now.
Sorry, I know this is difficult to work with, I didn't want to post the entire code. Everything looked like it was fine, until I tried to do collisions and stuff. If you want to see the whole code, it's here. focserver.londonmet.ac.uk/cu4005-1415-25/spacescript.js
|

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.