I’m having an extraordinarily difficult time just trying to draw a graphics object in my MatterJS game. Below is the code I am using. No matter what I do, I always get a dark green box in the same XY location, with the same dark green color, and the same height. Only the width changes with different values of width. I have tried several different colors including all of the pure RGB values, and it’s always dark green.
What am I doing wrong?
var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
backgroundColor: '#1b1464',
parent: 'house-div',
physics: {
default: 'matter'
},
scene: {
preload: preload,
create: create,
update: update
},
"transparent": true
};
var graphics;
var game = new Phaser.Game(config);
function makePlatformTexture(phaserObj, x, y, width, height, color = 0xff0000, alpha = 1) {
const graphics = phaserObj.add.graphics();
const thickness = 2;
graphics.lineStyle(thickness, color, alpha);
graphics.strokeRect(x, y, width, height);
const texture = graphics.generateTexture('platform');
graphics.destroy();
}
// Also tried creating the texture this way, same results
function makePlatformTexture_at_1(phaserObj, x, y, width, height, color = 0xff0000, alpha = 1) {
const thickness = 2;
// const graphics = phaserObj.add.graphics();
const graphics = phaserObj.add.graphics(
{
x: x,
y: y,
linestyle: { width: thickness, color: color, alpha: alpha },
fillStyle: { color: color},
add: true
}
);
const texture = graphics.generateTexture('platform');
graphics.destroy();
}
function addPlatform(phaserObj, x, y, width, height) {
var sprite = phaserObj.matter.add.sprite(width, height, 'platform');
// Stop the platform from falling down.
sprite.body.isStatic = true;
sprite.setIgnoreGravity(true);
sprite.setPosition(x, y);
return sprite;
}
function create ()
{
// Set world bounds for the physics engine to the screen. This means
// any objects in the world can leave the screen or cause it to
// scroll.
this.matter.world.setBounds(0, 0, game.config.width, game.config.height);
// Make the texture used with all our platforms.
// NOTE: Changing this values does not affect anything except the height!
var x = 200;
var y = 20;
var width = 10;
var height = 10;
makePlatformTexture(this, x, y, width, height);
// Add a platform to the game world with physics using the desired rectangle details.
addPlatform(this, x, y, width, height);
}