Jump to content

How to add rotation property in the phaser group


parwaniprakash
 Share

Recommended Posts

Hello Everyone,

        I got stuck in one place. I had created a group of phaser in create function and create an object. and In update function I just rotate the object on the perticular point. But when I create a new object in the update function and try to add the rotation property then it is showing the error of velocity undefine.

        My purpose is to create multiple objects in phaser group type after clicking on canvas and that objects should move in a given path of arrays. Means different object should move on different path defined in array.

      here is the code :-

<!doctype html> <html> <head>   <title>Phaser</title>   <script type="text/javascript" src="libs/js/phaser.js"></script> </head> <body>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create, update:update, render: render });
var timeCheck = 0;
var path1 = [new Phaser.Point(100, 500),new Phaser.Point(500, 500)];
var path2 = [new Phaser.Point(200, 500),new Phaser.Point(700, 300)]
var sprite =  sprite1 = null;

function preload()
{
    game.load.image('star', 'img/arrow.png');
}


function create() {

    game.stage.backgroundColor = '#454645';
    graphic = game.add.graphics(0, 0);

    enemies = game.add.group();
    sprite = enemies.create(360 + Math.random() * 200, 120 + Math.random() * 200, 'star');

    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.physics.enable(enemies, Phaser.Physics.ARCADE);


    graphic.lineStyle(10, '0xff0000', 1);
    graphic.moveTo(100, 500);
    graphic.lineTo(500, 500);
}

function update()
{
    graphic.lineStyle(10, '0x00ff00', 1);
    graphic.moveTo(100, 500);
    graphic.lineTo(700, 300);    


    graphic.lineStyle(10, '0x0000ff', 1);
    graphic.moveTo(700, 300);
    graphic.lineTo(300, 300);    

    if (game.input.mousePointer.isDown && game.time.now - timeCheck > 1000)
    {
        create_new_object();    
        timeCheck = game.time.now;        
    }

    sprite.rotation = game.physics.arcade.moveToXY(sprite, path1[0].x, path1[0].y, 100);

}


function create_new_object()
{
   sprite1 = enemies.create(360 + Math.random() * 200, 50 + Math.random() * 200, 'star');
   sprite1.rotation = game.physics.arcade.moveToXY(sprite1, path2[0].x, path2[0].y, 100);
}

function render() {

}
</script>

</body>
</html>

I have a Object sprite of a group and its working fine. But when I click on the canvas and add a new object named "sprite1" and try to add the same function it is showing me error. Please resolve my problem.

Thanks in advance

Link to comment
Share on other sites

The problem is in how you're enabling physics on the group:

game.physics.enable(enemies, Phaser.Physics.ARCADE);

This will only add physics bodies to any sprites already in the group. If you want all sprites added to the group to have physics you need to do this instead:

enemies.enableBody = true;enemies.physicsBodyType = Phaser.Physics.ARCADE;

Or set these when using the game object factory:

enemies = game.add.group(game.world, 'enemies', true, true, Phaser.Physics.ARCADE);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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