Jump to content

[Solved]About Resize the sprite


khleug35
 Share

Recommended Posts

Here is my full code of the example. 

https://jsfiddle.net/8ytsozsm/1/

I would like to create a platformer game and make the player has crouch function

but It has a problem , When I click the down button  to resize the sprite to achieve  "crouch"

 if (cursors.down.isDown) {
                 
        player.body.setSize(60, 89, 0, 0);    
        } else{
        player.body.setSize(60, 130, 0, 0); 
       
        }

the sprite is resize from from bottom to top, but I hope the sprite can resize from the top , how to do it with out player.scale.y = -1;

player.scale.y = -1;
this code can solve my problem, but Is there any other better way to do it?

thank you so much, thx

 

2.png.d092a2de681afee11d3ea91f5800d197.png

 

1.png.7e214fa7047b37e94a8a99194d874cb2.png

 

3.thumb.png.48e4192eecce7777aaa0e47534c4a2dd.png

 



var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update ,render: render});

var player;
var platforms;
var cursors;
var jumpButton;

function preload() {

    game.stage.backgroundColor = '#131314';

    game.load.baseURL = 'http://examples.phaser.io/assets/';
    game.load.crossOrigin = 'anonymous';

    /*game.load.image('player', 'sprites/phaser-dude.png');*/
    game.load.image('platform', 'sprites/platform.png');
    game.load.image('platform2', 'sprites/platform.png');
    
    game.load.tilemap('tilemap', 'https://hexus.github.io/phaser-arcade-slopes/assets/maps/demo.json', null, Phaser.Tilemap.TILED_JSON);
}

var player;
var platforms;
var cursors;
var jumpButton;

function create() {

    player = game.add.sprite(100, 100);

    game.physics.arcade.enable(player);

    player.body.collideWorldBounds = true;
    player.body.gravity.y = 500;

       
    
    platforms = game.add.physicsGroup();

    platforms.create(500, 150, 'platform');
    platforms.create(-200, 300, 'platform');
    platforms.create(400, 450, 'platform');

    platforms.setAll('body.immovable', true);

    cursors = game.input.keyboard.createCursorKeys();
    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

}
function update () {

    game.physics.arcade.collide(player, platforms);

    player.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        player.body.velocity.x = -250;
    }
    else if (cursors.right.isDown)
    {
        player.body.velocity.x = 250;
    }

    if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down))
    {
        player.body.velocity.y = -400;
    }
    
     if (cursors.down.isDown) {
                 
        player.body.setSize(60, 89, 0, 0);    
        } else{
        player.body.setSize(60, 130, 0, 0); 
       
        }
}

function render () {

game.debug.body(player);
    }

 

Link to comment
Share on other sites

4 hours ago, onlycape said:

Hi @khleug35 ,

You have to change the vertical offset.

Replace this line:


player.body.setSize(60, 89, 0, 0);

with this:


player.body.setSize(60, 89, 0, 41); 

41 = 130 - 89.

 

Regards.

OH!!!!!Many Thanks

It is very simple way to solve my problem

I am sorry that I am so stupid....

thank you very much

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...