What is the problem when an object falls even though collision physics is added to the object
It depends. Are any collisions working? Can you show the code?
import Phaser from "../lib/phaser.js";
import Coins from '../game/coins.js';
export default class Game extends Phaser.Scene
{
constructor() {
super("game");
}
preload() {
//LOAD THE SOUNDS AND IMAGES
this.load.image("platform", "assets/Platform/platformIndustrial_037.png");
this.load.image("obstacle1", "assets/Platform/platformIndustrial_071.png");
this.load.image("obstacle2", "assets/Platform/platformIndustrial_072.png");
this.load.image("obstacle3", "assets/Platform/platformIndustrial_083.png");
this.load.image(
"baricade_Left",
"assets/Platform/platformIndustrial_087.png"
);
this.load.image(
"baricade_Right",
"assets/Platform/platformIndustrial_089.png"
);
this.load.image(
"baricade_Middle",
"assets/Platform/platformIndustrial_088.png"
);
this.load.image("obstacle4", "assets/Platform/platformIndustrial_097.png");
this.load.image(
"player2",
"assets/Character/Male person/PNG/Poses HD/character_malePerson_idle.png"
);
this.load.atlas(
"player1",
"assets/Character/Male adventurer/Tilesheet/spritesheet (1).png",
"assets/Character/Male adventurer/Tilesheet/sprites.json"
);
this.load.image(
"obstacle5",
"assets/Platform/platformIndustrial_019.png");
this.load.image(
"obstacle6",
"assets/Platform/platformIndustrial_033.png"
);
this.load.image ("coins", "assets/Coin/coin_01.png")
}
create() {
//ADD MORE FUNCTIONALITIES
//ENABLING BAR LINE
//This adds platform1
this.platform1 = this.physics.add.staticGroup();
// this puts the platform in place
for (let i = -9; i < 60; ++i) {
const x = 0 + i * 70;
const y = 390;
const platform = this.platform1.create(x, y, "platform");
platform.scale = 0.5;
platform.body.setSize(platform.width * 0.12, platform.height * 0.01);
}
this.player1 = this.physics.add.sprite(100, 190, "player1").setScale(1.2).refreshBody();
this.physics.add.collider(this.player1, this.platform1);
//ENABLING UPPER PLATFROMS
this.obstacle1 = this.physics.add.staticGroup();
this.obstacle1.create(300, 320, "obstacle1").setScale(0.5).refreshBody();
this.obstacle2 = this.physics.add.staticGroup();
this.obstacle2.create(390, 300, "obstacle2").setScale(0.8).refreshBody();
this.physics.add.collider(this.player1, this.obstacle2);
this.obstacle3 = this.physics.add.staticGroup();
let obstacle3n = this.obstacle3
.create(560, 280, "obstacle3")
.setScale(1.5)
.refreshBody();
obstacle3n.body.setSize(obstacle3n.width * 0.8, obstacle3n.height * 1);
this.physics.add.collider(this.player1, this.obstacle3);
this.baricadeL = this.physics.add.staticGroup();
let baricadeLn = this.baricadeL
.create(800, 315, "baricade_Left")
.setScale(0.6)
.refreshBody();
baricadeLn.body.setSize(baricadeLn.width * 0.6, baricadeLn.height * 0.3);
this.physics.add.collider(this.player1, this.baricadeL);
this.baricadeM = this.physics.add.staticGroup();
let baricadeMn = this.baricadeM
.create(885, 315, "baricade_Middle")
.setScale(0.6)
.refreshBody();
baricadeMn.body.setSize(baricadeMn.width * 0.6, baricadeMn.height * 0.3);
this.physics.add.collider(this.player1, this.baricadeM);
this.baricadeR = this.physics.add.staticGroup();
let baricadeRn = this.baricadeR
.create(970, 315, "baricade_Right")
.setScale(0.6)
.refreshBody();
baricadeRn.body.setSize(baricadeRn.width * 0.6, baricadeRn.height * 0.3);
this.physics.add.collider(this.player1, this.baricadeR);
this.obstacle4 = this.physics.add.staticGroup();
this.obstacle4.create(1300, 290, "obstacle4").setScale(0.9).refreshBody();
this.physics.add.collider(this.player1, this.obstacle4);
this.obstacle5 = this.physics.add.staticGroup();
this.obstacle5.create(1600, 310, "obstacle5").setScale(0.6).refreshBody();
this.physics.add.collider(this.player1, this.obstacle5);
this.obstacle6 = this.physics.add.staticGroup();
this.obstacle6.create(1900, 290, "obstacle6").setScale(1).refreshBody();
this.physics.add.collider(this.player1, this.obstacle6);
this.obstacle7 = this.physics.add.staticGroup();
let obstacle7n = this.obstacle7
.create(2040, 290, "obstacle6")
.setScale(1)
.refreshBody();
obstacle7n.body.setSize(obstacle7n.width * 0.6);
this.physics.add.collider(this.player1, this.obstacle7);
//Create all animation here
this.anims.create({
key: "standing",
frames: this.anims.generateFrameNames("player1", {
prefix: "stand_",
start: 0,
end: 0,
zeroPad: 4,
}),
repeat: -1,
frameRate: 10,
});
this.anims.create({
key: "running",
frames: this.anims.generateFrameNames("player1", {
prefix: "run_",
start: 0,
end: 2,
zeroPad: 4,
}),
repeat: -1,
frameRate: 10,
});
this.anims.create({
key: "walking",
frames: this.anims.generateFrameNames("player1", {
prefix: "walk_",
start: 0,
end: 7,
zeroPad: 4,
}),
repeat: -1,
frameRate: 10,
});
this.anims.create({
key: "jumping",
frames: this.anims.generateFrameNames("player1", {
prefix: "jump_",
start: 0,
end: 2,
zeroPad: 4,
}),
repeat: -1,
frameRate: 9,
});
//Adding camera to player1
this.cameras.main.startFollow(this.player1, true, 0.05, 0.05)
this.cursors = this.input.keyboard.createCursorKeys();
//Adding coins
this.coins = this.physics.add.group({
classType: Coins
})
this.physics.add.collider(this.platform1, this.coins)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//This adds platform2
this.platform2 = this.physics.add.staticGroup();
// this puts the platform in place
for (let i = -9; i < 60; ++i) {
const x = 0 + i * 70;
const y = 730;
const platform = this.platform2.create(x, y, "platform");
platform.scale = 0.5;
platform.body.setSize(platform.width * 0.12, platform.height * 0.01);
}
// ENABLING LOWER PLATFORMS (s- same)
this.obstacle1s = this.physics.add.staticGroup();
this.obstacle1s.create(300, 660, "obstacle1").setScale(0.5).refreshBody();
this.obstacle2s = this.physics.add.staticGroup();
this.obstacle2s.create(390, 640, "obstacle2").setScale(0.8).refreshBody();
let obstacle3s = this.physics.add.staticGroup();
let obstacle3sn = obstacle3s
.create(560, 620, "obstacle3")
.setScale(1.5)
.refreshBody();
obstacle3sn.body.setSize(obstacle3sn.width * 0.8, obstacle3sn.height * 1);
let baricadeLs = this.physics.add.staticGroup();
let baricadeLsn = baricadeLs
.create(800, 655, "baricade_Left")
.setScale(0.6)
.refreshBody();
baricadeLsn.body.setSize(baricadeLsn.width * 0.6, baricadeLsn.height * 0.3);
let baricadeMs = this.physics.add.staticGroup();
let baricadeMsn = baricadeMs
.create(885, 655, "baricade_Middle")
.setScale(0.6)
.refreshBody();
baricadeMsn.body.setSize(baricadeMsn.width * 0.6, baricadeMsn.height * 0.3);
let baricadeRs = this.physics.add.staticGroup();
let baricadeRsn = baricadeRs
.create(970, 655, "baricade_Right")
.setScale(0.6)
.refreshBody();
baricadeRsn.body.setSize(baricadeRsn.width * 0.6, baricadeRsn.height * 0.3);
this.obstacle4s = this.physics.add.staticGroup();
this.obstacle4s.create(1300, 635, "obstacle4").setScale(0.9).refreshBody();
this.obstacle5s = this.physics.add.staticGroup();
this.obstacle5s.create(1600, 650, "obstacle5").setScale(0.6).refreshBody();
this.obstacle6s = this.physics.add.staticGroup();
this.obstacle6s.create(1900, 630, "obstacle6").setScale(1).refreshBody();
let obstacle7 = this.physics.add.staticGroup();
let obstacle7sn = obstacle7
.create(2040, 630, "obstacle6")
.setScale(1)
.refreshBody();
obstacle7sn.body.setSize(obstacle7sn.width * 0.6);
}
update(t, dt) {
if (this.cursors.left.isDown) {
this.player1.setVelocityX(-300);
this.player1.anims.play("running", true);
} else if (this.cursors.right.isDown) {
this.player1.setVelocityX(300);
this.player1.anims.play("running", true);
} else if (this.cursors.up.isDown) {
this.player1.setVelocityY(-300);
this.player1.anims.play("jumping", true);
} else {
this.player1.setVelocityX(0);
this.player1.anims.play("standing", true);
}
}}
//IMPROVEMENT:
// made the game in the center, add more obstacles, everything is running ok now
//NEXT:
// add coins, add collision to the player and charcter, add scoreboard.
I don’t know what the problem is in this code, and the object starts falling when I press the arrow keys
In the game config, add debug: true
to the Arcade Physics section, like
new Phaser.Game({
physics: {
default: 'arcade',
arcade: {
debug: true
}
}
});
Then check if the physics bodies are in the right place.