问题
Using a Nexus 5 (and any of the later android handsets such as Galaxy S5 etc) my game which previously worked fine on other devices, simply shows a black screen when launched, the buttons can be clicked (even though you cannot see them) and the sfx plays.
Having struggled massively with this problem, I somehow managed to fix it to some degree this is the code that matters regarding the launch;
mGLSurfaceView = new CCGLSurfaceView(this);
CCDirector.sharedDirector().setScreenSize(CCDirector.sharedDirector().winSize().width,
CCDirector.sharedDirector().winSize().height);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().getActivity().setContentView(mGLSurfaceView, createLayoutParams());
InitParam();
With the following code above, shows a black screen on launch (the splash screen graphic DOES show but the main gameactivity screen does not).
However, by changing this line;
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
to
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
The app displays - albeit stretched out graphics and not ideal! So this seems to be a resolution issue due to the newer android devices. Having searched I could find snippets of code relating to 'gluPerspective' but I do not have that in my code to change.
The Nexus / Galaxy has resolutions up to 1920 I believe, so somehow need to find a way to set that to work with the existing code.
Additional code which may help, from the TitleLayer
public TitleLayer()
{
super();
CCSprite sprite = CCSprite.sprite(G._getImg("background"));
G.setScale(sprite);
sprite.setAnchorPoint(0, 0);
sprite.setPosition(0, 0);
addChild(sprite);
isTouchEnabled_= true;
}
public static void setScale(){
G._scaleX = CCDirector.sharedDirector().winSize().width / G.WIN_W;
G._scaleY = CCDirector.sharedDirector().winSize().height / G.WIN_H;
}
And it gets the values from here;
public static Activity g_Context;
public static final float DEFAULT_W = 360f;
public static final float DEFAULT_H = 480f;
public static final float WIN_W = 720f;
public static final float WIN_H = 1280f;
public static void getScale(){
_scaleX = CCDirector.sharedDirector().winSize().width / WIN_W;
_scaleY = CCDirector.sharedDirector().winSize().height / WIN_H;
}
public static float _getX(float x) {
return _scaleX * x;
}
public static float _getY(float y) {
return _scaleY * y;
}
public static void setScale(CCNode node) {
node.setScaleX(_scaleX);
node.setScaleY(_scaleY);
}
public static void setScale(CCNode node, float scaleFactor) {
node.setScaleX(_scaleX*scaleFactor);
node.setScaleY(_scaleY*scaleFactor);
}
public static void setScale(CCNode node, boolean bSmall) {
float scale = bSmall ?
(_scaleX<_scaleY ? _scaleX : _scaleY) :
(_scaleX>_scaleY ? _scaleX : _scaleY);
node.setScale(scale);
}
回答1:
This is a issue of cocos2d for Android 4.3 and above Issue raised and answered at github forum
Though there are many work around like putting the below code in onStart() method.
app.metric = 1;
CGSize s = CCDirector.sharedDirector().winSize();
if (s.height > 1280) {
app.metric = s.height / 1280;
s.height = s.height / app.metric;
s.width = s.width / app.metric;
CCDirector.sharedDirector().setScreenSize((int) s.width, (int) s.height);
}
or if you have the source code of cocos2d then change the line mentioned below.
public static final int kCCDirectorProjectionDefault = kCCDirectorProjection3D;
or you can implement this solution
In CCDirector's setProjection Method - Change gluPerspective to 2000 instead of 1500 New Line will be like this - GLU.gluPerspective(gl, 60, size.width/size.height, 0.5f, 2000.0f);
But all this are work around for the issue, so if you really want good solution then you can get the latest cocos2d jar or source code and implement in your app.
来源:https://stackoverflow.com/questions/25137441/black-screen-on-android-launch-unless-using-landscape-orientation