Jump to content

Newbie question: different ways to move an object


Maria
 Share

Recommended Posts

Hi everybody!

 

I am totally new to phaser and I try to learn it.

 

I have made the first tutorials and also looked at a lot of tutorials and the docs.

 

I try to make my first game, which simply should be a car which drives from right to left through the screen, and when player clicks it, it should drive faster.

 

I have managed to move the car across the screen via add.tween

 

But, however, when I try to speed it up via click, it does nothing. The click is counted but the car just continues. I managed only to get a weird behaviour that the car was moved few pixel forward like some game error.

 

(I tried, amongst others, this: sprite.body.acceleration.x = -1000; )

 

So, when I look at tutorials and forum threads, there are many ways used to move an sprite. I have seen via Tween, MovetoPointer and others.

 

I think my game concept so far is just not working and I think I have some basic information missing.

 

It would be great if somebody could explain me the different movement methods or point me to the right direction.

 

Thanks so much!

Link to comment
Share on other sites

Hehe, I managed to animate my car and to tell them to accelerate.

 

This time I followed the instructions more clearly. In this example the movement is made via Tween, like this:

//  Here we create a tween on the sprite created above    var tween = game.add.tween(bot);    //  The object defines the properties to tween.    //  In this case it will move to x 600    //  The 6000 is the duration in ms - 6000ms = 6 seconds    tween.to({ x: 600 }, 6000);

But the acceleration is fired only when the car has arrived in its final place, not before. The counter counts correctly.

function listener (sprite) {    counter++;    console.log(counter);    sprite.body.acceleration.x = -1000; }

Why is this so? (In addition to my question above about the different movement methods) thanks

Link to comment
Share on other sites

There are multiple ways to move a sprite. Usually, you should pick one and be super careful about using others at the same time. They are usually incompatible.

 

* Physics system. This is body.velocity and body.acceleration. If you use these you have to be *very* careful about setting the position of the sprite directly since the physics system will interpret that as a huge increase in velocity and will slingshot your sprite out of the world.

 

* Tweens. The tween will take over the moving of your sprite for as long as the tween runs. AFAIK, setting the velocity or acceleration won't do anything when the sprite is tweening since the position is being set manually by the tween (see Physics, above).

 

* Manually. In an update function, change the position of your sprite yourself. Super flexible but you'll also have to take care of checking collisions and stuff.

 

Back to your question. What if you didn't use a tween and just moved the sprite yourself? Like, you know how far your sprite has to move (startX - endX) and you know how long you want it to take (e.g. 6000 ms in your example). In your update loop you can change the x of your car by doing, e.g. car.x += (startX - endX) / 6000 * game.time.physicsElapsedMS... and if the player clicks you can move those numbers around? Would that work?

Link to comment
Share on other sites

Thanks!

 

Honestly, I am not sure. I like the acceleration effect which I could do on the sprite. What is the best way? I tried to move the sprite by myself but I did not get it to work at all. 

 

It should be explained here I think:

http://phaser.io/examples/v2/sprites/move-a-sprite

 

But it is explained with keyboards. I tried on the update function like this

function update() {    car.x -= 1;}

But my car disappeared and I could not find it anymore :)

 

I'd like to use the physics system, but I do not know how to move the car with it.

 

In the example from the docs about body movement in the arcade physics, but they are using the tween to move the car

 

Another solution? :

Isn't there a way to stop the tween? I thought I could stop the tween and then tell it to accelerate as sprite, but when I tried to add tween.stop(); to the listener, it stopped only after finishing. (in which case it would have stopped anyway, so does not really make sense)

Link to comment
Share on other sites

Oooh finally! I got it! Lol had forgotten to enable the physics for the moveToXY :)

 

This is working now:

function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    game.add.sprite(0, 0, 'background');    //Vehicles group hold all cars    vehicles = game.add.group();    var policecar = vehicles.create(700, streetHeight, 'policecar');    vehicles.setAll('anchor.x', 0);    vehicles.setAll('anchor.y', 1);    game.physics.arcade.enable(policecar);        policecar.animations.add('run');    policecar.animations.play('run', 15, true);         policecar.inputEnabled = true;       policecar.events.onInputDown.add(ItemClicked, this);      game.physics.arcade.moveToXY(policecar, 100, streetHeight);}function ItemClicked (sprite) {    //gets the sprite of the item clicked    counter++;    console.log(counter);    sprite.body.acceleration.x = -1000; }

I know the group is not relevant here, my purpose is to have a lot of cars and one of them randomly drives across the screen and drives away when clicked

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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