Jump to content

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


airpower58
 Share

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

Link to comment
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.

Link to comment
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.

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...