Skip to content

Commit 840d366

Browse files
committed
Updated tutorial code so you can't bounce off stars.
1 parent 00abd38 commit 840d366

6 files changed

Lines changed: 64 additions & 6 deletions

File tree

resources/tutorials/02 Making your first game/part8.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
function update() {
104104

105105
// Collide the player and the stars with the platforms
106-
game.physics.arcade.collide(player, platforms);
106+
var hitPlatform = game.physics.arcade.collide(player, platforms);
107107
game.physics.arcade.collide(stars, platforms);
108108

109109
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
@@ -135,7 +135,7 @@
135135
}
136136

137137
// Allow the player to jump if they are touching the ground.
138-
if (cursors.up.isDown && player.body.touching.down)
138+
if (cursors.up.isDown && player.body.touching.down && hitPlatform)
139139
{
140140
player.body.velocity.y = -350;
141141
}

resources/tutorials/02 Making your first game/part9.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
function update() {
109109

110110
// Collide the player and the stars with the platforms
111-
game.physics.arcade.collide(player, platforms);
111+
var hitPlatform = game.physics.arcade.collide(player, platforms);
112112
game.physics.arcade.collide(stars, platforms);
113113

114114
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
@@ -140,7 +140,7 @@
140140
}
141141

142142
// Allow the player to jump if they are touching the ground.
143-
if (cursors.up.isDown && player.body.touching.down)
143+
if (cursors.up.isDown && player.body.touching.down && hitPlatform)
144144
{
145145
player.body.velocity.y = -350;
146146
}
Binary file not shown.
Binary file not shown.

src/animation/AnimationParser.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ Phaser.AnimationParser = {
2424
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
2525
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
2626
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
27+
* @param {Phaser.Frame} [frame] - Optional Frame, if this font is embedded in a texture atlas.
2728
* @return {Phaser.FrameData} A FrameData object containing the parsed frames.
2829
*/
29-
spriteSheet: function (game, key, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
30+
spriteSheet: function (game, key, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames, frame) {
3031

3132
var img = key;
3233

34+
var frameX = (frame) ? frame.x : 0;
35+
var frameY = (frame) ? frame.y : 0;
36+
3337
if (typeof key === 'string')
3438
{
3539
img = game.cache.getImage(key);
@@ -91,7 +95,7 @@ Phaser.AnimationParser = {
9195

9296
for (var i = 0; i < total; i++)
9397
{
94-
data.addFrame(new Phaser.Frame(i, x, y, frameWidth, frameHeight, ''));
98+
data.addFrame(new Phaser.Frame(i, x + frameX, y + frameY, frameWidth, frameHeight, ''));
9599

96100
x += frameWidth + spacing;
97101

src/loader/Cache.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,60 @@ Phaser.Cache.prototype = {
710710

711711
},
712712

713+
/**
714+
* Add a new Sprite Sheet to the Cache, where the sheet is part of a Texture Atlas.
715+
*
716+
* The atlas must already exist in the cache, and be available based on the given `atlasKey`.
717+
*
718+
* The `atlasFrame` specifies the name of the frame within the atlas that the Sprite Sheet is
719+
* stored in.
720+
*
721+
* @method Phaser.Cache#addSpriteSheetFromAtlas
722+
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
723+
* @param {string} atlasKey - The key of the Texture Atlas in the Cache.
724+
* @param {string} atlasFrame - The frame of the Texture Atlas that the Sprite Sheet is in.
725+
* @param {number} frameWidth - Width of the sprite sheet.
726+
* @param {number} frameHeight - Height of the sprite sheet.
727+
* @param {number} [frameMax=-1] - How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.
728+
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
729+
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
730+
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
731+
*/
732+
addSpriteSheetFromAtlas: function (key, atlasKey, atlasFrame, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
733+
734+
if (frameMax === undefined) { frameMax = -1; }
735+
if (margin === undefined) { margin = 0; }
736+
if (spacing === undefined) { spacing = 0; }
737+
738+
var frame = this.getFrameByName(atlasKey, atlasFrame);
739+
740+
if (!frame)
741+
{
742+
return;
743+
}
744+
745+
var obj = {
746+
font: null,
747+
base: this.getBaseTexture(atlasKey),
748+
frame: frame
749+
};
750+
751+
752+
var obj = {
753+
key: key,
754+
frameWidth: frameWidth,
755+
frameHeight: frameHeight,
756+
margin: margin,
757+
spacing: spacing,
758+
base: this.getBaseTexture(atlasKey),
759+
frame: frame,
760+
frameData: Phaser.AnimationParser.spriteSheet(this.game, data, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames, frame)
761+
};
762+
763+
this._cache.image[key] = obj;
764+
765+
},
766+
713767
/**
714768
* Add a new texture atlas to the Cache.
715769
*

0 commit comments

Comments
 (0)