0

I am new in cocos2dx android game development, I want to know how can i get the center point of sprite in cocos2dx. I am using the version 3.3.

Let me explain the problem I have one scheduler which call one of my function each 5 seconds. It will change the position of sprint. now over this sprite i want to put another sprite over this sprite exactly center over it. I want to know how to find the center point of running sprint in cocos2dx.

Any help appreciate :)

Thank you

Bhavdip Sagar
  • 1,951
  • 15
  • 27

3 Answers3

0

The following code will center a visual center of sprite on a point.

//your sprite
Sprite * spr = Sprite::create(...);

//a point you want to center the sprite on
Point pointToCenterOn;

//sprite should have 0.5,0.5 pivot in order to have it's center in a specific coordinate
//this code will set sprite's position to the desired point
spr->setPosition(pointToCenterOn);

If you want to do it every frame - use this code in update or schedule a callback to run constantly.

pdeschain
  • 1,411
  • 12
  • 21
0

if i understand your problem right then you want to place sprite B as a Child of Sprite A. try something like this,

Sprite* parent = Sprite::create("MyTexture.png");
parent->setposition(Vec2(100,100));
this->addChild(parent,2);

Sprite* child = Sprite::create("MyTexture2.png");
child->setposition(Vec2(parent->getBoundingBox().size.width/2,parent->getBoundingBox().size.height/2));
parent->addChild(child,2);

this will place child exactly at the center of parent, Moving parent will result in moving child also,

Lazy Gamer
  • 91
  • 5
  • Is child must need to add "parent->addChild(child,2);" can i add the Sprite* child as this->addChild(child,2); – Bhavdip Sagar May 13 '15 at 11:49
  • if u add use this->addChild(child,2); then this child will be added to same layer in which parent is added and you have to move child and parent separately which is a little messy for larger amount of objects, if you want to do it then u can do it like child->setPosition(parent->getPosition)); – Lazy Gamer May 13 '15 at 12:15
0
void yourLayer::setBg() {
    bg1 = CCSprite::create("menu/menu_bg1.jpg");
    bg1->setAnchorPoint(ccp(0, 0.5));
    bg1->setPosition(ccp(0, visibleSize.height / 2 + origin.y));

    lowerPart1 = CCSprite::create("menu/lower.jpg");

    lowerPart1->setAnchorPoint(ccp(0.5,0.5));
    lowerPart1->setPosition(ccp(bg1->getContentSize().width/2,bg1->getContentSize().height/2));
    bg1->addChild(lowerPart1);
    this->addChild(bg1, 0);
}

void yourLayer::scrollBk() {
    bg1->setPosition(ccp(bg1->getPosition().x - 1, bg1->getPosition().y));


    if (bg1->getPosition().x < -bg1->boundingBox().size.width) {
        bg1->setPosition(
                ccp(0, visibleSize.height / 2 + origin.y));
    }


}

void yourLayer::update(float dt) {

    scrollBk();

}

it should work for you...

Arjunsingh
  • 114
  • 5