Jump to content

Moving group of sprites at the same time


Recommended Posts

Hi all,


so I'm trying to detect whether a car is off or on the track. I tried previously with overlap but had de problem that even when almost all of the car is out, track and car still overlap. To correct this I was told to create a group of sprites, the first being the picture of the car, and four other which would be invisible and would detect whether that part of the car was off track.


So I have two issues: first, a sanity check. Is this the way it is supposed to be? Isn't it possible to apply all the body.functions to carGroup instead of having to apply them to 5 sprites independently? 

carGroup = game.add.group();    car = carGroup.create(200, 200, 'car');    car.anchor.setTo(0.5, 0.5);    car.scale.setTo(0.5, 0.5);    //  This will force it to decelerate and limit its speed    car.body.drag.setTo(200, 200);    car.body.maxVelocity.setTo(400, 400);    car.body.collideWorldBounds = false;    car.body.setSize(200, 200, 0, 0);    //car.body.acceleration.setTo(9,9);        wheel1 = carGroup.create(205,235);    wheel1.anchor.setTo(0.5, 0.5);    wheel1.scale.setTo(0.5, 0.5);    //  This will force it to decelerate and limit its speed    wheel1.body.drag.setTo(200, 200);    wheel1.body.maxVelocity.setTo(400, 400);    wheel1.body.collideWorldBounds = false;    wheel1.body.setSize(80, 20, 0, 0);

Same issue with update function:

        if ((cursors.left.isDown || touchTurn == -1) && currentSpeed > 0) {            car.angle -= currentSpeed/170;            wheel1.angle = car.angle; //etc.        }

Second issue, possibly because of my previous mistakes: 

Here you can see the "wheel" in a more or less ok position



When the group turns, as they do so independently, they don't turn keeping the relative position within the elements, so the "wheel" falls in the middle of the car, not in the right side



What shoud I do? Thank you for all the answers you've given me before



Link to comment
Share on other sites

Thanks, so I get the concept, but now wheel and chassis are going in opposite directions. I assume they are pivoting from a point, but wheel is not attached to chassis 

    car = game.add.group();    chassis = car.create(200, 200, 'chassis');    chassis.anchor.setTo(0.5, 0.5);    chassis.scale.setTo(0.5, 0.5);    chassis.body.collideWorldBounds = false;    chassis.body.setSize(200, 200, 0, 0);        wheel1 = car.create(205,235);    wheel1.pivot = chassis;    wheel1.body.collideWorldBounds = false;    wheel1.body.setSize(80, 20, 0, 0);    

As for velocities in update function, all must be applied to car now, right?


Thank you so much

Link to comment
Share on other sites


  • Recently Browsing   0 members

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