From d057a9fe112d6e41fb6a2f493c713268d1dc849c Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 19 Feb 2014 02:45:42 +0000 Subject: [PATCH] Device, Canvas and GamePad classes all updated for better CocoonJS support (thanks Videlais) --- README.md | 1 + examples/wip/contact1.js | 154 +++++++++++++++++++++++++++++++++++++++ src/input/Gamepad.js | 4 +- src/system/Canvas.js | 3 +- src/system/Device.js | 2 +- 5 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 examples/wip/contact1.js diff --git a/README.md b/README.md index 3ecb7eb0..4e5daaae 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ Updates: * The InputManager now sets the canvas style cursor to 'inherit' instead of 'default'. * World.reset now calls Camera.reset which sends the camera back to 0,0 and un-follows any object it may have been tracking. * Added hostname: '*' to the grunt-connect in Gruntfile.js (fixes #426) +* Device, Canvas and GamePad classes all updated for better CocoonJS support (thanks Videlais) Bug Fixes: diff --git a/examples/wip/contact1.js b/examples/wip/contact1.js new file mode 100644 index 00000000..37e2095a --- /dev/null +++ b/examples/wip/contact1.js @@ -0,0 +1,154 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48); + game.load.image('background', 'assets/games/starstruck/background2.png'); + game.load.image('box', 'assets/sprites/block.png'); + +} + +var player; +var facing = 'left'; +var jumpTimer = 0; +var cursors; +var jumpButton; +var boxes; + +function create() { + + game.stage.backgroundColor = '#000000'; + + bg = game.add.tileSprite(0, 0, 800, 600, 'background'); + bg.fixedToCamera = true; + + game.physics.gravity.y = 20; + // game.physics.setBoundsToWorld(true, true, false, true); + + // game.physics.world.gravity[1] = -20; + game.physics.friction = 0.5; + // game.physics.world.solver.stiffness = 1e20; + // game.physics.world.solver.relaxation = 3; + + // Materials + // var groundMaterial = game.physics.createMaterial('ground'); + // var characterMaterial = game.physics.createMaterial('character'); + // var boxMaterial = game.physics.createMaterial('box'); + + player = game.add.sprite(50, 400, 'dude'); + player.physicsEnabled = true; + player.body.fixedRotation = true; + // player.body.setMaterial(characterMaterial); + // player.body.mass = 1; + // player.body.damping = 0.5; + + player.animations.add('left', [0, 1, 2, 3], 10, true); + player.animations.add('turn', [4], 20, true); + player.animations.add('right', [5, 6, 7, 8], 10, true); + + boxes = game.add.group(); + + for (var i = 0; i < 1; i++) + { + var box = boxes.create(game.rnd.integerInRange(200, 700), game.rnd.integerInRange(0, 100), 'box'); + // box.scale.set(0.5); + // box.scale.set(game.rnd.realInRange(0.2, 0.7)); + box.physicsEnabled = true; + // box.body.mass = 10; + // box.body.setMaterial(boxMaterial); + box.body.fixedRotation = true; + } + + // Set the material along the ground + // game.physics.setWorldMaterial(groundMaterial); + + // var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground + // var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes + // var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground + + // console.log(groundCharacterCM); + // console.log(boxGroundCM); + + // game.camera.follow(player); + + cursors = game.input.keyboard.createCursorKeys(); + jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); + +} + +function update() { + + if (cursors.left.isDown) + { + player.body.moveLeft(200); + + if (facing != 'left') + { + player.animations.play('left'); + facing = 'left'; + } + } + else if (cursors.right.isDown) + { + player.body.moveRight(200); + + if (facing != 'right') + { + player.animations.play('right'); + facing = 'right'; + } + } + else + { + player.body.velocity.x = 0; + + if (facing != 'idle') + { + player.animations.stop(); + + if (facing == 'left') + { + player.frame = 0; + } + else + { + player.frame = 5; + } + + facing = 'idle'; + } + } + + if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump()) + { + player.body.moveUp(300); + jumpTimer = game.time.now + 750; + } + +} + +function checkIfCanJump(){ +var yAxis = p2.vec2.fromValues(0,1); +var result = false; +for(var i=0; i 0.5) result = true; + } +} +return result; +} + + +function render () { + + // if (player.debug) + // { + game.debug.renderPhysicsBody(player.body); + // game.debug.renderBodyInfo(player, 16, 24); + // } + +} diff --git a/src/input/Gamepad.js b/src/input/Gamepad.js index 10753344..281c23c9 100644 --- a/src/input/Gamepad.js +++ b/src/input/Gamepad.js @@ -67,7 +67,7 @@ Phaser.Gamepad = function (game) { * @property {boolean} _gamepadSupportAvailable - Are gamepads supported in this browser or not? * @private */ - this._gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads || (navigator.userAgent.indexOf('Firefox/') != -1); + this._gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads || (navigator.userAgent.indexOf('Firefox/') != -1) || !!navigator.getGamepads; /** * Used to check for differences between earlier polls and current state of gamepads. @@ -220,7 +220,7 @@ Phaser.Gamepad.prototype = { */ _pollGamepads: function () { - var rawGamepads = (navigator.webkitGetGamepads && navigator.webkitGetGamepads()) || navigator.webkitGamepads; + var rawGamepads = (navigator.webkitGetGamepads && navigator.webkitGetGamepads()) || navigator.webkitGamepads || navigator.getGamepads; if (rawGamepads) { diff --git a/src/system/Canvas.js b/src/system/Canvas.js index 46f416cf..8624fe3c 100644 --- a/src/system/Canvas.js +++ b/src/system/Canvas.js @@ -26,7 +26,8 @@ Phaser.Canvas = { width = width || 256; height = height || 256; - var canvas = document.createElement('canvas'); + // var canvas = document.createElement('canvas'); + var canvas = document.createElement(navigator.isCocoonJS ? 'screencanvas' : 'canvas'); if (typeof id === 'string') { diff --git a/src/system/Device.js b/src/system/Device.js index 97ff397b..62c72a2b 100644 --- a/src/system/Device.js +++ b/src/system/Device.js @@ -385,7 +385,7 @@ Phaser.Device.prototype = { */ _checkFeatures: function () { - this.canvas = !!window['CanvasRenderingContext2D']; + this.canvas = !!window['CanvasRenderingContext2D'] || this.iscocoonJS; try { this.localStorage = !!localStorage.getItem;