rbarriuso

Physics on text

3 posts in this topic

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.
 

Share this post


Link to post
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;
Mike and menthos984 like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.