Jump to content

Bullets Question


Ninjadoodle
 Share

Recommended Posts

Hi @enpu

I don't want to be a pain, but I'm really having trouble getting this to work. I can't really find any relevant examples for this.

***

1 - I have a rotating ship that fires lasers at the appropriate angle.

2 - The lasers get destroyed if they don't hit anything within 2 seconds.

3 - I have enemy ships that spawn at random locations every 2 seconds.

***

The problem I have is that I can't seem to get the lasers to hitTest (circle) against the enemy ships.

Do I need to put the lasers and ships in respective arrays in order to get them to hitTest against each other?

I really have no idea about how to approach this lol.

Any help/direction would be awesome :)

Link to comment
Share on other sites

Hi @enpu

Sorry, I didn't want to polute the forum with my questionable code lol.

game.createClass('PlayerShip', {
    
    init: function(x, y) {
        
        this.sprite = new game.Sprite('spinvader.png');
        
        this.sprite.position.set(x, y);
        this.sprite.anchorCenter();
        this.sprite.addTo(game.scene.mg);
        
        game.Tween.add(this.sprite, {
            alpha: 1
        }, 1000, {
            easing: 'Linear.None'
        }).start();
    }
});



game.createClass('Spinvader', {
    
    init: function(x, y) {
        
        this.sprite = new game.Sprite('spinvader.png');
        
        this.sprite.position.set(x, y);
        this.sprite.anchorCenter();
        this.sprite.addTo(game.scene.mg);
        
        game.Tween.add(this.sprite, {
            alpha: 1
        }, 1000, {
            easing: 'Linear.None'
        }).start();
    }
});



game.createClass('Laser', {
    
    init: function() {
        
        this.sprite = new game.Sprite('spinvader.png');
        
        this.sprite.position.set(game.scene.playerShip.sprite.x, game.scene.playerShip.sprite.y);
        this.sprite.anchorCenter();
        this.sprite.scale.set(0.5);
        this.sprite.rotation = game.scene.playerShip.sprite.rotation;
        this.sprite.addTo(game.scene.mg);
        
        game.Timer.add(2000, function() {
            this.remove();
        }.bind(this.sprite));
    },
    
    update: function() {
        
        this.sprite.position.x += game.scene.laserSpeed * Math.cos(this.sprite.rotation - (0.5 * Math.PI)) * game.delta;
        this.sprite.position.y += game.scene.laserSpeed * Math.sin(this.sprite.rotation - (0.5 * Math.PI)) * game.delta;
        
        var distance = this.sprite.position.distance(game.scene.spinvader.sprite.position);
        if (distance < 128 + 64) {
            game.scene.spinvader.sprite.alpha = 0.5;
        }
    }
});



// ***** MAIN CODE *****
game.createScene('Main', {
    
    num: 0,
    wave: 3,
    time: 2000,
    
    laserSpeed: 500,
    shipSpeed: 0.025,
    spinning: false,
    stopped: false,
    dir: 2,
    
    init: function() {
        
        this.stageSetup = new game.StageSetup();
        this.stageSetup.setupStage();
        this.stageSetup.containers();
        
        this.playerShip = new game.PlayerShip(640, 640);
        
        game.Timer.add(this.time, function() {
            
            if (game.scene.num < game.scene.wave) {
                game.scene.spinvader = new game.Spinvader(Math.random()*1000, 100);
                game.scene.num ++;
            }
            
        }, true);
    },
    
    update: function() {
        
        this.stageSetup.updateStage();
        
        if (this.spinning) {
            
            if (this.dir === 1) {
                this.playerShip.sprite.rotation += this.shipSpeed * Math.PI;
            } else {
                this.playerShip.sprite.rotation -= this.shipSpeed * Math.PI;
            }
            
        }
    },
    
    mousedown: function() {
        
        if (!this.spinning) {
            
            if (this.dir === 2) {
                this.dir = 1;
            } else {
                this.dir = 2;
            }
            
            this.spinning = true;
            
        } else {
            
            this.spinning = false;
            this.laser = new game.Laser();
        }
    },
});

});

 

Link to comment
Share on other sites

@enpu - It acts the same when I drop it right down. I'm pretty sure I'm doing something wrong. Sometimes the enemy will turn transparent even if the laser is nowhere near. Also, only one of the 3 enemy ships ever gets 'hit'.

I don't want to waste your time on this - I'll try to do some digging around. I guess I'm just wondering if any special treatment is required multiple spawned enemies and bullets.

Thanks!

Link to comment
Share on other sites

@enpu

The hitTest work correctly now, but like you said only the last invader gets hit (because I'm overriding them).

The issue is that I have multiple lasers and multiple invaders - so I would need two arrays and cross check them against each other (way over my head).

I'm going to try and do some research on how to do that lol.

Thank you!

Link to comment
Share on other sites

If you do the check in the lasers update function, then you don't need two arrays. Just one array where you store all your enemies.

game.createClass('Laser', {
    update: function() {
        for (var i = 0; i < game.scene.enemies.length; i++) {
            var enemy = game.scene.enemies[i];
            var distance = this.sprite.position.distance(enemy.sprite.position);
            if (distance < laserRadius + enemyRadius) {
                enemy.sprite.alpha = 0.5;
            }
        }
    }
});

game.createScene('Main', {
    enemies: [],
    
    addEnemy: function() {
        var enemy = new game.Enemy();
        this.enemies.push(enemy);
    }
});

 

Link to comment
Share on other sites

Or if you want to store the lasers also in array you could do it like this:

game.createClass('Laser', {
    check: function() {
        for (var i = 0; i < game.scene.enemies.length; i++) {
            var enemy = game.scene.enemies[i];
            var distance = this.sprite.position.distance(enemy.sprite.position);
            if (distance < laserRadius + enemyRadius) {
                enemy.sprite.alpha = 0.5;
            }
        }
    }
});

game.createScene('Main', {
    lasers: [],
    enemies: [],
    
    addLaser: function() {
        var laser = new game.Laser();
        this.lasers.push(laser);
    },
    
    addEnemy: function() {
        var enemy = new game.Enemy();
        this.enemies.push(enemy);
    },
    
    check: function() {
        for (var i = 0; i < this.lasers.length; i++) {
            this.lasers[i].check();
        }
    }
});

Then when you call game.scene.check() every laser gets checked against every enemy

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...