cap_mkt_tools
cap_mkt_tools

Reputation: 98

Sprite cannot be fixed in 2D in Phaser

I am running ubuntu 14.04 with Phaser (v2.3.0 "Tarabon" - Built: Thu Mar 26 2015 02:36:37).

I am trying to have two sprites where one of them (yellowRectangle) should be fixed and the other one (redSquare) moves toward the fixed sprite.

The issue is that the sprite that should be fixed still moves when the two sprites collide.

I attempted the workaround to set yellowRectangle.physicsType = Phaser.SPRITE; but it does not help.

Here is the short program:

var game = new Phaser.Game(width="100", height="100", Phaser.AUTO,'pressIt', { preload: preload, create: create, update:update });

var redSquare; var yellowRectangle;


function preload() {
    game.load.image('redSquare', '/redSquare25x25.png');
    game.load.image('yellowRectangle', '/yellowRectangle100x10.png');
}

function create() {
    game.physics.startSystem(Phaser.Physics.P2JS);

    redSquare = game.add.sprite(x=game.world.centerX, y=35, 'redSquare');
    game.physics.p2.enable(redSquare);

    yellowRectangle = game.add.sprite(x=game.world.centerX, y=game.world.height - 100, 'yellowRectangle');
    //was suggested to add due to bug in Phaser, but does not fix it
    //yellowRectangle.physicsType = Phaser.SPRITE;
    game.physics.p2.enable(yellowRectangle);
    yellowRectangle.body.moves = false;
    yellowRectangle.immovable = true; 
};

function update() {
    redSquare.body.force.y = 500; 
};

Upvotes: 0

Views: 150

Answers (1)

beagleknight
beagleknight

Reputation: 668

You should set the yellow rectangle as immovable like this:

yellowRectangle.body.immovable = true;

You just forgot the body part ;)

Hope it helps!

Upvotes: 1

Related Questions