3
\$\begingroup\$

When I set a position3D for a Sprite in cocos2dx, it render two times the same sprite, i took a screenshot:

Image

I set the position3D because i want to use the Z position the give an effect of distance, but its rendering these two sprites in the lower left corner, how can I fix that?

Here is my code in .h:

cocos2d::Sprite* mySprite;
cocos2d::Sprite* mySprite2;
cocos2d::Camera* camera;

Here i my code in cpp:

bool HelloWorld::init()
{

if ( !Layer::init() )
{
    return false;
}


auto s = Director::getInstance()->getWinSize();
camera = Camera::createPerspective(60, (GLfloat)s.width / s.height, 1, 100);

camera->setRotation3D(Vec3(0, 0, 0));

this->addChild(camera);
mySprite2 = Sprite::create("idle1.png");
mySprite2->setPosition3D(Vec3(5, 0, -41));
mySprite2->getTexture()->setAliasTexParameters();

this->addChild(mySprite2, 2);

mySprite = Sprite::create("idle1.png");
mySprite->setPosition3D(Vec3(-5, 0, -81));
mySprite->getTexture()->setAliasTexParameters();

this->addChild(mySprite, 0);
}
\$\endgroup\$
1
\$\begingroup\$

You have to set the camera user to something other than DEFAULT because the DEFAULT camera is used for things like UI, Menus and is rendered last.

// Camera
auto s = Director::getInstance()->getWinSize();
auto camera = Camera::createPerspective(60, (GLfloat) s.width / s.height, 1, 1000);

// set parameters for camera
camera->setPosition3D(Vec3(0, 400, 400));
camera->lookAt(Vec3(0, 0, 0), Vec3(0, 1, 0));
camera->setCameraFlag(CameraFlag::USER1);
this->addChild(camera); //add camera to the scene

So when you create a new camera the deafult camera is set to DEFAULT, and the new camera is also set to DEFAULT initially. So set it to USER1.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.