Jump to content

Problem with Tiled ( Probably )


Temich
 Share

Recommended Posts

Hello, Phaser Community! I'm new to HTML5 development, and i've got some problems. Currently i'm working on platformer movement. If a character moves to the edge of the platform, he gets instantly moved to the beginning of that platform,

GIF - http://imgur.com/4mKZoH3

Here's my Level1 code:

Game.Level1 = function(game){};

var map;
var layer;
var player;
var player_test;
var heart;
var MAX_SPEED = 48; 
var ACCELERATION = 1; 
var DRAG = 500; 
var GRAVITY = 920; 
var JUMP_SPEED = -250;  
var RIGHT = true;
var LEFT = false;
var JUMP_TIME = 0.5;

Game.Level1.prototype = 
{
    upInputIsActive:function(duration) 
    {
    var isActive = false;

    isActive = this.input.keyboard.downDuration(Phaser.Keyboard.Z, duration);
    isActive |= (this.input.activePointer.justPressed(duration + 1000/60) &&
    this.input.activePointer.x > game.width/4 &&
    this.input.activePointer.x < game.width/2 + game.width/4);

    return isActive;
    },
    
    upInputReleased:function() {
    var released = false;

    released = this.input.keyboard.upDuration(Phaser.Keyboard.Z);
    released |= this.game.input.activePointer.justReleased();

    return released;
},
    
   leftInputIsActive:function() {
    var isActive = false;

    isActive = this.input.keyboard.isDown(Phaser.Keyboard.LEFT);
    isActive |= (this.game.input.activePointer.isDown &&
    this.game.input.activePointer.x < this.game.width/4);

    return isActive;
   },
    
    rightInputIsActive:function() {
    var isActive = false;

    isActive = this.input.keyboard.isDown(Phaser.Keyboard.RIGHT);
    isActive |= (this.game.input.activePointer.isDown &&
    this.game.input.activePointer.x > this.game.width/2 + this.game.width/4);

    return isActive;
   },
    
    
    
    create:function()
    {
       
        map = this.add.tilemap('lv1', 8, 8 );
        map.addTilesetImage('Tileset', 'tileset');
        layer = map.createLayer(0);
        layer.resizeWorld();
       
        map.setCollisionBetween(1,5);
        
        player = this.game.add.sprite(this.game.width/4, this.game.height - 32, 'player');
        player.animations.add('run_left', [0, 1], 13, true);
        player.animations.add('run_right', [2, 3], 13, true);
        player.animations.add('idle_left', [8, 8, 8, 8, 8, 8, 9, 9, 9, 9], 5, true);
        player.animations.add('idle_right', [10, 10, 10, 10, 10, 10, 11, 11, 11, 11], 5, true);
        
        heart = this.game.add.sprite(this.game.width/2, this.game.height/2, 'heart');
        heart.animations.add('idle', [0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8], 18, true);
        
        player_test = this.game.add.sprite(this.game.width-20, this.game.height - 20, 'player_idle_test');
        player_test.animations.add('test_idle', [0, 1, 2, 3, 4], 13, true);
        
        this.physics.enable(player, Phaser.Physics.ARCADE);
        this.physics.arcade.gravity.y = GRAVITY;
        player.body.maxVelocity.setTo(MAX_SPEED, MAX_SPEED * 10);
    },
    
    update:function()
    {
        this.physics.arcade.collide(player, layer);
        var onTheGround = player.body.blocked.down;
        heart.animations.play('idle');
        player_test.animations.play('test_idle');
        
        if (player.body.velocity.y>0)
        {
            if (LEFT == true) 
                player.frame = 6; 
            if (RIGHT == true)
                player.frame = 5;   
        }
        
    
    
        if (this.leftInputIsActive()) 
        {   
                    player.body.velocity.x += -ACCELERATION/3;
                    LEFT = true;
                    RIGHT = false;            
        } 
    
        else if (this.rightInputIsActive()) 
        {
                player.body.velocity.x = ACCELERATION/3;
                LEFT = false;
                RIGHT = true;
        } 
    
        else 
        {
            player.body.velocity.x = 0;

            if(LEFT == true && RIGHT == false && onTheGround && player.body.velocity.y == 0)
             {    
                player.animations.play('idle_left');  
             }
        
            if(RIGHT == true && LEFT == false && onTheGround && player.body.velocity.y == 0)
            {    
                 player.animations.play('idle_right');       
            }
        }
    
    
    if (this.leftInputIsActive() && player.body.onFloor) 
    {   
        player.animations.play('run_left');        
    } 
    
    if (this.rightInputIsActive() && player.body.onFloor) 
    {   
        player.animations.play('run_right');        
    }
            

    // Set a variable that is true when the player is touching the ground
   
    
    if(onTheGround)
        {
            DRAG = 2250;
            ACCELERATION = 750;
        }
    else
        {
            DRAG = 1750;
            ACCELERATION = 1000;
        }
    

    // If the player is touching the ground, let him jump
    if (onTheGround) {
        this.jumping = false;
    }

    // Jump! Keep y velocity constant while the jump button is held for up to 150 ms
    if (this.upInputIsActive(100) && (this.jumping == false)) 
    {   
        player.body.velocity.y = JUMP_SPEED * JUMP_TIME;
        player.animations.stop();
        if(LEFT)
        {
            player.frame = 7;
        }
        if(RIGHT)
        {
            player.frame = 4;
        }
    }

    // Reduce the number of available jumps if the jump input is released
    if (this.upInputReleased()) 
    {
       this.jumping = true;
    }
        
}
    

        
    }

    

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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