Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在cocos2d-x Android上添加背景图像的问题_Android_Background_Cocos2d X - Fatal编程技术网

在cocos2d-x Android上添加背景图像的问题

在cocos2d-x Android上添加背景图像的问题,android,background,cocos2d-x,Android,Background,Cocos2d X,我尝试通过以下方式添加图像背景: background = CCSprite.sprite("filename.png"); background.setAnchorPoint(CGPoint.ccp(0.5f,0.0f)); background.setPosition(width/2, background.getContentSize().height); layer.backgroundLayer.addChild(background); 问题是添加的背景很奇怪,有些部分被拉伸了。请

我尝试通过以下方式添加图像背景:

background = CCSprite.sprite("filename.png");
background.setAnchorPoint(CGPoint.ccp(0.5f,0.0f));
background.setPosition(width/2, background.getContentSize().height);
layer.backgroundLayer.addChild(background);
问题是添加的背景很奇怪,有些部分被拉伸了。请告诉我如何正确添加背景。提前谢谢

要了解更多信息,请查看原始图像和结果

原件:

结果:

配置代码

    @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);
}
你可以这样做

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));
然后应用精灵位置

// 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");

        }
//初始化控制器
director=CCDirector::sharedDirector();
EGLView=CCEGLView::sharedOpenGLView();
director->setOpenGLView(EGLView);
CCSize screenSize=EGLView->getFrameSize();
CCSize设计尺寸=CCSizeMake(800480);
EGLView->setDesignResolutionSize(designSize.width、designSize.height、kResolutionExactFit);
如果(screenSize.height>480&&screenSize.height<720)
{
CCSize-resourceSize=CCSizeMake(960540);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog(“Karboon的分辨率比例=%f”,resourceSize.width/screenSize.width);
}
否则如果(screenSize.height>=720&&screenSize.height<800)
{
CCSize resourceSize=CCSizeMake(1280720);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog(“注释的分辨率比例=%f”,resourceSize.width/screenSize.width);
}
否则如果(屏幕大小.高度>800)
{
CCSize resourceSize=CCSizeMake(19201080);
director->setContentScaleFactor(resourceSize.height/screenSize.height);
CCLog(“Nexus的分辨率比例=%f”,resourceSize.width/screenSize.width);
}
其他的
{
director->setContentScaleFactor(1);
CCLog(“S前进的分辨率范围=%f”);
}

Singhak thx获取答案。但问题不在于尺寸,我编辑了我的问题,请看一看look@Vervatovskis是什么problem@Vervatovskis可能是由于Appdeleget.cpp文件中的某些设置,我可以查看您的Appdeleget.cpp文件吗?对于纵向模式,您必须将screenSize.height更改为screenSize.width,并在CCSizeMake()中交换宽度和高度坎多伊。sumit@gmail.comamine.jebabli@gmail.com我提前三个小时通过WetTransfer发送。。如果可以的话,来吧
// 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");

        }