Jump to content

Move an object to the MOVING object


weratius
 Share

Recommended Posts

Hello, guys

 

I have a sprite which moves in update method 

planetMovement: function() {        if(planetMove.status) {            for(var planet in planetMove.obj) {                var id = planetMove.obj[planet].id;                if(createdPlanets[id] != null && createdPlanets[id].planet.earthBMD != null)                {                    createdPlanets[id].planet.earthBMD.move(-1, 0);                                        createdPlanets[id].planet.x = planetMove.obj[planet].x;                    createdPlanets[id].planet.y = planetMove.obj[planet].y;                    createdPlanets[id].nickName.x = planetMove.obj[planet].x - 30;                    createdPlanets[id].nickName.y = planetMove.obj[planet].y - 100;                }            }            planetMove.status = false;            delete planetMove.obj;        }    }

It's planets initialization:

initPlanet: function(x, y, planetKey, name, race, id) {        var container = game.add.group();        container.race = race;        var newW = (1200 / 300) * 200;        var newH = 200;        var earthBMD = game.make.bitmapData(newW, newH, 'earthBMD');        container.earthBMD = earthBMD;        earthBMD.draw('map1', 0, 0, newW, newH);               var somePlanet = game.add.sprite(0, 0, earthBMD);        somePlanet.anchor.set(0.5);        somePlanet.angle = -15;        somePlanet.planetId = id;        somePlanet.inputEnabled = true;        game.physics.arcade.enable(somePlanet, Phaser.Physics.ARCADE);        somePlanet.events.onInputDown.add(myObject.moveToMovingPlanet, this);        container.somePlanet = somePlanet;        var shadow = game.make.sprite(0, 0, 'sphere');        shadow.scale.set(0.5, 0.5);        shadow.anchor.set(0.5);               var maska = game.add.graphics(0, 0);        maska.beginFill(0xFF0000, 0.5);        maska.drawCircle(0, 0, 150);        container.add(somePlanet);        container.add(maska);        container.add(shadow);        container.mask = maska;        return container;    }

So, also I have a ship 

initShip: function(x, y, shipType) {        var someShip = game.add.sprite(x, y, shipType);        someShip.animations.add('run');        someShip.animations.play('run', 20, true);        someShip.anchor.setTo(0.5, 0.5);        someShip.scale.set(0.5, 0.5);        game.physics.arcade.enable(someShip, Phaser.Physics.ARCADE);                return someShip;}

I need to move the ship when the planet is clicked, I do smth like this:

moveToMovingPlanet: function(movingPlanet) {        console.log('Clicked')        this.goToMovingPlanet.status = true;        this.goToMovingPlanet.planetId = movingPlanet.planetId;            },

(planet was clicked)

 

And here is the ship's movement to the clicked planet:

update: function() {        if(this.goToMovingPlanet.status) {            var planet = createdPlanets[this.goToMovingPlanet.planetId].planet.somePlanet;            // myShip.rotation = Math.atan2(planet.y, planet.x);            game.physics.arcade.moveToObject(myShip, planet, playerInfoObject.engine.speed);        }}

It doesn't move at all. What's wrong?

 

I have checked that the planet's coords change, but the ship stands still =(

 

Thank you in advance!

Link to comment
Share on other sites

I have decided such a problem like this, is it ok, how do you think, guys?

update: function() {        if(this.goToMovingPlanet.status) {            var planet = createdPlanets[this.goToMovingPlanet.planetId].planet;            // myShip.rotation = Math.atan2(planet.y, planet.x);            if(planet.x > myShip.x) {                myShip.x += planet.x / 1000;            } else {                myShip.x -= planet.x / 1000;            }                        if(planet.y > myShip.y) {                myShip.y += planet.y / 1000;            } else {                myShip.y -= planet.y / 1000;            }        }}
Link to comment
Share on other sites

It doesn't move at all. What's wrong?

 

 

I have checked that the planet's coords change, but the ship stands still =(

Hard to tell, it looks like it should work. What if the planet doesn't move,does the ship move then?

 

I have decided such a problem like this, is it ok, how do you think, guys?

 

Hmm, the logic appears strange -- are you looking for some kind of exponentially decreasing distance? If so, I would do:

var planet = createdPlanets[this.goToMovingPlanet.planetId].planet;var deltaX = planet.x - myShip.xvar deltaY = planet.y - myShip.y;var k=50;myShip.x += (deltaX/50);myShip.y += (deltaY/50);

But this would make the ship kind of "rubber-band" its way to the planet. If you want something to linearly move, you should set a defined velocity and use that.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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