airpower58 Posted May 28, 2015 Share Posted May 28, 2015 Hello I was wondering how would I add a 10 second invincibility power up in my game where the character can go through the obstacles and not collide with them. I have already made it possible for the character to collect the power up but it just does not do anything yet. Or would it be possible to make the obstacles disappear of the screen for 10 seconds after collecting a power up. Thanks Quote Link to comment Share on other sites More sharing options...
spinnerbox Posted May 28, 2015 Share Posted May 28, 2015 In your character code there would be field called invincible: false which you would set to true when he collects the power up. Each of the obstacles would check if the character is invincible and if yes the damage is ignored, if not than apply damage to the character. But yes all your obstacle should be inheriting from one class, maybe called Obstacle in which there would be if-else block to check if the character is invincible or not. And of course after Timer of 10 seconds is finished set invincible back to false. Quote Link to comment Share on other sites More sharing options...
airpower58 Posted May 29, 2015 Author Share Posted May 29, 2015 I haven't got a field called invincible I just have a collisiongroup and a collideagaints in my code for the character. Could you please give an example of a possible solution to turn of the collision for a few seconds Quote Link to comment Share on other sites More sharing options...
spinnerbox Posted May 29, 2015 Share Posted May 29, 2015 Here is a piece of non working code but you get the idea:var limit = 30, counter = 0;var character = (function () { var health = 100; return { invincible: false; doDamage: function (damagePoints) { health -= damagePoints; } };}());var powerUp = function (x, y) { // invincible power up};var timer = new Timer(10);timer.addEventListener(function () { character.invincible = false;});var obstacleDamagePoints = 5;var Obstacle = function (x, y) { return { // obstacle fields, methods };};var obstacles = [];for (var i = 0; i < 20; i += 1) { obstacles.push(new Obstacle(x, y));}var gameState = (function () { return { update: function () { if (counter === limit) { if (character.collides(powerUp)) { character.invincible = true; timer.start(); } for (var i = 0; i < 20; i += 1) { if (!character.invincible && character.collides(obstacles[i])) { character.doDamage(obstacleDamagePoint); } } counter = 0; } else { counter += 1; } } };}());update() runs 60 times per second so you can limit the execution in update with a counter. Quote Link to comment Share on other sites More sharing options...
spinnerbox Posted May 29, 2015 Share Posted May 29, 2015 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.