Jump to content

More tiles Problems


DonFrag
 Share

Recommended Posts

Im trying to make a game where the player can run and climb tiles (like ninja gaiden)

for this i have setup a player sprite and two empty child  sprites with body for overlap ground check and front check

 

when the character goes right starts to climb  ground check detect the tile in the overlap

 

FgvKQBq.png

 

but in the opposite way return false

FIN6Udq.png

 

heres is my player code buy i dont know if im doing something wrong

var globalVars= require('../GlobalVars.js'); var STATES={STOP:0,MOVING:1,JUMPING:2,CLIMBING_RIGHT:3,CLIMBING_LEFT:4,MOVE_UPSIDEDOWN:5,FALLING:6};var DIRECTION={NONE:0,UP:1,DOWN:2,LEFT:3,RIGHT:4}; var Player=function (mainGame,startPosition){	this.sprite=game.add.sprite( startPosition.x,startPosition.y, 'player');  //  Phaser.Sprite.call(this,game, startPosition.x,startPosition.y, 'player');     		     this.sprite.anchor.setTo(0.5);          this.groundCheck=game.add.sprite( 0,16, null); 	this.groundCheck.anchor.setTo(0.5); 	this.groundCheck.width=30; 	this.groundCheck.height=16; 	 	 this.frontCheck=game.add.sprite( 16,0, null);  	this.frontCheck.anchor.setTo(0.5);  	this.frontCheck.width=16;  	this.frontCheck.height=8;  	  	  	 	this.sprite.addChild(this.groundCheck); 	this.sprite.addChild(this.frontCheck);  	 	          this.sprite.animations.add('idle',[12]);     this.sprite.animations.add('walk',[12,13,14]);         game.physics.enable(this.sprite, Phaser.Physics.ARCADE);     game.physics.enable(this.groundCheck, Phaser.Physics.ARCADE);     game.physics.enable(this.frontCheck, Phaser.Physics.ARCADE);      this.groundCheck.body.allowGravity=false;     this.frontCheck.body.allowGravity=false;           this.isGrounded=false;     this.isFacing=false;     this.canClimb=false;     this.isClimbing=false;          this.justChangedDirection=false;          this.body=this.sprite.body;                    this.sprite.body.collideWorldBounds = true;    this.sprite.body.bounce.set(0);          this.currentState=STATES.FALLING;    this.currentDirection=DIRECTION.NONE;    this.previousDirection=DIRECTION.NONE;        this.SPEED=100;    this.CLIMB_SPEED=50;     this.sprite.animations.play('idle',1,true);        this.yarnList=[];        //this is for shooting    this.isAiming=false;                //this is form moving    this.myTile=null;    this.targetTile=null;    this.currentTile=null;        game.camera.follow(this);//this is for followers    this.history = [];    this.HISTORY_LENGTH = 5; // Define constants that affect motion    this.MAX_SPEED = 200; // pixels/second    this.MIN_DISTANCE = 32; // pixels     //playerSpecificVars    this.currentHP=3;    this.cursors = game.input.keyboard.createCursorKeys();        //line debug    this.facingText=game.add.text(0, -100, '');    this.groundText=game.add.text(0, -80, '');    this.climbText=game.add.text(0, -70, '');          // Create a bitmap texture for drawing lines    game.camera.follow(this.sprite);};Player.prototype.ClimbTile=function(direction){	//console.log(this.currentTile); 		 this.sprite.body.x=this.currentTile.worldX;	 this.sprite.body.y=this.currentTile.worldY;	 this.ChangeDirection(direction);	 	 } Player.prototype.ChangeToFalling=function(){	this.previusDirection=this.currentDirection; 	this.currentState=STATES.FALLING;			}Player.prototype.ChangeDirection=function(direction){		this.sprite.body.velocity.x=0;	this.sprite.body.velocity.y=0;	switch(direction)	{		case DIRECTION.UP:		  this.isFacing=false;		  this.canClimb=false; 				 				if(this.currentDirection==DIRECTION.RIGHT)		{	 		this.sprite.angle=-90;	 		this.sprite.body.dirty=true;	 		this.previusDirection=this.currentDirection;			this.currentDirection=DIRECTION.UP;			this.currentState=STATES.CLIMBING_RIGHT;		}		else			if(this.currentDirection==DIRECTION.LEFT)				{		  		this.sprite.angle=90; 		  		this.sprite.scale.x=-1; 		 		this.sprite.body.dirty=true;		 						this.previusDirection=this.currentDirection;				this.currentDirection=DIRECTION.UP; 				this.currentState=STATES.CLIMBING_LEFT;								}		 						break;		case DIRECTION.RIGHT:  		this.sprite.angle=0;  		if(this.sprite.scale.x==-1)this.sprite.scale.x=1;		//change the colliders 		this.sprite.body.velocity.y=0;		this.previusDirection=this.currentDirection;		this.currentDirection=DIRECTION.RIGHT;		this.currentState=STATES.MOVING;				break;			case DIRECTION.LEFT:  		this.sprite.angle=0;		if(this.sprite.scale.x==1)this.sprite.scale.x=-1;		//change the colliders 						this.body.velocity.y=0;		this.previusDirection=this.currentDirection;		this.currentDirection=DIRECTION.LEFT;		this.currentState=STATES.MOVING;				break;		}	 	this.justChangedDirection=true;    game.time.events.add(300, function(){    	this.justChangedDirection=false;    }, this);	} Player.prototype.CheckCollisionWorld=function(collisionSide){ 	 	 	 if(!this.isGrounded)	 		 {	 		 	 		 this.currentState=STATES.STOP;	 		 return;	 		 	 		 }	 	 	 	  	 	 	 	 	 	 if(collisionSide.right && this.currentDirection==DIRECTION.RIGHT)	 		 {	 		 this.ChangeDirection(DIRECTION.LEFT);	 		 }	 	 else	 	if(collisionSide.left && this.currentDirection==DIRECTION.LEFT)		 {		 this.MoveOppositeWay();		 return;	 		 		 }	 	if(collisionSide.up && this.currentDirection==DIRECTION.UP)		 {		 this.MoveOppositeWay();		 return;	 		 		 }	 	if(collisionSide.down && this.currentDirection==DIRECTION.DOWN)		 {		 this.MoveOppositeWay();		 return;	 		 		 }		 }  Player.prototype.ControlPlayer=function(){    var inputDown=game.input.activePointer;    if (inputDown.rightButton.isDown)return;      if(this.canMove)    {      	    	switch(this.currentState)    	{    	case STATES.STOP:    		    		if(!this.isAiming)    			this.currentState=STATES.MOVING;    		    		break;    	case STATES.MOVING:    		      		  if(this.justChangedDirection)return;     		       		   switch(this.currentDirection)     		  {     		       		  case DIRECTION.RIGHT:     			        			 if(this.isFacing && this.isGrounded)    			  {    			     			  if(this.canClimb)    				  {     				      			  	this.ChangeDirection(DIRECTION.UP);    				  }    			 else    			 	{			       		this.ChangeDirection(DIRECTION.LEFT);			       	 }    			  }       			   		     		  else    			  if(!this.isGrounded)    				  {    	     			      				  this.currentState=STATES.FALLING;     	     			      				      				  }     			      			  break;     			       			      		  case DIRECTION.LEFT:    			          			 if(this.canClimb && this.isGrounded)     			  {      			  if(this.canClimb)      			  	this.ChangeDirection(DIRECTION.UP);     			 else     			 	{ 			       		this.ChangeDirection(DIRECTION.RIGHT); 			       	 }     			  }        			   		      		  else     			  if(!this.isGrounded)     				  {     	     			       				  this.currentState=STATES.FALLING;      	     			       				       				  }    			  break;    			       			       		  }           			      		    		break;    	///CLIMBING	    	case STATES.CLIMBING_RIGHT:      		  if(this.justChangedDirection)return;       		  console.log(this.isGrounded);     		          		        		if(this.isFacing && this.isGrounded)			  {			  			  	//this.ChangeDirection(DIRECTION.LEFT);			  				  			  }		  else			  if(!this.isGrounded)				  {				  	this.ClimbTile(DIRECTION.RIGHT);				  	 				  }        		break;  		    	case STATES.CLIMBING_LEFT:       		      		  if(this.justChangedDirection)return;     		  console.log(this.isGrounded);      		if(this.canClimb && this.isGrounded)			  {			  			  	//this.ChangeDirection(DIRECTION.RIGHT);			  				  			  }		  else			  if(!this.isGrounded)				  {				  	this.ClimbTile(DIRECTION.LEFT);				  }  			     		break;	 	    		     		case STATES.JUMPING:    			    			    			break;    			    		case STATES.FALLING:    			    			     			        		  if(this.isGrounded)       			  {       			  this.ResumeMoving();       			         			  }    			    			break;    			    			default:    	            this.sprite.body.velocity.set(0,0);    		        }    		     		     }                }Player.prototype.DisableMove=function(canMove){    this.canMove=!canMove;}Player.prototype.getBounds=function(){	return this.sprite.getBounds();}Player.prototype.MoveForward=function(direction){	 	switch(direction)	{		case DIRECTION.RIGHT:				this.sprite.body.velocity.x=this.SPEED;		//animation purpose		if(this.sprite.scale.x==-1)this.sprite.scale.x=1;								break;	case DIRECTION.LEFT:		this.sprite.body.velocity.x=-this.SPEED;		break;			case DIRECTION.UP: 			 				this.sprite.body.velocity.y=-this.CLIMB_SPEED; 				break;	case DIRECTION.DOWN:		this.sprite.body.velocity.y=+this.CLIMB_SPEED;		break;			case DIRECTION.NONE: 		break;	 	}	  		}Player.prototype.MoveOppositeWay=function(){	 	if(this.currentDirection==DIRECTION.RIGHT)		{		this.currentDirection=DIRECTION.LEFT;		return;		}		if(this.currentDirection==DIRECTION.LEFT)	{	this.currentDirection=DIRECTION.RIGHT;	return;	}		if(this.currentDirection==DIRECTION.UP)	{	this.currentDirection=DIRECTION.DOWN;	return;	}		if(this.currentDirection==DIRECTION.DOWN)	{	this.currentDirection=DIRECTION.UP;	return;	}		}Player.prototype.MoveToGoal=function(targetPosition){ var mtgt=game.add.tween(this.sprite.body).to( { x:targetPosition.x,y:targetPosition.y }, 2000, "Linear", true);    mtgt.onComplete.add(function(){        game.add.tween(this.sprite.body).to( { rotation:720 }, 2000, "Linear", true);        var bybyePLayer=game.add.tween(this.sprite.scale).to( { x:0,y:0 }, 2000, "Linear", true);     },this);}Player.prototype.ResumeMoving=function(){	this.currentState=STATES.MOVING;    //this.currentDirection=this.previousDirection;	} Player.prototype.Start=function(){	this.DisableMove(false);	this.currentState=STATES.MOVING;    this.currentDirection=DIRECTION.RIGHT;    this.previousDirection=DIRECTION.RIGHT;    }Player.prototype.Update=function(){			 game.physics.arcade.overlap(this.groundCheck, game.gm.climbLayer,function(body1,tile){ 		 		 if(tile.index==-1)	this.isGrounded=false;else	{	this.isGrounded=true;	this.currentTile=tile;	} 		 	 },null,this);	 	 	game.physics.arcade.overlap(this.frontCheck, game.gm.climbLayer,function(player,tile){  	    		if(tile.index!=-1)		{	   		 this.isFacing=true;			var climb=JSON.parse(tile.properties.climb); 				 this.canClimb=climb;		 										}		    	     	        },null,this);		 	 	 	 	this.MoveForward(this.currentDirection);			 	 	 	 var sprite=this.sprite; 	 		     }  module.exports=Player;

thank you

 

i think that the problem is that when i flip the sprite the body doesnt but...in the game  the body debug show the body like if is in the position

Link to comment
Share on other sites

i found the problem (i think) but the solution is unknown for me.

i changed the offset of groundCheck and now detects both...but i have to moveit too far.

know what i think that happens is that isGrounded detect both tiles, the tile empty and the wall.

when climbs right the wall enter first to the overlap check so everything goes fine. but in the left climb the empty tile comes first.

so the problem its the groundcheck overlap two tiles at the time, but i need that overlap just the wall and terrain.

:/

Link to comment
Share on other sites

Just skimmed very quickly through your code, but I think it might be a similar issue that I had a few days ago.

If you look in Bounds.js(in Phaser's source code), you will see this somewhere:

    /**    * The right coordinate of the Game Object.    * This is the same as `x + width - offsetX`.    *    * @property {number} right    * @readOnly    */    right: {        get: function () {            return (this.x + this.width) - this.offsetX;        }    },

Now, that's all fine and dandy while the sprite's scale.x is a positive number, but if it's a negative number, sprite.width also becomes negative, and actually inverts(becoming left instead of right, sort of).

What might fix it, is if you would make it so that sprite.left is still a lower number than sprite.right.

    /**    * The right coordinate of the Game Object.    * This is the same as `x + width - offsetX`.    *    * @property {number} right    * @readOnly    */    right: {        get: function () {            return this.x + (Math.abs(this.width) - Math.abs(this.offsetX));        }    },

This way, sprite.right is actually sprite.right, so to speak. Do the same for left:

    left: {        get: function() {            return this.x - Math.abs(this.width);        }    }

And I think that would solve it.

 

In my case, I made my own physics system, so I could easily and safely use custom properties. But if this is indeed what nags you, the quickest way to try it out is to edit the source code of Phaser a bit. After all, the physics systems delivered with Phaser will probably seek out those getters.

 

Again, I'm not entirely sure if this is the problem, but it certainly is counter-intuitive.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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