Black screen on Android Launch if not using landscape orientation
Using a Nexus 5 (and any of the subsequent Android phones like Galaxy S5, etc.) my game, which previously worked great on other devices, just shows a black screen on startup, the buttons can be clicked (although you can't see them ) and sfx plays.
Having a lot of hard work dealing with this problem, I somehow managed to fix it to some extent, this is the code that matters to run;
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();
The above code shows a black screen on startup (the graphical splash screen shows but there is no game activity on the main screen).
However, by changing this line,
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
to
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
The app displays - albeit stretched graphics, not perfect! So this appears to be a permission issue due to newer Android devices. After searching, I could find code snippets related to "gluPerspective", but I don't have that in my code to change.
The Nexus / Galaxy has pre-1920 permissions I believe, so you need to somehow find a way to set this to work with your existing code.
Additional code that might help from 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 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);
}
source to share
This is a cocos2d issue for Android 4.3 and above Issue raised and answered on the github forum
Even though there is a lot of work around, for example entering the code below in the 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 cocos2d source code then change the line below.
public static final int kCCDirectorProjectionDefault = kCCDirectorProjection3D;
or you can implement this solution
In CCDirector setProjection Method - Changing gluPerspective to 2000 instead of 1500 The new line would be like this: GLU.gluPerspective (gl, 60, size.width / size.height, 0.5f, 2000.0f);
But this is all to solve this problem, so if you really want a good solution, you can get the latest version or source code of cocos2d and implement it in your application.
source to share