Sign in to follow this  
airpower58

Help, Can some one please help me with adding power ups to my game

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.