Question

enter image description hereI am trying to add an image background this way:

background = CCSprite.sprite("filename.png");
background.setAnchorPoint(CGPoint.ccp(0.5f,0.0f));
background.setPosition(width/2, background.getContentSize().height);
layer.backgroundLayer.addChild(background);

The problem is that the added background is weird and some parts are stretched. Please I need help how to add background properly. Thanks in advance.

To explain more please look at the original image and the result

Original:

Result:

enter image description here

Config code

    @Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    _glSurfaceView = new CCGLSurfaceView(this);
    setContentView(_glSurfaceView);
}

@Override
public void onStart()
{
    super.onStart();

    CCDirector.sharedDirector().attachInView(_glSurfaceView);

    CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);

    CCDirector.sharedDirector().setDisplayFPS(true);

    CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);

    CCScene scene = HelloWorldLayer.scene();
    CCDirector.sharedDirector().runWithScene(scene);
}
Était-ce utile?

La solution

    For landscape Mode you can try this in your AppDelegate Class

Then apply sprite position

// initialize director
        director = CCDirector::sharedDirector();
        EGLView  = CCEGLView::sharedOpenGLView();

        director->setOpenGLView(EGLView);


        CCSize screenSize = EGLView->getFrameSize();
        CCSize designSize = CCSizeMake(800, 480);
        EGLView->setDesignResolutionSize(designSize.width,designSize.height, kResolutionExactFit);


        if(screenSize.height > 480 && screenSize.height < 720 )
        {

            CCSize resourceSize = CCSizeMake(960, 540);
            director->setContentScaleFactor(resourceSize.height/screenSize.height);
            CCLog("Resolution Scale OF Karboon=%f",resourceSize.width/screenSize.width);
        }


        else if (screenSize.height >= 720 && screenSize.height < 800)
        {

            CCSize resourceSize = CCSizeMake(1280, 720);
            director->setContentScaleFactor(resourceSize.height/screenSize.height);
            CCLog("Resolution Scale OF NOTE=%f",resourceSize.width/screenSize.width);

        }

        else if(screenSize.height > 800)
        {
            CCSize resourceSize = CCSizeMake(1920, 1080);
            director->setContentScaleFactor(resourceSize.height/screenSize.height);
            CCLog("Resolution Scale OF Nexus=%f",resourceSize.width/screenSize.width);

        }

        else
        {


         director->setContentScaleFactor(1);
        CCLog("Resolution Scale OF S Advance=%f");

        }

Autres conseils

You can do such a way

CCSize s = CCDirector::sharedDirector()->getWinSize();
float w = s.width;
float h = s.height;
CCSprite *bg = CCSprite::create("shop_bg.jpg");
bg->setPosition(ccp(s.width/2, s.height/2));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top