cocos 2dx getPosition() , always returns initial position instead of current position

泪湿孤枕 提交于 2019-12-11 08:09:15

问题


I have a sprite with having an action through which, it moves towards right side.

 CCSprite *spaceShip1=CCSprite::create("ufo_1.png");
 spaceShip1->setPosition(ccp(visibleSize.width/8,visibleSize.height/2));
 this->addChild(spaceShip1, 3);
 CCMoveBy* moveleft1 = CCMoveBy::create(3, ccp(visibleSize.width, 0));
 spaceShip1->runAction(moveleft1);

I want to get the current visual position of sprite, as it is moving due to runAction, it should return its updated position.

I am using:

spaceShip1->getPosition();

and this position is utilizing here:

CCParticleSystemQuad* _spaceShip1 = new CCParticleSystemQuad();
_spaceShip1 = CCParticleSystemQuad::create("growingFlare.plist");
_spaceShip1->setPosition(spaceShip1->getPosition());
_spaceShip1->setAngle(180);
_spaceShip1->setScale(5);
_spaceShip1->stopSystem();
this->addChild(_spaceShip1, 4);

That particle effect must take place at the current position of spaceship, but it always took effect at initial position of spaceship.

I'm trying to get the current position of sprite. but it always returns the initial position instead of its current moving position.

What I have to do to get its current position?


回答1:


float xPos = spaceShip->getPositionX();
float yPos = spaceShip->getPositionY();
CCLog("%f     %f", xPos, yPos);

Try this in the method where you want to get the current x,y position of the moving sprite. It won't get the initial position what you get now.




回答2:


That particle initialization happens during init(). Animation is not live then, hence will always give initial position.

You have 2 options.

  1. In your update method. SetPosition() of your particle system every frame to the updated spaceship getPostion.
  2. Make particle system as a child of spaceship. In case of, "this->addChild()", do "_firstSpaceShip->addChild(_particleSystem)" and give it a initial x position like -10, so it always follow behind your spaceship.



回答3:


May be this will help you out

CCParticleSystemQuad* _spaceShip1 = new CCParticleSystemQuad(); _spaceShip1 = CCParticleSystemQuad::create("growingFlare.plist"); _spaceShip1->setPosition(spaceShip1->getPositionX(), spaceShip1->getPositionY());



来源:https://stackoverflow.com/questions/24730270/cocos-2dx-getposition-always-returns-initial-position-instead-of-current-pos

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!