Jump to content

Block Collision Glitch


nintendofan85
 Share

Recommended Posts

HI, I am new to the forum and I really need help on fixing a major bug in my Phaser 3 Project. I am making a platformer level editor where can place blocks for the player. However, if the user creates a wall of blocks and the player jumps right next to it, it causes the player to jump repeatedly as if they are standing on the platform or not be able to jump at all. Does anyone know how to fix this? Here is the code for my engine if it helps: 

class Engine extends Phaser.Scene {
	constructor() {
    super("playGame");
  }

	create(data){

		this.events.on('pause', function(){
			this.resetLevel(this, this.player.body.enable);
		}, this);

		this.events.on('resume', function(){
			this.cameras.main.startFollow(this.player, true);
		}, this);

		this.events.on('moveCam', function(x, y){
				this.camX.scrollX += x;
				this.camX.scrollY += y;
				this.text.setText(this.camX.scrollX);

				this.backgroundX.tilePositionX = this.camX.scrollX;
				this.backgroundY.tilePositionX = this.camX.scrollX;
				this.backgroundY.tilePositionY = this.camX.scrollY;

		}, this);

		var levelWidth = 1;
		var levelHeight = 1;

		var scene = this.scene.get("editor");

		this.setUpBackgrounds(levelWidth*1000, levelHeight*1000);

		this.player = this.physics.add.sprite(300, (levelHeight*1000) - 500, "Mario");
		this.player.anims.play("idle");

		this.player.startX = this.player.x;
		this.player.startY = this.player.y;

		this.player.on('decapture', this.ejectCapture, this);
		this.player.on('capture', this.deactivatePlayer, this);

		this.enemies = this.physics.add.group();

		this.captures = this.physics.add.group();

		this.ground = this.physics.add.staticGroup();
		this.blocks = this.physics.add.staticGroup();
        //Blocks are 32 by 32 pixels


		this.block = this.blocks.create(800, levelHeight*1000 - 400, "? Block");
		this.block.anims.play("? Block_Anim");
		//this.ground.create(400, levelHeight*1000 - 150, "platform");

		this.cursors = this.input.keyboard.createCursorKeys();

	  this.player.specialMove = "none";

		this.player.setSize(45, 70);
	  this.player.speed = 8;
		this.player.running = false;
		this.player.wearingCap = true;
		this.player.rollTimer = 0;

		this.player.jumpCounter = 0;
		//For Double and Triple Jumps

		this.capHold = false;
		this.player.throwDelay = 0;

		this.setUpCamera(levelWidth*1000, levelHeight*1000);
		this.scene.launch("editor");

		this.scene.bringToTop("editor");

		this.physics.add.collider(this.ground, this.enemies);
		this.physics.add.collider(this.platforms, this.player);
		this.physics.add.collider(this.ground, this.player, function() {
					if(this.player.specialMove == "none"){
						// if(this.player.flipX && this.cursors.left.isDown && this.player.body.touching.left){
						// 	this.player.specialMove = "wallSlide";
						// }else if(!this.player.flipX && this.cursors.right.isDown && this.player.body.touching.right){
						// 	this.player.specialMove = "wallSlide";
						// }
					}
					if(this.player.specialMove == "wallSlide"){
						this.player.setVelocityY(100);
					}
				}, null, this);

		this.physics.add.collider(this.player, this.block, function() {
			if(this.player.y > this.block.y + 32 || this.player.specialMove == "dive" || this.player.specialMove == "roll"){
				this.block.anims.play("? Block_Hit");
			}
		}, null, this);

		this.cap = initCap(this.physics.add.sprite(150, 100, "cap"));

		this.physics.add.collider(this.cap, this.block, function() {
			if(!this.cap.capReturn){
					this.block.anims.play("? Block_Hit");
			}
		}, null, this);

		this.physics.add.collider(this.ground, this.cap);

    this.text = this.add.text(10, 30, '', { font: '16px Courier', fill: '#00ff00' });
		this.text.setScrollFactor(0);

		this.keyX = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);
		this.keyZ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);

		this.player.costume = "";

		this.player.size = new Phaser.Structs.Size(45, 50);

		// var koopa = new Koopa(this, this.player.x + 60, this.player.y - 150);
		// this.enemies.add(koopa);
		// koopa.setDisplaySize(50, 50);
		// koopa.initObj();

		//Do not delete
		//this.button = this.add.image(800-16, 300, 'fullscreen', 0).setOrigin(1, 0).setInteractive();

		/*this.button.on('pointerup', function () {

            if (this.scale.isFullscreen)
            {
                this.button.setFrame(0);

                this.scale.stopFullscreen();
            }
            else
            {
                this.button.setFrame(1);

                this.scale.startFullscreen();
            }

        }, this);*/

			}

			update(){
				this.text.setText(Math.round(this.player.body.velocity.x));
				var renderScene = this.scene.get("displayGame");

				//console.log(renderScene.camX);

			  this.backgroundX.tilePositionX = this.camX.scrollX;

				this.backgroundY.tilePositionX = this.camX.scrollX;
				this.backgroundY.tilePositionY = this.camX.scrollY;

				if(this.player.wearingCap){
					this.player.costume = "_Hat";
				}else{
					this.player.costume = "";
				}

				// if(this.cursors.up.isDown){
				// 	this.camX.scrollY -= 10;
				// }
				// if(this.cursors.down.isDown){
				// 	this.camX.scrollY += 10;
				// }
				//
				// if(this.cursors.left.isDown){
				// 	this.camX.scrollX -= 10;
				// }
				// if(this.cursors.right.isDown){
				// 	this.camX.scrollX += 10;
				// }

				// if(this.cursors.up.isDown){

				// }

				//console.log(this.player.body.onWall());


				if(this.player.specialMove == "none"){
					if(this.player.body.velocity.y >= 0){
						if (this.player.flipX && this.cursors.left.isDown && !this.keyX.isDown && this.player.body.onWall() && !this.player.body.onFloor()) {
							this.player.specialMove = "wallSlide";
						}else if (!this.player.flipX && this.cursors.right.isDown && !this.keyX.isDown && this.player.body.onWall()  && !this.player.body.onFloor()) {
							this.player.specialMove = "wallSlide";
						}
					}

					if(this.cursors.down.isDown && this.player.body.onFloor()){
						this.player.anims.play("duck" + this.player.costume, true);
						if(Math.abs(this.player.body.velocity.x) < 5){
							this.player.body.setVelocityX(0);
						}else{
							if(this.player.body.velocity.x > 0){
								this.player.body.velocity.x -= 3.5;
							}else{
								this.player.body.velocity.x += 3.5;
							}
						}
						if(this.keyX.isDown){
							  this.player.setVelocityY(-600);
						}
						if(this.keyZ.isDown && this.keyZ.getDuration() < 50){
							this.player.specialMove = "roll";
							this.player.anims.play("rollAnim" + this.player.costume);
						}
					}else{
					  playerMovement(this.player, this.cursors, this.keyX.isDown, this.keyZ.isDown, this.player.body.onFloor());
					}
				}else{
					this.player.jumpCounter = 0;
					if(this.player.specialMove == "groundPound"){
						if(this.player.anims.isPlaying){
							this.player.body.allowGravity = false;
							this.player.body.setVelocityX(0);
							this.player.setVelocityY(0);
						}else{
							this.player.body.allowGravity = true;
							this.player.setVelocityY(800);
							if (this.player.body.onFloor()) {
								this.player.specialMove = "none";
								if(this.keyX.isDown && this.keyX.getDuration() < 50){
									this.player.setVelocityY(-700);
								}else if(this.keyZ.isDown && this.keyZ.getDuration() < 40){
									this.player.specialMove = "roll";
									this.player.rollTimer = 0;
									this.cap.numPress = 0;
									if(this.player.flipX){
										this.player.setVelocityX(-800);
									}else{
										this.player.setVelocityX(800);
									}
								}
							}
						}
						if(this.cursors.up.isDown && !this.player.body.onFloor()){
							this.player.specialMove = "dive";
							this.player.setVelocityY(-200);
						}
					}

					if(this.player.specialMove == "dive"){
						this.player.body.allowGravity = true;
						this.player.anims.play("dive" + this.player.costume);
						if(this.player.flipX){
							this.player.body.setVelocityX(-300);
						} else {
							this.player.body.setVelocityX(300);
						}
						if (this.player.body.onFloor()) {
							this.player.specialMove = "none";
						}else{
							if(this.player.body.onWall()){
								this.player.specialMove = "kickback";
							}
						}
					}

					if(this.player.specialMove == "kickback"){
						if(this.player.body.onFloor()){
							this.player.specialMove = "none";
						}else{
							if(this.player.flipX){
								this.player.body.setVelocityX(100);
							}else{
								this.player.body.setVelocityX(-100);
							}
						}
					}


					if (this.player.specialMove == "roll") {
						if(this.player.anims.isPlaying){
							if (Math.abs(this.player.body.velocity.x) != 800) {
								if (this.keyZ.isDown && this.keyZ.getDuration() < 50) {
									this.player.rollTimer +=5;
								}
								this.player.rollTimer--;
								if(this.player.rollTimer > 10){
									this.player.rollTimer = 10;
								}
								if(this.player.rollTimer < 0){
									this.player.rollTimer = 0;
								}
								if(this.player.flipX){
									this.player.body.setVelocityX(-300 + (this.player.rollTimer*-15));
								}else{
									this.player.body.setVelocityX(300 + (this.player.rollTimer*15));
								}
							}
							 if(this.keyX.isDown && this.player.body.onFloor() && this.keyX.getDuration() < 50){
							 	this.player.specialMove = "longJump";
								this.player.setVelocityY(-400);
							 }

						} else {
							if(this.keyZ.isDown || this.player.rollTimer > 5){
								this.player.anims.play("rollAnim" + this.player.costume);
							}else{
								if(this.player.body.onFloor()){
									this.player.anims.play("idle");
									this.player.specialMove = "none";
									this.player.rollTimer = 0;
								}
							}
						}
					}

					if(this.player.specialMove == "longJump"){
						this.player.anims.play("longJump" + this.player.costume);
						if(this.player.flipX){
							this.player.body.setVelocityX(-350);
						}else{
							this.player.body.setVelocityX(350);
						}
						if(this.player.body.onFloor() && this.player.body.velocity.y > -1){
							this.player.specialMove = "none";
						}
					}

					if(this.player.specialMove == "wallSlide"){
						this.player.anims.play("wallSlide" + this.player.costume);
						if(this.player.flipX){
							if(!this.cursors.left.isDown || this.player.body.onFloor() || (!this.player.body.onWall() && Math.abs(100 - this.player.body.velocity.y) > 420)){
								this.player.specialMove = "none";
							}else if(this.keyX.isDown){
								this.player.setVelocityY(-400);
								this.player.body.setVelocityX(250);
								this.player.flipX = false;
								this.player.specialMove = "none";
							}
						}else{
							if(!this.cursors.right.isDown || this.player.body.onFloor() || (!this.player.body.onWall() && Math.abs(100 - this.player.body.velocity.y) > 420)){
								this.player.specialMove = "none";
							}else if(this.keyX.isDown){
								this.player.setVelocityY(-400);
								this.player.body.setVelocityX(-250);
								this.player.flipX = true;
								this.player.specialMove = "none";
							}
						}
					}
				}

				//If the player is ducking, shrink the hitbox
				if(this.player.specialMove == "roll" || this.player.specialMove == "longJump"){
					this.player.setSize(45);
				}else{
					if((this.player.anims.getCurrentKey() == "duck" || this.player.anims.getCurrentKey() == "duck_Hat") && this.player.specialMove == "none"){
						this.player.setSize(45);
					}else{
						 this.player.setSize(45, 70);
					}
				}

				throwCap(this.cap, this.player, this.keyZ.isDown &&
					this.keyZ.getDuration() < 15 && this.player.wearingCap
					&& !this.cap.capReturn && this.player.specialMove == "none"
					&& this.player.anims.getCurrentKey() != ("duck" + this.player.costume) && this.player.body.enable);

				if(!this.player.wearingCap){
					if(this.cap.flipX){
						this.cap.body.velocity.x += 10;
						if(this.cap.body.velocity.x > 0){
							this.cap.setVelocityX(0);
							if(!this.capHold){
								if(this.keyZ.isDown && !this.cap.capReturn){
									this.capHold = true;
									this.cap.numPress = 0;
								}else{
									this.cap.capReturn = true;
									this.timedEvent = this.time.addEvent({
										delay: 100, callback: this.capReturn,
										callbackScope: this, repeat: 0
									});
								}
							}else{
								this.capPause(this.keyZ.isDown);
							}
						}
					}else{
						this.cap.body.velocity.x -= 10;
						if(this.cap.body.velocity.x < 0){
							this.cap.setVelocityX(0);
							if(!this.capHold){
								if(this.keyZ.isDown && !this.cap.capReturn){
									this.capHold = true;
									this.cap.numPress = 0;
								}else{
									this.cap.capReturn = true;
									this.timedEvent = this.time.addEvent({
										delay: 100, callback: this.capReturn,
										callbackScope: this, repeat: 0
									});
								}
							}else{
								this.capPause(this.keyZ.isDown);
							}
						}
					}
				}
			}

			capReturn(capHold){
				this.capHold = false;
				this.cap.capDelay = 0;
				if(this.player.x - 20 > this.cap.x){
					this.cap.x += 5;
				}
				if(this.player.x + 20 < this.cap.x){
						this.cap.x -= 5;
				}

				if(this.player.y - 10 < this.cap.y){
					this.cap.y -= 5;
				}
				if(this.player.y + 10 > this.cap.y){
					this.cap.y +=5;
				}
				this.cap.touchPlayer = this.physics.add.overlap(this.player, this.cap, function (){
					if(this.cap.capReturn){
						this.physics.world.removeCollider(this.cap.touchPlayer);
						this.cap.disableBody(true, true);
						this.player.wearingCap = true;
						this.cap.capReturn = false;
						this.timedEvent.remove(false);
					}
				}, null, this);
			}


			capPause(keyDown){
					this.physics.world.overlap(this.player, this.cap, function(){
						if(this.player.y < this.cap.y || this.player.body.onFloor()){
							this.player.body.setVelocityY(-600);
						}else{
							this.player.body.setVelocityY(-300);
						}
						if(this.player.specialMove == "dive"){
							this.player.specialMove = "none";
						}
						this.cap.numPress = 300;
					}, null, this);

				if(keyDown){
					this.cap.numPress++;
				}else{
					this.cap.numPress = 300;
				}
				if(this.cap.numPress >= 300){
					this.cap.capReturn = true;
					this.capHold = false;
				}
			}

			floorDetection(){
			  var bool = false;
				this.physics.world.overlap(this.player, this.cap, function(){
					bool = true;
				}, null, this);
				return bool;
			}

			setUpCamera(width, height){
				this.camX = this.cameras.main.setBounds(0, 0, width, height);
				this.cameras.main.startFollow(this.player, true);
			}

			setUpBackgrounds(width, height){
				this.backgroundY = this.add.tileSprite(0, 0, config.width, config.height, "background_y");
				this.backgroundY.setOrigin(0, 0);

				if(height >= 1000){
					this.backgroundX = this.add.tileSprite(0, height - 1000, config.width, config.height, "background");
				  this.backgroundX.setOrigin(0, 0);
				}else{
					this.backgroundX = this.add.tileSprite(0, -400, config.width, config.height, "background");
				  this.backgroundX.setOrigin(0, 0);
				}

				this.backgroundX.scrollFactorX = 0;
				this.backgroundY.setScrollFactor(0);
			}

			deactivatePlayer (scene, npc){
    		scene.player.disableBody(true, true);
				scene.cap.disableBody(true, true);
			  scene.cameras.main.stopFollow();
				scene.setUpCapture(scene, "goomba", npc);
			}

			ejectCapture (scene){
    		scene.player.body.setVelocityY(-500);
				scene.player.specialMove = "none";
				scene.player.wearingCap = true;
				scene.cap.capReturn = false;
				scene.cameras.main.stopFollow();
				scene.cameras.main.startFollow(scene.player, true);
			}

			setUpCapture(scene, captureType, npc){
				if(captureType == "goomba"){
					scene.capture = new Capture(scene, npc.x, npc.y, npc);
					scene.cameras.main.startFollow(scene.capture, true);
					scene.captures.add(scene.capture);
					scene.physics.add.collider(scene.ground, scene.capture);
				}
			}

			resetLevel(scene, playerAlive){
				scene.player.enableBody(true, scene.player.startX, scene.player.startY, true, true);
				scene.player.specialMove = "none";
				scene.player.wearingCap = true;
				scene.cap.disableBody(true, true);
				scene.cap.capReturn = false;
				scene.cameras.main.stopFollow();
	 			 for(var i = 0; i < scene.captures.getLength(); i++){
	 				 var g = scene.captures.getChildren()[i];
	 				 if (!g.captureActive) {
	 				 		g.timedEvent.remove(false);
	 				 }
	 			 }
	 			 scene.captures.clear(true, true);
	 			 for(var i = 0; i < scene.enemies.getLength(); i++){
	 				 var c = scene.enemies.getChildren()[i];
	 				 c.respawn();
	 			 }
			}
}

 

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...