简体   繁体   中英

(p5js/javascript) Got an error trying to pass in 2 objects into a function: Uncaught TypeError: object is undefined

I am trying to pass 2 objects into a function to see if they would collide, and destroy them both if they do. it was working before i added the collide function. tried it first by calling the attribute from the object, then again with accessors, but i couldn't find out whats wrong with it. indicated below the line with the error! would appreciate any help. Thank you.

Edit: included my full code, there are some unused variables in the objects which is why i didnt include it at first

Edit 2: tried adding a check if (typeof this.missile != 'undefined') before the code, but it just lags out the game...

Edit 3: really puzzled, collide(missiles[i], enemies[k], i, k); in the main draw() function wouldn't be called if there are no missile in the list of missiles. It gets called, it means there is a missile in the list, so why is it undefined? Getting desperate...


var missiles;
var hero;
var enemies;
function setup()
{
    createCanvas(1024, 512);
    hero = new hero(1, 0)
    enemies = [];
    missiles = [];
}

function draw()
{
    background(0);
    hero.draw();
    enemies.push(new enemy(512, 50, 1));
    enemies.push(new enemy(700, 50, 1));

    for (var i = 0; i < missiles.length; i++)
    {
        missiles[i].draw();
    }    

    for (var i = 0; i < enemies.length; i++)
    {
        enemies[i].draw();
    }

    for (var i = 0; i < missiles.length; i++)
    {
        for (var k = 0; k < enemies.length; i++)
        {
            collide(missiles[i], enemies[k], i, k); 
        }
    }
}

function hero(weaponLevel, wingmanLevel)
{
    this.weapon = weaponLevel;
    this.firerate =  5000 - (this.weapon - 1) * 50;
    this.wingman = wingmanLevel;
    this.timer = 0;

    this.draw = function()
    {
        fill(255);
        ellipse(mouseX, mouseY, 15);
        this.update();
    }

    this.update = function()
    {
        if (millis() > this.timer)
        {
            this.fire();
            this.timer = this.timer + 500;
        }
    }

    this.fire = function()
    {
        missiles.push(new missile(mouseX, mouseY, this.weapon));
    }
}

function missile(x, y, weaponLevel)
{
    this.x = x;
    this.y = y - 5;
    this.speed = 5;

    this.update = function()
    {
        this.y -= this.speed;

        if (this.y + 5 < 0)
        {
            var i = missiles.indexOf(this);
            missiles.splice(i, 1);
        }
    }

    this.draw = function()
    {
        this.update();
        stroke(255);
        line(this.x, this.y, this.x, this.y + 5);
    }

    this.getX = function()
    {
        return this.x;
    }

    this.getY = function()
    {
        return this.y;
    }
}

function enemy(x, y, level)
{
    this.x = x;
    this.y = y;
    this.health = level * 10 + 100;
    this.currentX = x;
    this.currentY = y;
    if (random(0, 100) < 3)
    {
        if (random(0, 100) < 50)
        {
            this.dropsUpgrade = true;
            this.dropsWingman = false;
        }
        else
        {
            this.dropsUpgrade = false;
            this.dropsWingman = true;
        }
    }

    this.draw = function()
    {
        rectMode(CENTER);
        fill(255,0,0);
        rect(this.x, this.y, 15, 15);
    }

    this.getX = function()
    {
        return this.currentX;
    }

    this.getY = function()
    {
        return this.currentY;
    }
}

function collide(missile, enemy, i, k)
{
    this.missile = missile;
    this.enemy = enemy;
    this.xDist = dist(this.missile.getX(), this.enemy.getX()); //Uncaught TypeError: this.missile is undefined
    this.yDist = dist(this.missile.getY(), this.enemy.getY());

    if (xDist < 15 && yDist < 15)
    {
        missiles.splice(i, 1);
        enemies.splice(k, 1);
    }
}

Got it...

  1. for loop variables are wrong (used i instead of k for inner for loop)
  2. dist function used incorrectly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM