Updated tutorial code so you can't bounce off stars.

This commit is contained in:
photonstorm 2016-09-27 13:57:32 +01:00
parent 00abd388e8
commit 840d3669b0
6 changed files with 64 additions and 6 deletions

View file

@ -103,7 +103,7 @@ function create() {
function update() { function update() {
// Collide the player and the stars with the platforms // Collide the player and the stars with the platforms
game.physics.arcade.collide(player, platforms); var hitPlatform = game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(stars, platforms); game.physics.arcade.collide(stars, platforms);
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function // Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
@ -135,7 +135,7 @@ function update() {
} }
// Allow the player to jump if they are touching the ground. // Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down) if (cursors.up.isDown && player.body.touching.down && hitPlatform)
{ {
player.body.velocity.y = -350; player.body.velocity.y = -350;
} }

View file

@ -108,7 +108,7 @@ function create() {
function update() { function update() {
// Collide the player and the stars with the platforms // Collide the player and the stars with the platforms
game.physics.arcade.collide(player, platforms); var hitPlatform = game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(stars, platforms); game.physics.arcade.collide(stars, platforms);
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function // Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
@ -140,7 +140,7 @@ function update() {
} }
// Allow the player to jump if they are touching the ground. // Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down) if (cursors.up.isDown && player.body.touching.down && hitPlatform)
{ {
player.body.velocity.y = -350; player.body.velocity.y = -350;
} }

View file

@ -24,12 +24,16 @@ Phaser.AnimationParser = {
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here. * @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here. * @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image. * @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
* @param {Phaser.Frame} [frame] - Optional Frame, if this font is embedded in a texture atlas.
* @return {Phaser.FrameData} A FrameData object containing the parsed frames. * @return {Phaser.FrameData} A FrameData object containing the parsed frames.
*/ */
spriteSheet: function (game, key, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) { spriteSheet: function (game, key, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames, frame) {
var img = key; var img = key;
var frameX = (frame) ? frame.x : 0;
var frameY = (frame) ? frame.y : 0;
if (typeof key === 'string') if (typeof key === 'string')
{ {
img = game.cache.getImage(key); img = game.cache.getImage(key);
@ -91,7 +95,7 @@ Phaser.AnimationParser = {
for (var i = 0; i < total; i++) for (var i = 0; i < total; i++)
{ {
data.addFrame(new Phaser.Frame(i, x, y, frameWidth, frameHeight, '')); data.addFrame(new Phaser.Frame(i, x + frameX, y + frameY, frameWidth, frameHeight, ''));
x += frameWidth + spacing; x += frameWidth + spacing;

View file

@ -710,6 +710,60 @@ Phaser.Cache.prototype = {
}, },
/**
* Add a new Sprite Sheet to the Cache, where the sheet is part of a Texture Atlas.
*
* The atlas must already exist in the cache, and be available based on the given `atlasKey`.
*
* The `atlasFrame` specifies the name of the frame within the atlas that the Sprite Sheet is
* stored in.
*
* @method Phaser.Cache#addSpriteSheetFromAtlas
* @param {string} key - The key that this asset will be stored in the cache under. This should be unique within this cache.
* @param {string} atlasKey - The key of the Texture Atlas in the Cache.
* @param {string} atlasFrame - The frame of the Texture Atlas that the Sprite Sheet is in.
* @param {number} frameWidth - Width of the sprite sheet.
* @param {number} frameHeight - Height of the sprite sheet.
* @param {number} [frameMax=-1] - How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
* @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image.
*/
addSpriteSheetFromAtlas: function (key, atlasKey, atlasFrame, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) {
if (frameMax === undefined) { frameMax = -1; }
if (margin === undefined) { margin = 0; }
if (spacing === undefined) { spacing = 0; }
var frame = this.getFrameByName(atlasKey, atlasFrame);
if (!frame)
{
return;
}
var obj = {
font: null,
base: this.getBaseTexture(atlasKey),
frame: frame
};
var obj = {
key: key,
frameWidth: frameWidth,
frameHeight: frameHeight,
margin: margin,
spacing: spacing,
base: this.getBaseTexture(atlasKey),
frame: frame,
frameData: Phaser.AnimationParser.spriteSheet(this.game, data, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames, frame)
};
this._cache.image[key] = obj;
},
/** /**
* Add a new texture atlas to the Cache. * Add a new texture atlas to the Cache.
* *