Cocos2d-x v3.6 android black screen when you add sprites

858 Views Asked by At

At once I apologize for my English, I do not know the language, and therefore use google-translator.

Use Cocos2d-x version 3.6 for writing games for Android. I tested on Samsung Galaxy S4 Mini with version Android 4.4.2.

The game consists of two stages - the main menu and, in fact, the game itself. The game is in init () is loaded and rendered Tilemap two visible layers. Also in the init () is added to the player sprite. In a separate Layer-e is the menu (one single button, the picture, which returns to the stage with the main menu).

Now, the problem itself. The update () the scene (called due scheduleUpdate ()) creates a sprite (food) if the amount of food on the map less MAX_FOOD. When you add these sprites to the scene, the scene may at any time cease to display anything except the return to the main menu. Completely black screen with a reset button disappeared Added sprites food, sprite and background player Tilemap. This usually happens in a split second after the start of the scene: everything is rendered and a split second later disappears into a black screen. Sometimes, a "crash" occurs through five or ten seconds after the start of the scene. And the reset button is displayed properly and running. If this did not happen in the first fifteen seconds, a further problem arises.

The more MAX_FOOD, the more likely when you start the scene it will be black. When set to 5, somewhere, every fifth launch of the black screen ends with a value of 10 - one in five is displayed normally, while the other four are black. LogCat to this problem does not react.

Somebody faced with similar? In what may be the problem, what are the ways to solve it?

//Showing Tilemap (in GameScene :: init ())
std::string file = "big_map.tmx";
auto str = String::createWithContentsOfFile (FileUtils::getInstance()->fullPathForFilename(file.c_str()).c_str());
_tileMap = TMXTiledMap::createWithXML(str->getCString(),"");
_background1 = _tileMap->getLayer("Layer1");
_background2 = _tileMap->getLayer("Layer2");
_impassableZones = _tileMap->getLayer("Impassable");
addChild(_background1, -1);
addChild(_background2, 1);
addChild(_impassableZones, 2);
_impassableZones->setVisible(false);

//Displaying the player (in GameScene :: init ())
playerSprite = cocos2d::Sprite::create("player.png");
playerSprite->setPosition(x + _tileMap->getTileSize().width / 2, y + _tileMap->getTileSize().height / 2);
addChild(playerSprite, 0);

//Displaying the reset button (in GameScene :: init ())
userInterface = Layer::create();
auto closeItem = MenuItemImage::create("button_pause1.png", "button_pause.png", CC_CALLBACK_1(GameScene::GoBack, this));
closeItem->setPosition(Point(closeItem->getContentSize().width/2, visibleSize.height - closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point(0, 0));
userInterface->addChild(menu);
addChild(userInterface, 100);

void GameScene::update(float dt){
    if((int)floatingFood.size() < MAX_FOOD){
        int dist = Helper::hypotenuse(visibleSize.width, visibleSize.height);
        //generateFood(playerSprite->getPosition(), dist, dist*2);
        generateFood(playerSprite->getPosition(), 0, 300); // Generate food
    }
    freeFood(playerSprite->getPosition());  // Delete sprites food that far
    floating();
    this->setViewPointCenter(playerSprite->getPosition());
}

// Create the sprite food in a certain zone around player
void GameScene::generateFood(cocos2d::Point position, int min, int max){
    int distance = rand()%(max - min) + min;
    double angle = rand()%360;
    angle *= 3.14/180;
    Point pos = Point(distance*cos(angle), distance*sin(angle));
    Food* _food = new Food();
    Sprite* temp = Sprite::create("food1.png");
    _food->setSprite(temp);
    temp->setPosition(pos + position);
    addChild(temp, 10);
    floatingFood.push_back(_food);
}
1

There are 1 best solutions below

0
On

The problem was that I created separate layers and added to the scene, and the map tiles are added. As a result, cocos deleted it as unnecessary, but in another function I called her to find out the coordinates. As a consequence, they are incorrect, and this function is the center of the screen to put them. But there was nothing there, so pictured a black screen.