mirror of
https://github.com/wassname/phaser.git
synced 2026-08-11 11:23:06 +08:00
* Updated: event.preventDefault() has been added to all Mouse event handlers.
* Updated: Sprite.deltaX/Y removed due to non-use. prevX/Y values moved to Sprite._cache.prevX/Y. * Updated: Due to missing extends parameter the Sprite prototype was picking up functions from classes it never meant to (Button, TilemapLayer), now fully isolated.
This commit is contained in:
@@ -1,34 +1,40 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
|
||||
|
||||
function preload() {
|
||||
game.load.spritesheet('button', 'assets/buttons/number-buttons.png', 160, 160);
|
||||
}
|
||||
|
||||
var container;
|
||||
// Enable scaling
|
||||
game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL;
|
||||
game.stage.scale.maxWidth = 1024;
|
||||
game.stage.scale.maxHeight = 672;
|
||||
game.stage.scale.refresh();
|
||||
|
||||
game.load.image('atari1', 'assets/sprites/atari130xe.png');
|
||||
game.load.image('atari2', 'assets/sprites/atari800xl.png');
|
||||
game.load.image('atari4', 'assets/sprites/atari800.png');
|
||||
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
|
||||
game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
|
||||
game.load.image('firstaid', 'assets/sprites/firstaid.png');
|
||||
game.load.image('diamond', 'assets/sprites/diamond.png');
|
||||
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
|
||||
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
container = game.add.group();
|
||||
// This returns an array of all the image keys in the cache
|
||||
var images = game.cache.getImageKeys();
|
||||
|
||||
// Add buttons to container.
|
||||
container.add(game.add.button(200, 100, 'button', bringMeToTop, this, 0, 0, 0));
|
||||
container.add(game.add.button(300, 100, 'button', bringMeToTop, this, 1, 1, 1));
|
||||
container.add(game.add.button(100, 200, 'button', bringMeToTop, this, 2, 2, 2));
|
||||
container.add(game.add.button(400, 200, 'button', bringMeToTop, this, 3, 3, 3));
|
||||
container.add(game.add.button(300, 300, 'button', bringMeToTop, this, 4, 4, 4));
|
||||
container.add(game.add.button(200, 300, 'button', bringMeToTop, this, 5, 5, 5));
|
||||
// Now let's create some random sprites and enable them all for drag and 'bring to top'
|
||||
for (var i = 0; i < 20; i++)
|
||||
{
|
||||
var img = game.rnd.pick(images);
|
||||
var tempSprite = game.add.sprite(game.world.randomX, game.world.randomY, img);
|
||||
tempSprite.inputEnabled = true;
|
||||
tempSprite.input.enableDrag(false, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderText('Tap or click button to bring it to the top.', 32, 32);
|
||||
|
||||
}
|
||||
|
||||
function bringMeToTop(btn) {
|
||||
|
||||
container.bringToTop(btn);
|
||||
|
||||
}
|
||||
game.debug.renderInputInfo(32, 32);
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
// Enable scaling
|
||||
// game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL;
|
||||
// game.stage.scale.maxWidth = 1500;
|
||||
// game.stage.scale.maxHeight = 1000;
|
||||
// game.stage.scale.startFullScreen();
|
||||
// game.stage.scale.refresh();
|
||||
|
||||
game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71);
|
||||
|
||||
game.load.image('sky0','assets/skies/space2.png');
|
||||
game.load.image('sky1','assets/skies/cavern1.png');
|
||||
game.load.image('sky2','assets/skies/chrome.png');
|
||||
game.load.image('sky3','assets/skies/fire.png');
|
||||
game.load.image('sky4','assets/skies/fog.png');
|
||||
game.load.image('sky5','assets/skies/sky1.png');
|
||||
game.load.image('sky6','assets/skies/toxic.png');
|
||||
|
||||
}
|
||||
|
||||
var background;
|
||||
var button1;
|
||||
var button2;
|
||||
var button3;
|
||||
var button4;
|
||||
var button5;
|
||||
var button6;
|
||||
|
||||
function create() {
|
||||
|
||||
background = game.add.sprite(0, 0, 'sky0');
|
||||
background.name = 'background';
|
||||
|
||||
// Standard button (also used as our pointer tracker)
|
||||
button1 = game.add.button(100, 100, 'button', changeSky, this, 2, 1, 0);
|
||||
button1.name = 'sky1';
|
||||
button1.anchor.setTo(0.5, 0.5);
|
||||
|
||||
// Rotated button
|
||||
button2 = game.add.button(330, 200, 'button', changeSky, this, 2, 1, 0);
|
||||
button2.name = 'sky2';
|
||||
button2.angle = 24;
|
||||
button2.anchor.setTo(0.5, 0.5);
|
||||
|
||||
// Width scaled button
|
||||
button3 = game.add.button(100, 300, 'button', changeSky, this, 2, 1, 0);
|
||||
button3.name = 'sky3';
|
||||
button3.width = 300;
|
||||
button3.anchor.setTo(0, 0.5);
|
||||
// button3.angle = 0.1;
|
||||
|
||||
// Scaled button
|
||||
button4 = game.add.button(300, 450, 'button', changeSky, this, 2, 1, 0);
|
||||
button4.name = 'sky4';
|
||||
button4.scale.setTo(2, 2);
|
||||
|
||||
// Shrunk button
|
||||
button5 = game.add.button(100, 450, 'button', changeSky, this, 2, 1, 0);
|
||||
button5.name = 'sky5';
|
||||
button5.scale.setTo(0.5, 0.5);
|
||||
|
||||
// Scaled and Rotated button
|
||||
button6 = game.add.button(570, 200, 'button', changeSky, this, 2, 1, 0); // anchor 0.5
|
||||
button6.name = 'sky6';
|
||||
button6.angle = 32;
|
||||
button6.scale.setTo(2, 2);
|
||||
button6.anchor.setTo(0.5, 0.5);
|
||||
|
||||
// works regardless of world angle, parent angle or camera position
|
||||
// game.world.setBounds(0, 0, 2000, 2000);
|
||||
// game.world.angle = 10;
|
||||
// game.camera.x = 300;
|
||||
|
||||
// Phaser.Canvas.setSmoothingEnabled(false);
|
||||
|
||||
game.input.onDown.add(gofull, this);
|
||||
|
||||
}
|
||||
|
||||
function gofull() {
|
||||
|
||||
game.stage.scale.startFullScreen(false);
|
||||
|
||||
}
|
||||
|
||||
function changeSky (button) {
|
||||
|
||||
background.loadTexture(button.name);
|
||||
|
||||
}
|
||||
|
||||
function render () {
|
||||
|
||||
game.debug.renderSpriteCorners(button1, false, true);
|
||||
game.debug.renderSpriteCorners(button2, false, true);
|
||||
game.debug.renderSpriteCorners(button3, false, true);
|
||||
game.debug.renderSpriteCorners(button4, false, true);
|
||||
game.debug.renderSpriteCorners(button5, false, true);
|
||||
game.debug.renderSpriteCorners(button6, false, true);
|
||||
|
||||
// game.debug.renderWorldTransformInfo(button1, 32, 132);
|
||||
// game.debug.renderText('sx: ' + button3.scale.x + ' sy: ' + button3.scale.y + ' w: ' + button3.width + ' cw: ' + button3._cache.width, 32, 20);
|
||||
// game.debug.renderText('ox: ' + game.stage.offset.x + ' oy: ' + game.stage.offset.y, 32, 20);
|
||||
// game.debug.renderPoint(button3.input._tempPoint);
|
||||
// game.debug.renderPoint(button6.input._tempPoint);
|
||||
|
||||
game.debug.renderInputInfo(32, 32);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
// Enable scaling
|
||||
game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL;
|
||||
game.stage.scale.maxWidth = 1500;
|
||||
game.stage.scale.maxHeight = 1000;
|
||||
game.stage.scale.refresh();
|
||||
|
||||
game.load.image('atari1', 'assets/sprites/atari130xe.png');
|
||||
game.load.image('atari2', 'assets/sprites/atari800xl.png');
|
||||
game.load.image('atari4', 'assets/sprites/atari800.png');
|
||||
game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png');
|
||||
game.load.image('duck', 'assets/sprites/darkwing_crazy.png');
|
||||
game.load.image('firstaid', 'assets/sprites/firstaid.png');
|
||||
game.load.image('diamond', 'assets/sprites/diamond.png');
|
||||
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
|
||||
|
||||
}
|
||||
|
||||
function create() {
|
||||
|
||||
game.world.setBounds(0, 0, 2000, 2000);
|
||||
|
||||
// This returns an array of all the image keys in the cache
|
||||
var images = game.cache.getImageKeys();
|
||||
|
||||
// Now let's create some random sprites and enable them all for drag and 'bring to top'
|
||||
for (var i = 0; i < 200; i++)
|
||||
{
|
||||
var img = game.rnd.pick(images);
|
||||
var tempSprite = game.add.sprite(game.world.randomX, game.world.randomY, img);
|
||||
tempSprite.inputEnabled = true;
|
||||
tempSprite.input.enableDrag(false, true);
|
||||
}
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
game.camera.y -= 4;
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
game.camera.y += 4;
|
||||
}
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
game.camera.x -= 4;
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
game.camera.x += 4;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderInputInfo(32, 32);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('atari', 'assets/sprites/atari130xe.png');
|
||||
|
||||
}
|
||||
|
||||
var sprite1;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
sprite1 = game.add.sprite(350, 300, 'atari');
|
||||
sprite1.name = 'atari';
|
||||
sprite1.anchor.setTo(0.5, -2);
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
sprite1.angle += 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
function render() {
|
||||
|
||||
game.debug.renderSpriteCorners(sprite1);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('atari', 'assets/sprites/atari130xe.png');
|
||||
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
|
||||
|
||||
}
|
||||
|
||||
var sprite1;
|
||||
var sprite2;
|
||||
|
||||
function create() {
|
||||
|
||||
game.stage.backgroundColor = '#2d2d2d';
|
||||
|
||||
// This will check Sprite vs. Sprite collision
|
||||
|
||||
sprite1 = game.add.sprite(50, 250, 'atari');
|
||||
sprite1.name = 'atari';
|
||||
sprite1.body.immovable = true;
|
||||
|
||||
sprite2 = game.add.sprite(0, 0, 'mushroom');
|
||||
sprite2.name = 'mushroom';
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
sprite1.body.x = game.input.x; //uncoment for tests
|
||||
|
||||
if (sprite2.y > 400)
|
||||
{
|
||||
sprite2.x = 0;
|
||||
sprite2.y = 0;
|
||||
}
|
||||
|
||||
sprite2.body.velocity.x = 100;
|
||||
sprite2.body.velocity.y = 50;
|
||||
|
||||
// object1, object2, collideCallback, processCallback, callbackContext
|
||||
game.physics.collide(sprite1, sprite2, collisionHandler, null, this);
|
||||
|
||||
}
|
||||
|
||||
function collisionHandler (obj1, obj2) {
|
||||
|
||||
game.stage.backgroundColor = '#992d2d';
|
||||
|
||||
console.log(obj1.name + ' collided with ' + obj2.name);
|
||||
|
||||
}
|
||||
|
||||
function render() {
|
||||
game.debug.renderSpriteInfo(sprite1, 100, 400);
|
||||
game.debug.renderSpriteBounds(sprite1);
|
||||
game.debug.renderSpriteInfo(sprite2, 100, 100);
|
||||
game.debug.renderSpriteBounds(sprite2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user