Jump to content

Physics on text


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,

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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