We talk about JavaScript. Each month in Warsaw, Poland.
Cyfrowy Polsat, Warsaw
JavaScript Ninja. Mac lover. Open source fan.
Organizer WarsawJS
"Kto chce szuka sposobu, kto nie chce szuka powodu."
31↑
Use ECMAScript 6.
webpack and Babel.js take care of compiling to ECMAScript 5 (for browser).
module.exports = {
resolve: { extensions: ['.es6.js', '.js', ''] },
entry: './app/scripts/main',
output: {
filename: 'bundle.js',
path: './app/dist'
},
module: { loaders: [{
test: /\.es6\.js/,
exclude: /node_modules/,
loader: 'babel?stage=0'
}] }
}
// Destructuring assignment
let [x, y] = ['abc', 100]
// x = 'abc', y = 100
No more HTML files, because everything is in the Canvas.
Type | Files |
---|---|
Normal |
2.8M phaser.js 726K phaser.min.js |
Only need to |
2,2M phaser-arcade-physics.js 754K phaser-arcade-physics.map 567K phaser-arcade-physics.min.js |
// Create game object
this.game = new Phaser.Game(800, 400, Phaser.Canvas, 'game`)
157 LOC (126↑)
// Add state to set
this.game.state.add(name, handler)
// Activate passed state
this.game.state.start(name)
// Load image file and put it to cache
this.load.image(key, path)
// Fetch from cache image and put to Canvas
this.add.image(x, y, key)
// Add button to Canvas
this.add.button(x, y, key, handler)
258 LOC (101↑)
this.load.tilemap(key, path, data, format)
// data: if `path` was passed, equals null
// format: Phaser.Tilemap.TILED_JSON
this.load.spritesheet(key, path, width, height)
// width: tile width
// height: tile height
// Fetch file and put response into cache
this.load.json(key, path)
// Return parsed object
object = this.cache.getJSON(key)
// Add sprite to game
this.add.sprite(x, y, key)
// Add label
message = this.add.text(x, y, text)
message.alpha = 0
this.add.tween(message).to(config, time, type, true)
// config: { alpha: 1 }
// time: Phaser.Timer.SECOND
// type: Phaser.Easing.Linear.None
if (keyboard.isDown(Phaser.Keyboard.LEFT) {
player.x -= 5
}
If we would like to use collision detection,
we should update player.velocity.x not only player.x.
// Update velocity of player
player.velocity.x -= 5
Remember to clear velocity before that process!
// Reset velocity
player.velocity.x = 0
// Enable arcade mode for our player
this.physics.arcade.enable(player)
map.setCollisionByIndex(1) // Start counting from 1
layer = map.createLayer(name)
this.physics.arcade.collide(player, layer)
Player will be collide with tile with index = 0 from now!
balls = this.add.group()
balls.enableBody = true
// Add body to group of balls
this.physics.arcade.enable(balls)
// Start collide between player and group of balls
this.physics.arcade.collide(player, balls, handler)
384 LOC (126↑)
// Clock with reference to state
this.time.events.add(time, handler, context)
710 LOC (326↑)
this.load.audio(key, path)
sound = this.add.audio(key)
sound.play()
858 LOC (148↑)
1028 LOC (170↑)
// Support mouse over
button.events.onInputOver.add(handler, context)
isOverlap() => {
return this.physics.arcade.overlap(player, enemy)
}
1270 LOC (242↑)
1533 LOC (263↑)
1732 LOC (199↑)
// Upgrade collision
map.setCollision([1, 3])
Phaser.State#rnd.integerInRange(min, max)
instead of:
Utilities.random(min, max)