[Cocos2d-x] - Relative Sprite Position

  • igorppbr
    Likes 0

    Problem Description

    I trying to understand how I can set the position of sprites in my Scene.

    I have to set the position of various sprites but I need to set the same position independet of the devices that running the game.

    I tried with method setPosition, it works. But my question is, the position that I set in this method is relative of the full width of the device or for example if I set 600 of height and the max-width of my device is 500 the “600” value is a static value independent of the width from the device?

    How I can set the position relative of the device sizes?

    Thanks!

  • igorppbr
    Likes 0

    I saw the video but I can’t understand this part of code:

    setPosition(Vec2(origin.x+visibleSize.width - sprite1->getContentSize().width/2, origin.y + sprite1->getContentSize().height/2));

    In this part(origin.x+visibleSize.width - sprite1->getContentSize().width/2) I get the full size of my screen width – the sprite width/2?

    I can get the width of my screen and set sprite location for example:

        auto 1percentWidth = visibleSize.width/100;
        auto 1percentHeight = visibleSize.height/100;

        field1->setPosition(Vec2(1percentWidth*5, 1percentHeight*5));

     

    ?

  • Sonar Systems admin
    Likes 0

    I don’t quite understand what you are asking?

  • MoseHas
    Likes 0

    If you need the location of the sprite, try 

    float x = sprite1->getLocation().x;
    float y = sprite1->getLocation().y;

    And then you get the coordinate of the sprite.

     

     

    setPosition() is for setting the coordinate of the sprite. If your screen size is 800*600.

    sprite->setPosition(visibleSize.width / 2, visibleSize.height / 2);

    which means I set the sprite in position (400,300).

    Another example, if you want to set your sprite locate at (40,400), you can just

    sprite->setPosition(visibleSize.width / 20, visibleSize.height / 3 * 2);


  • igorppbr
    Likes 0

    Thanks for the answers.

    I tried to position the sprite relative to screen size.

    I used (visibleSize.width * percent_value) to set sprite position relative to the screen.

Login to reply