Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

PRO members can see and edit the Pen thumbnail here after the Pen has been saved.

HTML

              
                <footer>
  <div id=version></div>
</footer>
              
            
!

CSS

              
                html,
body {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  background: #111;
  color: #eee;
  font: caption;
}

#version {
  position: absolute;
  left: 0;
  top: 0;
  padding: 1px 2px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
}

              
            
!

JS

              
                /* global Phaser */

document.getElementById("version").textContent = "Phaser v" + Phaser.VERSION;

var config = {
  type: Phaser.AUTO,
  scale: {
    width: 800,
    height: 600,
    autoCenter: Phaser.Scale.CENTER_BOTH
  },
  scene: {
    init: init,
    preload: preload,
    create: create,
    update: update
  },
  loader: {
    baseURL:
      "https://raw.githubusercontent.com/dino-foot/phasersandbox/main/public/",
    crossOrigin: "anonymous"
  }
};

var game = new Phaser.Game(config);

function init() {
  score = 0;
  gameOver = false;
}

function preload() {
  this.load.image(
    "sky",
    "https://labs.phaser.io/src/games/firstgame/assets/sky.png"
  );
}

function create() {
  //  A simple background image
  const bg = this.add.image(400, 300, "sky");
  bg.setDepth(-1);

  // instantiate healthbar object with spawn position and max health
  const healthBar = new HealthBar(this, 100, 100, 1000);
  // apply damage for testing
  healthBar.getDamage(500);
}

function update() {
  // do things here
}

class HealthBar extends Phaser.GameObjects.Graphics {
  constructor(scene, x, y, max = 1000) {
    super(scene);
    this.bar = this; // the Graphics object
    this.x = x; // X coordinate of the healthbar
    this.y = y; // Y coordinate of the healthbar
    this.value = max; // current healthbar value (initialize with max value)
    this.percentage = 238 / max; // get the percentage
    this.maxHP = max; // set max healthbar
    this.draw(); // draw the healthbar
    this.setDepth(1); // set depth so it is always on top
    scene.add.existing(this); // add to the scene
  }

  draw() {
    this.bar.clear(); // clear the graphics
    this.percentage = 238 / this.maxHP;
    // background
    this.bar.fillStyle(0xa3a3a3); // grey
    this.bar.fillRoundedRect(this.x, this.y, 240, 16, 2);
    // border
    this.bar.lineStyle(2, 0x000000, 1);
    this.bar.strokeRoundedRect(this.x, this.y, 240, 16, 2);
    // health color
    this.bar.fillStyle(0x74e291); // green

    const dx = Math.floor(this.percentage * this.value);
    this.bar.fillRoundedRect(this.x + 2, this.y + 2, dx, 12, 2);
  }

  // apply damage and get current health value
  getDamage(amount) {
    this.value = amount;

    if (this.value < 0) {
      this.value = 0;
    }

    this.draw();
    return this.value === 0;
  }

  // return current health value
  getCurrentHealth() {
    return this.value;
  }
}

              
            
!
999px
Keep friends close and formatters closer.

Console