Skip to content

Commit d057a9f

Browse files
committed
Device, Canvas and GamePad classes all updated for better CocoonJS support (thanks Videlais)
1 parent 14646e8 commit d057a9f

5 files changed

Lines changed: 160 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ Updates:
132132
* The InputManager now sets the canvas style cursor to 'inherit' instead of 'default'.
133133
* World.reset now calls Camera.reset which sends the camera back to 0,0 and un-follows any object it may have been tracking.
134134
* Added hostname: '*' to the grunt-connect in Gruntfile.js (fixes #426)
135+
* Device, Canvas and GamePad classes all updated for better CocoonJS support (thanks Videlais)
135136

136137

137138
Bug Fixes:

examples/wip/contact1.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48);
7+
game.load.image('background', 'assets/games/starstruck/background2.png');
8+
game.load.image('box', 'assets/sprites/block.png');
9+
10+
}
11+
12+
var player;
13+
var facing = 'left';
14+
var jumpTimer = 0;
15+
var cursors;
16+
var jumpButton;
17+
var boxes;
18+
19+
function create() {
20+
21+
game.stage.backgroundColor = '#000000';
22+
23+
bg = game.add.tileSprite(0, 0, 800, 600, 'background');
24+
bg.fixedToCamera = true;
25+
26+
game.physics.gravity.y = 20;
27+
// game.physics.setBoundsToWorld(true, true, false, true);
28+
29+
// game.physics.world.gravity[1] = -20;
30+
game.physics.friction = 0.5;
31+
// game.physics.world.solver.stiffness = 1e20;
32+
// game.physics.world.solver.relaxation = 3;
33+
34+
// Materials
35+
// var groundMaterial = game.physics.createMaterial('ground');
36+
// var characterMaterial = game.physics.createMaterial('character');
37+
// var boxMaterial = game.physics.createMaterial('box');
38+
39+
player = game.add.sprite(50, 400, 'dude');
40+
player.physicsEnabled = true;
41+
player.body.fixedRotation = true;
42+
// player.body.setMaterial(characterMaterial);
43+
// player.body.mass = 1;
44+
// player.body.damping = 0.5;
45+
46+
player.animations.add('left', [0, 1, 2, 3], 10, true);
47+
player.animations.add('turn', [4], 20, true);
48+
player.animations.add('right', [5, 6, 7, 8], 10, true);
49+
50+
boxes = game.add.group();
51+
52+
for (var i = 0; i < 1; i++)
53+
{
54+
var box = boxes.create(game.rnd.integerInRange(200, 700), game.rnd.integerInRange(0, 100), 'box');
55+
// box.scale.set(0.5);
56+
// box.scale.set(game.rnd.realInRange(0.2, 0.7));
57+
box.physicsEnabled = true;
58+
// box.body.mass = 10;
59+
// box.body.setMaterial(boxMaterial);
60+
box.body.fixedRotation = true;
61+
}
62+
63+
// Set the material along the ground
64+
// game.physics.setWorldMaterial(groundMaterial);
65+
66+
// var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground
67+
// var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes
68+
// var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground
69+
70+
// console.log(groundCharacterCM);
71+
// console.log(boxGroundCM);
72+
73+
// game.camera.follow(player);
74+
75+
cursors = game.input.keyboard.createCursorKeys();
76+
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
77+
78+
}
79+
80+
function update() {
81+
82+
if (cursors.left.isDown)
83+
{
84+
player.body.moveLeft(200);
85+
86+
if (facing != 'left')
87+
{
88+
player.animations.play('left');
89+
facing = 'left';
90+
}
91+
}
92+
else if (cursors.right.isDown)
93+
{
94+
player.body.moveRight(200);
95+
96+
if (facing != 'right')
97+
{
98+
player.animations.play('right');
99+
facing = 'right';
100+
}
101+
}
102+
else
103+
{
104+
player.body.velocity.x = 0;
105+
106+
if (facing != 'idle')
107+
{
108+
player.animations.stop();
109+
110+
if (facing == 'left')
111+
{
112+
player.frame = 0;
113+
}
114+
else
115+
{
116+
player.frame = 5;
117+
}
118+
119+
facing = 'idle';
120+
}
121+
}
122+
123+
if (jumpButton.isDown && game.time.now > jumpTimer && checkIfCanJump())
124+
{
125+
player.body.moveUp(300);
126+
jumpTimer = game.time.now + 750;
127+
}
128+
129+
}
130+
131+
function checkIfCanJump(){
132+
var yAxis = p2.vec2.fromValues(0,1);
133+
var result = false;
134+
for(var i=0; i<game.physics.world.narrowphase.contactEquations.length; i++){
135+
var c = game.physics.world.narrowphase.contactEquations[i];
136+
if(c.bi === player.body.data || c.bj === player.body.data){
137+
var d = p2.vec2.dot(c.ni,yAxis); // Normal dot Y-axis
138+
if(c.bi === player.body.data) d *= -1;
139+
if(d > 0.5) result = true;
140+
}
141+
}
142+
return result;
143+
}
144+
145+
146+
function render () {
147+
148+
// if (player.debug)
149+
// {
150+
game.debug.renderPhysicsBody(player.body);
151+
// game.debug.renderBodyInfo(player, 16, 24);
152+
// }
153+
154+
}

src/input/Gamepad.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Phaser.Gamepad = function (game) {
6767
* @property {boolean} _gamepadSupportAvailable - Are gamepads supported in this browser or not?
6868
* @private
6969
*/
70-
this._gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads || (navigator.userAgent.indexOf('Firefox/') != -1);
70+
this._gamepadSupportAvailable = !!navigator.webkitGetGamepads || !!navigator.webkitGamepads || (navigator.userAgent.indexOf('Firefox/') != -1) || !!navigator.getGamepads;
7171

7272
/**
7373
* Used to check for differences between earlier polls and current state of gamepads.
@@ -220,7 +220,7 @@ Phaser.Gamepad.prototype = {
220220
*/
221221
_pollGamepads: function () {
222222

223-
var rawGamepads = (navigator.webkitGetGamepads && navigator.webkitGetGamepads()) || navigator.webkitGamepads;
223+
var rawGamepads = (navigator.webkitGetGamepads && navigator.webkitGetGamepads()) || navigator.webkitGamepads || navigator.getGamepads;
224224

225225
if (rawGamepads)
226226
{

src/system/Canvas.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Phaser.Canvas = {
2626
width = width || 256;
2727
height = height || 256;
2828

29-
var canvas = document.createElement('canvas');
29+
// var canvas = document.createElement('canvas');
30+
var canvas = document.createElement(navigator.isCocoonJS ? 'screencanvas' : 'canvas');
3031

3132
if (typeof id === 'string')
3233
{

src/system/Device.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ Phaser.Device.prototype = {
385385
*/
386386
_checkFeatures: function () {
387387

388-
this.canvas = !!window['CanvasRenderingContext2D'];
388+
this.canvas = !!window['CanvasRenderingContext2D'] || this.iscocoonJS;
389389

390390
try {
391391
this.localStorage = !!localStorage.getItem;

0 commit comments

Comments
 (0)