在破坏时给予物体不同的点

时间:2015-02-28 10:09:20

标签: javascript

我希望我们的敌人在被摧毁时给予每个敌人不同的分数。

所以我有这个

//enemies
var enemies = [],
        enemy_x = 50,
        enemy_y = -45,
        enemy_w = 50,
        enemy_h = 50,
        speeds = {"enemy0": 1, "enemy1": 1, "enemy2": 1, "enemy3": 1, "enemy4": 1, "enemy5": 1},
        hitpoints = {"enemy0": 5, "enemy1": 6, "enemy2": 7, "enemy3": 8, "enemy4": 9, "enemy5": 10},
        score = {"enemy0": 5, "enemy1": 6, "enemy2": 7, "enemy3": 8, "enemy4": 9, "enemy5": 10};




function _Enemy($x, $y, $w, $h, $s, $i, $hitpoints) {
        this.x = $x;
        this.y = $y;
        this.w = $w;
        this.h = $h;
        this.speed = $s;
        this.image = $i;
        this.hitpoints = $hitpoints;    
}

// Iterate through array of enemies and draw them on the canvas
function drawEnemies() {
        for (var i = 0; i < enemies.length; i++) {
                ctx.drawImage(enemies[i].image, enemies[i].x, enemies[i].y);
                // Or you can use
                // ctx.drawImage(enemies[i]["image"], enemies[i]["x"], enemies[i]["y"]);
        }
}


// Iterate through array of enemies and update their position
function moveEnemies() {
        for (var i = 0; i < enemies.length; i++) {
                if (enemies[i].y < height) {
                        enemies[i].y += enemies[i].speed;
                } else if (enemies[i].y > height - 1) {
                        // after the enemies scroll off screen, this value is where they get re-spawned
                        enemies[i].y = -45;
                }
        }
}

// Check for laser collision
function hitTest() {
        var remove = false;
        var laserremove = false;
        for (var i = 0; i < lasers.length; i++) 
        {
                for (var j = 0; j < enemies.length; j++) 
                {
                        // laser's y position is less than or equal to the enemy's y position plus its height -AND-
                        // laser's x position is greater than or equal to the enemy's x position -AND-
                        // laser's x position is less than or equal to the enemy's x position plus it's width
                        var laser = lasers[i];
                        var enemy = enemies[j];
                        if (laser[1] <= ( enemy.y + enemy.h ) && laser[0] >= enemy.x && laser[0] <= ( enemy.x + enemy.w)) 
                        {
                                laserremove = true;
                                enemy.hitpoints = enemy.hitpoints - 1;
                                if (enemy.hitpoints == 0) 
                                {

                                        enemies.splice(j, 1);
                                        remove = true;
                                        var $r = Math.floor(Math.random() * 5),
                                        enemy = new Image();
                                        enemy.src = 'enemy' + $r + '.png';

                                        enemies.push(new _Enemy((Math.random() * 450 + 50), -45, enemy_w, enemy_h, speeds["enemy" + $r], enemy, hitpoints["enemy" + $r]));
                                }


                        }


                }
                if (remove == true) {

                        lasers.splice(i, 1);
                        score += 10;                        // laser at this array position no longer gets drawn
                        remove = false;

                }
                 if (laserremove == true) {
                        lasers.splice(i, 1);  // laser at this array position no longer gets drawn
                        laserremove = false;

                }

        }
}


(function ($a) {
        //Build enemy array of x/y coordinate, width, height, and speed
        for (var i = 0; i < $a; i++) {
                var $r = Math.floor(Math.random() * 5),
                        enemy = new Image();
                enemy.src = 'enemy' + $r + '.png';

                enemies.push(new _Enemy(enemy_x, enemy_y, enemy_w, enemy_h, speeds["enemy" + $r], enemy, hitpoints["enemy" + $r]));
                enemy_x += enemy_w + 60;
        }
})(5); // Here you can pass in the number of enemies to create

所以在顶部我得分,然后每个敌人应该给予什么。所以像敌人0应该给5分,敌人1应该给6分等。

现在添加右下角,当敌人被摧毁时,它只会增加10。但这不是我想要的方式

有人知道怎么做吗?

0 个答案:

没有答案