Hi,
I am doing a failing balls game and want to contol final destination of each ball( e.g. slot 1, 2 or 3). So everything should looks like they falling down natively.
I tried to set velocityX on each collision, but its often goes into wrong direction.
Any ideas how to do that ?
var config = {
type: Phaser.AUTO,
width: 480,
height: 605,
backgroundColor: '#cecece',
parent: 'phaser-container',
physics: {
default: 'matter',
matter: {
enableSleeping: true,
debug: true,
}
},
scene: {
preload: preload,
create: create
}
};
function create() {
this.options = {
plinkoCols: 6,
plinkoRows: 11,
ballCount: 5,
dropArea: {
x: 90,
y: 110,
width: 304,
height: 476
},
ItemGrid: {
X: 130,
Y: 120,
ItemSpacingX: 44,
ItemSpacingY: 15,
NumColumns: 5
},
PrizesGrid: {
X: 78,
Y: 536,
ItemSpacingX: 110,
ItemSpacingY: 0,
NumColumns: 3
}
};
var dropArea = this.options.dropArea;
this.matter.world.setBounds(dropArea.x, dropArea.y, dropArea.width, dropArea.height, 32, true, true, false, true);
for (var i = 0; i < this.options.PrizesGrid.NumColumns; i++) {
var row = Math.floor(i / this.options.PrizesGrid.NumColumns);
var column = i % this.options.PrizesGrid.NumColumns;
var xPos = this.options.PrizesGrid.X + column * this.options.PrizesGrid.ItemSpacingX;
var yPos = this.options.PrizesGrid.Y + row * this.options.PrizesGrid.ItemSpacingY;
if (i !== 0) {
var sprite = this.matter.add.image(xPos, yPos, 'bottomSplitor', null, { isStatic: true });
sprite.setOrigin(0.5, 0.5);
}
}
// Add in a stack of balls
var plinkoRadius = 4;
this.pins = [];
var width = this.options.dropArea.width / this.options.plinkoCols;
for (var y = 0; y < this.options.plinkoRows - 1; y++) {
var yPos = this.options.dropArea.y + 20 + (y + 1) * 32;
for (var i = 0; i < this.options.plinkoCols - 1; i++) {
var xPos = (i + 1) * width + this.options.dropArea.x + y % 2 * width / 2 - plinkoRadius - 10;
var pin = this.matter.add.image(xPos, yPos, 'pin');
pin.setCircle();
pin.setStatic(true);
pin.body.label = 'pin';
pin.body.column = i;
pin.body.row = y;
this.pins.push(pin);
}
}
this.balls = [];
for (var i = 0; i < this.options.ballCount; i++) {
var column = i % (this.options.ItemGrid.NumColumns + 1);
var xPos = this.options.ItemGrid.X + column * this.options.ItemGrid.ItemSpacingX + this.options.ItemGrid.ItemSpacingX / 2;
var ball = this.matter.add.image(xPos, this.options.ItemGrid.Y + this.options.ItemGrid.ItemSpacingY / 2, 'ball' + i);
ball.setCircle();
ball.setFriction(0.005);
ball.setBounce(0.5 * Math.random() + 0.1);
console.log(ball.body.restitution);
ball.setToSleep();
ball.body.label = 'ball';
this.balls.push(ball);
}
var self = this;
var r1 = this.add.rectangle(0, 0, 130, 130, 0x6666ff);
r1.setInteractive();
r1.on('pointerup', function () {
for (var i = 0; i < self.balls.length; i++) {
var ball = self.balls[i];
ball.setAwake();
//break;
}
});
}

