Ninjadoodle Posted March 21, 2018 Share Posted March 21, 2018 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 Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2018 Share Posted March 21, 2018 @Ninjadoodle Simple code example would help a lot Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 21, 2018 Author Share Posted March 21, 2018 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(); } }, }); }); Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2018 Share Posted March 21, 2018 @Ninjadoodle Did not spot anything wrong with your code. Does it work if you drop down the laserSpeed? I'm suspecting that maybe it's going so fast that it really doesn't overlap the enemy in any frame. Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 21, 2018 Author Share Posted March 21, 2018 @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! Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2018 Share Posted March 21, 2018 I think there was bug in the distance function. Can you update and try again? Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2018 Share Posted March 21, 2018 Also in your code: game.scene.spinvader = new game.Spinvader(Math.random()*1000, 100); That is overwriting the previous Spinvader instance. You might want to instead store them all in array, and then check the laser position against all the instances. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 21, 2018 Author Share Posted March 21, 2018 @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! Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2018 Share Posted March 21, 2018 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); } }); Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
enpu Posted March 21, 2018 Share Posted March 21, 2018 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 Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 21, 2018 Author Share Posted March 21, 2018 @enpu - This is too easy for you lol. I really appreciate the help and code snippets, would have taken me like two days to figure out! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.