HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html>
<head>
<meta name="description" content="Cloud Platform" />
<title>Phaser Coding Tips Issue 4</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1, IE=9">
<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=0 minimal-ui" />
<script src="//cdn.jsdelivr.net/phaser/2.2.2/phaser.min.js"></script>
</head>
<body>
<div id="game"></div>
<a href="https://phaser.io"><img src="https://files.phaser.io.s3.amazonaws.com/codingtips/issue001/phaser-tips-header1.png" title="Phaser Coding Tips Weekly" style="margin-top: 8px" /></a>
</body>
</html>
var game = new Phaser.Game(640, 480, Phaser.CANVAS, 'game');
var PhaserGame = function () {
this.bg = null;
this.trees = null;
this.player = null;
this.stationary = null;
this.clouds = null;
this.facing = 'left';
this.jumpTimer = 0;
this.cursors;
this.locked = false;
this.lockedTo = null;
this.wasLocked = false;
this.willJump = false;
};
PhaserGame.prototype = {
init: function () {
this.game.renderer.renderSession.roundPixels = true;
this.world.resize(640*3, 480);
this.physics.startSystem(Phaser.Physics.ARCADE);
this.physics.arcade.gravity.y = 600;
},
preload: function () {
// We need this because the assets are on Amazon S3
// Remove the next 2 lines if running locally
this.load.baseURL = 'https://files.phaser.io.s3.amazonaws.com/codingtips/issue004/';
this.load.crossOrigin = 'anonymous';
this.load.image('trees', 'assets/trees-h.png');
this.load.image('background', 'assets/clouds-h.png');
this.load.image('platform', 'assets/platform.png');
this.load.image('cloud-platform', 'assets/cloud-platform.png');
this.load.spritesheet('dude', 'assets/dude.png', 32, 48);
// Note: Graphics are Copyright 2015 Photon Storm Ltd.
},
create: function () {
this.background = this.add.tileSprite(0, 0, 640, 480, 'background');
this.background.fixedToCamera = true;
this.trees = this.add.tileSprite(0, 364, 640, 116, 'trees');
this.trees.fixedToCamera = true;
// Platforms that don't move
this.stationary = this.add.physicsGroup();
this.stationary.create(0, 96, 'platform');
this.stationary.create(632, 220, 'platform');
this.stationary.create(1100, 300, 'platform');
this.stationary.setAll('body.allowGravity', false);
this.stationary.setAll('body.immovable', true);
// Platforms that move
this.clouds = this.add.physicsGroup();
var cloud1 = new CloudPlatform(this.game, 300, 450, 'cloud-platform', this.clouds);
cloud1.addMotionPath([
{ x: "+200", xSpeed: 2000, xEase: "Linear", y: "-200", ySpeed: 2000, yEase: "Sine.easeIn" },
{ x: "-200", xSpeed: 2000, xEase: "Linear", y: "-200", ySpeed: 2000, yEase: "Sine.easeOut" },
{ x: "-200", xSpeed: 2000, xEase: "Linear", y: "+200", ySpeed: 2000, yEase: "Sine.easeIn" },
{ x: "+200", xSpeed: 2000, xEase: "Linear", y: "+200", ySpeed: 2000, yEase: "Sine.easeOut" }
]);
var cloud2 = new CloudPlatform(this.game, 800, 96, 'cloud-platform', this.clouds);
cloud2.addMotionPath([
{ x: "+0", xSpeed: 2000, xEase: "Linear", y: "+300", ySpeed: 2000, yEase: "Sine.easeIn" },
{ x: "-0", xSpeed: 2000, xEase: "Linear", y: "-300", ySpeed: 2000, yEase: "Sine.easeOut" }
]);
var cloud3 = new CloudPlatform(this.game, 1300, 290, 'cloud-platform', this.clouds);
cloud3.addMotionPath([
{ x: "+500", xSpeed: 4000, xEase: "Expo.easeIn", y: "-200", ySpeed: 3000, yEase: "Linear" },
{ x: "-500", xSpeed: 4000, xEase: "Expo.easeOut", y: "+200", ySpeed: 3000, yEase: "Linear" }
]);
// The Player
this.player = this.add.sprite(32, 0, 'dude');
this.physics.arcade.enable(this.player);
this.player.body.collideWorldBounds = true;
this.player.body.setSize(20, 32, 5, 16);
this.player.animations.add('left', [0, 1, 2, 3], 10, true);
this.player.animations.add('turn', [4], 20, true);
this.player.animations.add('right', [5, 6, 7, 8], 10, true);
this.camera.follow(this.player);
this.cursors = this.input.keyboard.createCursorKeys();
this.clouds.callAll('start');
},
customSep: function (player, platform) {
if (!this.locked && player.body.velocity.y > 0)
{
this.locked = true;
this.lockedTo = platform;
platform.playerLocked = true;
player.body.velocity.y = 0;
}
},
checkLock: function () {
this.player.body.velocity.y = 0;
// If the player has walked off either side of the platform then they're no longer locked to it
if (this.player.body.right < this.lockedTo.body.x || this.player.body.x > this.lockedTo.body.right)
{
this.cancelLock();
}
},
cancelLock: function () {
this.wasLocked = true;
this.locked = false;
},
preRender: function () {
if (this.game.paused)
{
// Because preRender still runs even if your game pauses!
return;
}
if (this.locked || this.wasLocked)
{
this.player.x += this.lockedTo.deltaX;
this.player.y = this.lockedTo.y - 48;
if (this.player.body.velocity.x !== 0)
{
this.player.body.velocity.y = 0;
}
}
if (this.willJump)
{
this.willJump = false;
if (this.lockedTo && this.lockedTo.deltaY < 0 && this.wasLocked)
{
// If the platform is moving up we add its velocity to the players jump
this.player.body.velocity.y = -300 + (this.lockedTo.deltaY * 10);
}
else
{
this.player.body.velocity.y = -300;
}
this.jumpTimer = this.time.time + 750;
}
if (this.wasLocked)
{
this.wasLocked = false;
this.lockedTo.playerLocked = false;
this.lockedTo = null;
}
},
update: function () {
this.background.tilePosition.x = -(this.camera.x * 0.7);
this.trees.tilePosition.x = -(this.camera.x * 0.9);
this.physics.arcade.collide(this.player, this.stationary);
this.physics.arcade.collide(this.player, this.clouds, this.customSep, null, this);
// Do this AFTER the collide check, or we won't have blocked/touching set
var standing = this.player.body.blocked.down || this.player.body.touching.down || this.locked;
this.player.body.velocity.x = 0;
if (this.cursors.left.isDown)
{
this.player.body.velocity.x = -150;
if (this.facing !== 'left')
{
this.player.play('left');
this.facing = 'left';
}
}
else if (this.cursors.right.isDown)
{
this.player.body.velocity.x = 150;
if (this.facing !== 'right')
{
this.player.play('right');
this.facing = 'right';
}
}
else
{
if (this.facing !== 'idle')
{
this.player.animations.stop();
if (this.facing === 'left')
{
this.player.frame = 0;
}
else
{
this.player.frame = 5;
}
this.facing = 'idle';
}
}
if (standing && this.cursors.up.isDown && this.time.time > this.jumpTimer)
{
if (this.locked)
{
this.cancelLock();
}
this.willJump = true;
}
if (this.locked)
{
this.checkLock();
}
}
};
CloudPlatform = function (game, x, y, key, group) {
if (typeof group === 'undefined') { group = game.world; }
Phaser.Sprite.call(this, game, x, y, key);
game.physics.arcade.enable(this);
this.anchor.x = 0.5;
this.body.customSeparateX = true;
this.body.customSeparateY = true;
this.body.allowGravity = false;
this.body.immovable = true;
this.playerLocked = false;
group.add(this);
};
CloudPlatform.prototype = Object.create(Phaser.Sprite.prototype);
CloudPlatform.prototype.constructor = CloudPlatform;
CloudPlatform.prototype.addMotionPath = function (motionPath) {
this.tweenX = this.game.add.tween(this.body);
this.tweenY = this.game.add.tween(this.body);
// motionPath is an array containing objects with this structure
// [
// { x: "+200", xSpeed: 2000, xEase: "Linear", y: "-200", ySpeed: 2000, yEase: "Sine.easeIn" }
// ]
for (var i = 0; i < motionPath.length; i++)
{
this.tweenX.to( { x: motionPath[i].x }, motionPath[i].xSpeed, motionPath[i].xEase);
this.tweenY.to( { y: motionPath[i].y }, motionPath[i].ySpeed, motionPath[i].yEase);
}
this.tweenX.loop();
this.tweenY.loop();
};
CloudPlatform.prototype.start = function () {
this.tweenX.start();
this.tweenY.start();
};
CloudPlatform.prototype.stop = function () {
this.tweenX.stop();
this.tweenY.stop();
};
game.state.add('Game', PhaserGame, true);
Also see: Tab Triggers