Jump to content

Physics on text


rbarriuso
 Share

Recommended Posts

Hi all,

I'm trying to enable physics on a text object but it seems it doesn't work. Here's the code:
 

//In the "create" function of my Phaser.Statethis.physics.startSystem(Phaser.Physics.ARCADE);// ...var text = this.add.text(this.world.centerX-100, this.world.centerY-200, "MyText", { font: 'bold 100pt Arial', fill: "#8888FF", align: 'center' });text.anchor.setTo(0.5);text.setShadow(5, 5, 'rgba(0,0,0,0.5)', 5);this.physics.enable(text, Phaser.Physics.ARCADE);text.body.bounce.y = 1;text.body.gravity.y = 2000;text.body.collideWorldBounds = true;

I get an error because "text.body" is not defined (line 8 in the code above).
I supposed "Phaser.Text" supported physics since it inherits from "Sprite".

Is this an expected limitation?

Thanks in advance,
  Rafa.
 

Link to comment
Share on other sites

Only Sprites can have physics enabled, but you can create an empty sprite and attach the text object to it with addChild:

var text = this.add.text(0, 0, "MyText", { font: 'bold 100pt Arial', fill: "#8888FF", align: 'center' });text.anchor.setTo(0.5);text.setShadow(5, 5, 'rgba(0,0,0,0.5)', 5);var textSprite = this.add.sprite(this.world.centerX-100, this.world.centerY-200, null);textSprite.addChild(text);this.physics.enable(textSprite, Phaser.Physics.ARCADE);textSprite.body.bounce.y = 1;textSprite.body.gravity.y = 2000;textSprite.body.collideWorldBounds = true;
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...