在适配多分辨率时,我需要根据分辨率来设置对应的resPath,setDesignResolutionSize,以及setContentScaleFactor。
假如我的设计分辨率为320480,为此我准备了两套美术资源(320480, 640*960),并将这两套美术资源,
分别使用TexturePacker制作成sprites.plist和sprites.png。
然后将它们分别放置到目录iphone/res和iphonehd/res下,如下图:
那么代码如下:
var DesignResolutionSize = cc.size(320, 460);
var SmallResource = new Resource(cc.size(320, 480), "res/iphone");
var MediumResource = new Resource(cc.size(640, 960), "res/iphonehd");
function setResPathAndScaleFactor()
{
var frameSize = cc.view.getFrameSize();
if (frameSize.height > SmallResource.size.height)
{
cc.loader.resPath = MediumResource.directory;
cc.view.setDesignResolutionSize(
MediumResource.size.width,
MediumResource.size.height,
cc.ResolutionPolicy.SHOW_ALL
);
cc.director.setContentScaleFactor(Math.min(MediumResource.size.height/DesignResolutionSize.height,
MediumResource.size.width/DesignResolutionSize.width));
}
else
{
cc.loader.resPath = SmallResource.directory;
cc.view.setDesignResolutionSize(
SmallResource.size.width,
SmallResource.size.height,
cc.ResolutionPolicy.SHOW_ALL
);
cc.director.setContentScaleFactor(Math.min(SmallResource.size.height/DesignResolutionSize.height,
SmallResource.size.width/DesignResolutionSize.width));
}
}
```
main.js代码如下:
cc.game.onStart = function(){
cc.view.adjustViewPort(true);
// init res path and scale factor
setResPathAndScaleFactor();
// set font size
setFontSize();
// resize browser size
cc.view.resizeWithBrowserSize(true);
// load resources
LoaderScene.preload(g_resources, function () {
// load brain session
loadBrainSession();
// add sprite frames
cc.spriteFrameCache.addSpriteFrames(res.Brain_plist, res.Brain_png);
// run splash scene
cc.director.runScene(new SplashScene());
});
};
cc.game.run();
```
运行后,我得到如下报错:
不知道是不是我理解错了,用错了?



