Jump to content

Problem with making platformer using Arcade


XHH
 Share

Recommended Posts

Hello,

 

I'm using Arcade physics to detect collisions in a platformer I'm working on. However when my player jumps while moving towards a wall, the player can stand on the wall and just hang there (see image below).

 

29qib1g.png

 

How can I get the player to stop hanging on walls?

Here is my code. Sorry for the weird formatting/organization. I put my code together using an IDE I'm working on.

Really hope someone can help me with platforming. Thanks in advance.

var game = new Phaser.Game(640,762,Phaser.CANVAS,'', { preload: preload, create: create, update: update, render: render});var G_Player;var G_Ground;var G_Collision_box;function Player(x,y,frame){	this._name='Player';	this.x=typeof x !== 'undefined' ? x : 0;;	this.y=typeof y !== 'undefined' ? y: 0;;    	this.sprite = G_Player.create(this.x,this.y,'player_stand',frame);   	OBJECTS.push(this);        this.onCreate = function(){	// physics setup code	game.camera.follow(this.sprite);	game.world.setBounds(0, 0, 1920, 1200);	this.sprite.body.collideWorldBounds = true;	this.sprite.body.gravity.y = 700;	this.sprite.body.setSize(13,30,2,2);	this.sprite.body.bounce.x = 0.1;	this.sprite.animations.add('stand');	this.sprite.animations.play('walk');	this.jump_height = 400	this.hspeed = 150	this.jumped = true;}this.onUpdate = function(){	// Horizontal Movement	this.sprite.body.velocity.x = 0;	if(K_ARROWS.right.isDown && !this.sprite.body.touching.right){		this.sprite.body.velocity.x = this.hspeed;	}	else if(K_ARROWS.left.isDown){		this.sprite.body.velocity.x = -this.hspeed;	}	if(K_ARROWS.up.isDown && !this.jumped){		this.sprite.body.velocity.y = -this.jump_height;		this.jumped = true;	}	if(this.sprite.body.touching.down)this.jumped = false;	if(K_ARROWS.down.isDown){		this.destroy();	}}this.onRender = function(){	game.debug.body(this.sprite,'#7FFFD4');}        if(typeof this.onCreate == 'function')this.onCreate()    	this.update=function(){		if(typeof this.onUpdate == 'function')this.onUpdate()	};    this.render=function(){        if(typeof this.onRender == 'function')this.onRender()    };    this.destroy=function(){        if(typeof this.onDestroy == 'function')this.onDestroy()        delete this    }}function Ground(x,y,frame){	this._name='Ground';	this.x=typeof x !== 'undefined' ? x : 0;;	this.y=typeof y !== 'undefined' ? y: 0;;    	this.sprite = G_Ground.create(this.x,this.y,'ground',frame);   	OBJECTS.push(this);        this.onCreate = function(){	game.physics.enable(this.sprite, Phaser.Physics.ARCADE);	this.sprite.body.immovable = true;}this.onRender = function(){	game.debug.body(this.sprite,'#7FFFD4');}        if(typeof this.onCreate == 'function')this.onCreate()    	this.update=function(){		if(typeof this.onUpdate == 'function')this.onUpdate()	};    this.render=function(){        if(typeof this.onRender == 'function')this.onRender()    };    this.destroy=function(){        if(typeof this.onDestroy == 'function')this.onDestroy()        delete this    }}function Collision_box(x,y,frame){	this._name='Collision_box';	this.x=typeof x !== 'undefined' ? x : 0;;	this.y=typeof y !== 'undefined' ? y: 0;;    	this.sprite = G_Collision_box.create(this.x,this.y,'32x32collisionbox',frame);   	OBJECTS.push(this);                if(typeof this.onCreate == 'function')this.onCreate()    	this.update=function(){		if(typeof this.onUpdate == 'function')this.onUpdate()	};    this.render=function(){        if(typeof this.onRender == 'function')this.onRender()    };    this.destroy=function(){        if(typeof this.onDestroy == 'function')this.onDestroy()        delete this    }}var state0 = {	OBJECTS:[],	preload: function() {// IMAGES		game.load.spritesheet('player_stand','assets/IMAGES/player_stand.png',18,35,1);		game.load.spritesheet('player_walk','assets/IMAGES/player_walk.png',18,35,2);		game.load.spritesheet('ground','assets/IMAGES/ground.png',33,33,16);		game.load.spritesheet('32x32collisionbox','assets/IMAGES/32x32collisionbox.png',32,32,1);},	create: function() {		G_Player = game.add.group();		G_Player.z = 0;		G_Player.enableBody = true;		G_Player.physicsBodyType = Phaser.Physics.ARCADE;		G_Ground = game.add.group();		G_Ground.z = 0;		G_Ground.enableBody = true;		G_Ground.physicsBodyType = Phaser.Physics.ARCADE;		G_Collision_box = game.add.group();		G_Collision_box.z = 0;		G_Collision_box.enableBody = true;		G_Collision_box.physicsBodyType = Phaser.Physics.ARCADE;K_ARROWS = game.input.keyboard.createCursorKeys();		new Ground(0,762,2.0,'ground');		new Ground(33,762,2.0,'ground');		new Ground(64,762,2.0,'ground');		//... and so on... i took this out just to make this clip shorter},	render: function() {	for (var i=0; i<OBJECTS.length; i++){	if (typeof OBJECTS[i].render == 'function')OBJECTS[i].render()	}},	update: function() {game.physics.arcade.collide(G_Player, G_Ground);	for (var i=0; i<OBJECTS.length; i++){	OBJECTS[i].update()	}},}game.state.add('state0',state0,false);function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    game.state.start('state0', true, true);	game.stage.backgroundColor = '#FFFFFF';}function update () {    game.world.sort('z', Phaser.Group.SORT_DESCENDING)}function render(){    for (var i=0; i<OBJECTS.length; i++){        if (typeof OBJECTS[i].onDraw == 'function')OBJECTS[i].onDraw()        }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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