Hello! I have been facing an issue:
Scene* HelloWorld::createScene()
{
// create the scene with physics enabled
Scene* scene = Scene::createWithPhysics();
// set gravity
scene->getPhysicsWorld()->setGravity(Vec2(0, -900));
// optional: set debug draw
scene->getPhysicsWorld()->setDebugDrawMask(0xffff);
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
When
auto layer = HelloWorld::create();
scene->addChild(layer);
is commented out, then I receive only a black screen with fps information on it. Why does including those two lines of code make it work properly?
And furthermore, if I add a child to my Scene that is just another scene (Since HelloWorld inherits from Scene), aren’t I just adding a scene as a child to another scene?
And when adding objects to the returned scene, are they added to the PhysicsWorldScene or the child Scene?
It is all very confusing. Here is a copy and paste of full file for reference:
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
//version 1, works when addchild!!!!!!!!
Scene* HelloWorld::createScene()
{
// create the scene with physics enabled
Scene* scene = Scene::createWithPhysics();
// set gravity
scene->getPhysicsWorld()->setGravity(Vec2(0, -900));
// optional: set debug draw
scene->getPhysicsWorld()->setDebugDrawMask(0xffff);
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
//version 2 !!!!!!!! This implementation actually makes sense to me. Only one scene instead of two, easy to read code, and it works flawlessly.
/*Scene* HelloWorld::createScene()
{
HelloWorld* scene = new HelloWorld();
if (scene && scene->initWithPhysics() && scene->init())
{
scene->autorelease();
// custom code
scene->getPhysicsWorld()->setGravity({ 0.0f,-900.0f });
scene->getPhysicsWorld()->setSubsteps(1);
scene->getPhysicsWorld()->setDebugDrawMask(0xffff);
// return the scene
return scene;
}
CC_SAFE_DELETE(scene);
return nullptr;
}*/
void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
log("Key with keycode %d pressed", keyCode);
}
void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
log("Key with keycode %d released", keyCode);
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 4);
//auto block = Sprite::create("14.png");
Sprite* block[10];
for (int i = 0; i < 10; i++) {
block[i] = Sprite::create("14.png");
auto physicsbody = PhysicsBody::createBox(Size(130.0f, 140.00f),
PhysicsMaterial(0.1f, .9f, 0.0f));
physicsbody->setDynamic(false);
block[i]->setPhysicsBody(physicsbody);
}
int offset = 0;
for (int i = 0; i < 10; i++) {
block[i]->setPosition(Vec2(offset,100));
offset+=128;
}
for (int i = 0; i < 10; i++) {
addChild(block[i]);
}
auto bg = Sprite::create("BG.png");
auto rkt = Rect(0, 0, 1024, 768);
bg->setTextureRect(rkt);
bg->setScale(2);
addChild(bg, -1);
auto physicsbody1 = PhysicsBody::createBox(Size(40.0f, 51.0f),
PhysicsMaterial(0.1f, 1.0f, 0.0f));
auto mush = Sprite::create("Mushroom_1.png");
mush->setPosition(Vec2(450,400));
physicsbody1->setDynamic(true);
mush->setPhysicsBody(physicsbody1);
auto moveBy = MoveTo::create(10, Vec2(700, 600));
addChild(mush);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
//Close the cocos2d-x game scene and quit the application
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
/*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
//EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent);
}
Thank you very much for your help guys.
Michael