Build games as easily as you play them — join the Phaser Beam waitlist for Early Access.
var config = { type: Phaser.AUTO, parent: 'phaser-example', width: 800, height: 600, scene: { preload: preload, create: create } }; var game = new Phaser.Game(config); function preload () { this.load.setBaseURL('https://cdn.phaserfiles.com/v355'); this.load.atlas('cards', 'assets/atlas/cards.png', 'assets/atlas/cards.json'); } function create () { // Create a stack of random cards var frames = this.textures.get('cards').getFrameNames(); var x = 100; var y = 100; for (var i = 0; i < 64; i++) { var image = this.add.image(x, y, 'cards', Phaser.Math.RND.pick(frames)).setInteractive(); this.input.setDraggable(image); y += 6; } // A drop zone var zone = this.add.zone(500, 300, 300, 300).setRectangleDropZone(300, 300); // Just a visual display of the drop zone var graphics = this.add.graphics(); graphics.lineStyle(2, 0xffff00); graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height); this.input.on('dragstart', function (pointer, gameObject) { this.children.bringToTop(gameObject); }, this); this.input.on('drag', function (pointer, gameObject, dragX, dragY) { gameObject.x = dragX; gameObject.y = dragY; }); this.input.on('dragenter', function (pointer, gameObject, dropZone) { graphics.clear(); graphics.lineStyle(2, 0x00ffff); graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height); }); this.input.on('dragleave', function (pointer, gameObject, dropZone) { graphics.clear(); graphics.lineStyle(2, 0xffff00); graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height); }); this.input.on('drop', function (pointer, gameObject, dropZone) { gameObject.x = dropZone.x; gameObject.y = dropZone.y; gameObject.input.enabled = false; }); this.input.on('dragend', function (pointer, gameObject, dropped) { if (!dropped) { gameObject.x = gameObject.input.dragStartX; gameObject.y = gameObject.input.dragStartY; } graphics.clear(); graphics.lineStyle(2, 0xffff00); graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height); }); }
var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600,
scene: {
preload: preload,
create: create
}
};
var game = new Phaser.Game(config);
function preload ()
{
this.load.setBaseURL('https://cdn.phaserfiles.com/v355');
this.load.atlas('cards', 'assets/atlas/cards.png', 'assets/atlas/cards.json');
}
function create ()
{
// Create a stack of random cards
var frames = this.textures.get('cards').getFrameNames();
var x = 100;
var y = 100;
for (var i = 0; i < 64; i++)
{
var image = this.add.image(x, y, 'cards', Phaser.Math.RND.pick(frames)).setInteractive();
this.input.setDraggable(image);
y += 6;
}
// A drop zone
var zone = this.add.zone(500, 300, 300, 300).setRectangleDropZone(300, 300);
// Just a visual display of the drop zone
var graphics = this.add.graphics();
graphics.lineStyle(2, 0xffff00);
graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height);
this.input.on('dragstart', function (pointer, gameObject) {
this.children.bringToTop(gameObject);
}, this);
this.input.on('drag', function (pointer, gameObject, dragX, dragY) {
gameObject.x = dragX;
gameObject.y = dragY;
});
this.input.on('dragenter', function (pointer, gameObject, dropZone) {
graphics.clear();
graphics.lineStyle(2, 0x00ffff);
graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height);
});
this.input.on('dragleave', function (pointer, gameObject, dropZone) {
graphics.clear();
graphics.lineStyle(2, 0xffff00);
graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height);
});
this.input.on('drop', function (pointer, gameObject, dropZone) {
gameObject.x = dropZone.x;
gameObject.y = dropZone.y;
gameObject.input.enabled = false;
});
this.input.on('dragend', function (pointer, gameObject, dropped) {
if (!dropped)
{
gameObject.x = gameObject.input.dragStartX;
gameObject.y = gameObject.input.dragStartY;
}
graphics.clear();
graphics.lineStyle(2, 0xffff00);
graphics.strokeRect(zone.x - zone.input.hitArea.width / 2, zone.y - zone.input.hitArea.height / 2, zone.input.hitArea.width, zone.input.hitArea.height);
});
}