Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes #36

Open
wants to merge 6 commits into
base: v3.16
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Resources/res/chapter9/axe.c3b
Binary file not shown.
3 changes: 1 addition & 2 deletions cpp/src/MainScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ bool MainScene::init()
Director::getInstance()->replaceScene(Chapter4::createScene());
});
vMenuItems.at(3)->setCallback([&](cocos2d::Ref *sender) {
auto chapter5 = Chapter5();
Director::getInstance()->replaceScene(chapter5.getScene1());
Director::getInstance()->replaceScene(Chapter5::createScene());
});
vMenuItems.at(4)->setCallback([&](cocos2d::Ref *sender) {
Director::getInstance()->replaceScene(Chapter6::createScene());
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/chapter2/Chapter2.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#ifndef __CHAPTER2_H__
#define __CHAPTER2_H__

#include "cocos2d.h"

Expand All @@ -13,4 +13,4 @@ class Chapter2
static cocos2d::Scene* createScene();
};

#endif // __HELLOWORLD_SCENE_H__
#endif // __CHAPTER2_H__
193 changes: 52 additions & 141 deletions cpp/src/chapter5/Chapter5.cpp
Original file line number Diff line number Diff line change
@@ -1,166 +1,77 @@
#include "Chapter5.h"
#include "Chapter5_1.h"
#include "Chapter5_2.h"
#include "Chapter5_3.h"

USING_NS_CC;

Chapter5::Chapter5()
Scene* Chapter5::createScene()
{
initialize();
}
// 'scene' is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto layer = Chapter5::create();

Chapter5::~Chapter5() { }
// add layer as a child to scene
scene->addChild(layer);

void Chapter5::initialize()
// return the scene
return scene;
}

// on "init" you need to initialize your instance
bool Chapter5::init()
{
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// super init first
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (!Node::init())
{
return false;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// some upfront items that we need
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
auto dirs = Director::getInstance();
Size visibleSize = dirs->getVisibleSize();
Vec2 origin = dirs->getVisibleOrigin();

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// create a scene
// 'scene' is an autorelease object
// create a label
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scene1 = Scene::create();

auto thisSceneLabel = Label::createWithTTF("This Is A Scene", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// create a node to hold non-sprites.
// create a menu item
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
auto nodeItems = Node::create();
nodeItems->setName("nodeItems");

auto menuItem = MenuItemFont::create("Make A New Scene");
menuItem->setFontNameObj("Marker Felt.ttf");
menuItem->setFontSizeObj(32);
menuItem->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2).x,
(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height).y - 80));
menuItem->setCallback([&] (cocos2d::Ref* sender) {
Director::getInstance()->replaceScene(Chapter5_1::createScene());
});

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// create a node to hold menu
// create a menu
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
auto menuNode = Node::create();
menuNode->setName("menuNode");
int index = 2;

auto thisSceneLabel = Label::createWithTTF("This Is A Scene", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
scene1->addChild(thisSceneLabel, 1);

auto menuItem1 = MenuItemFont::create("Make A New Scene");
menuItem1->setFontNameObj("Marker Felt.ttf");
menuItem1->setFontSizeObj(32);
menuItem1->setName("menuItem1");
menuItem1->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x,
(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40));
menuItem1->setCallback([&](cocos2d::Ref *sender) {

Director::getInstance()->replaceScene(createScene2());
});

auto menu = Menu::create(menuItem1, NULL);
menu->setName("menu");
menuNode->addChild(menu, 1);
auto menu = Menu::create(menuItem, NULL);
menu->setPosition(Vec2::ZERO);

scene1->addChild(menuNode, 2);
}

Scene* Chapter5::createScene2()
{
auto dirs = Director::getInstance();
Size visibleSize = dirs->getVisibleSize();
Vec2 origin = dirs->getVisibleOrigin();

scene2 = Scene::create();
scene2->retain();

auto nodeItems = Node::create();
nodeItems->setName("nodeItems");

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// create a node to hold menu
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
auto menuNode = Node::create();
menuNode->setName("menuNode");
int index = 2;

auto thisSceneLabel = Label::createWithTTF("This Is A New Scene", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
scene2->addChild(thisSceneLabel, 1);

auto menuItem1 = MenuItemFont::create("Make a New Scene Using a Transition Fade");
menuItem1->setFontNameObj("Marker Felt.ttf");
menuItem1->setFontSizeObj(32);
menuItem1->setName("menuItem1");
menuItem1->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x,
(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40));
menuItem1->setCallback([&](cocos2d::Ref *sender) {

Director::getInstance()->replaceScene(TransitionFade::create(0.5, createScene3(), Color3B(0,255,255)));
});

auto menu = Menu::create(menuItem1, NULL);
menu->setName("menu");
menuNode->addChild(menu, 1);
menu->setPosition(Vec2::ZERO);

scene2->addChild(menuNode, 2);

return scene2;
}

Scene* Chapter5::createScene3()
{
auto dirs = Director::getInstance();
Size visibleSize = dirs->getVisibleSize();
Vec2 origin = dirs->getVisibleOrigin();

scene3 = Scene::create();
scene3->retain();

auto nodeItems = Node::create();
nodeItems->setName("nodeItems");

auto menuNode = Node::create();
menuNode->setName("menuNode");
int index = 2;

auto thisSceneLabel = Label::createWithTTF("This Scene used a Transition!", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
scene3->addChild(thisSceneLabel, 1);

auto menuItem1 = MenuItemFont::create("Make a New Scene Using a Flip");
menuItem1->setFontNameObj("Marker Felt.ttf");
menuItem1->setFontSizeObj(32);
menuItem1->setName("menuItem1");
menuItem1->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x,
(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40));
menuItem1->setCallback([&](cocos2d::Ref *sender) {

Director::getInstance()->replaceScene(TransitionFlipX::create(2, createScene4()));
});

auto menu = Menu::create(menuItem1, NULL);
menu->setName("menu");
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// add child nodes to containers
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
menuNode->addChild(menu, 1);
menu->setPosition(Vec2::ZERO);

scene3->addChild(menuNode, 2);
this->addChild(thisSceneLabel, 1);
this->addChild(menuNode, 2);

return scene3;
}

Scene* Chapter5::createScene4()
{
auto dirs = Director::getInstance();
Size visibleSize = dirs->getVisibleSize();
Vec2 origin = dirs->getVisibleOrigin();

scene4 = Scene::create();
scene4->retain();

auto nodeItems = Node::create();
nodeItems->setName("nodeItems");

auto menuNode = Node::create();
menuNode->setName("menuNode");

auto thisSceneLabel = Label::createWithTTF("This Scene used a Flip!", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
scene4->addChild(thisSceneLabel, 1);

return scene4;
return true;
}
29 changes: 9 additions & 20 deletions cpp/src/chapter5/Chapter5.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,16 @@

#include "cocos2d.h"

USING_NS_CC;

class Chapter5 : public Ref
class Chapter5 : public cocos2d::Node
{
public:
Chapter5();
~Chapter5();

void initialize();
Scene* createScene2();
Scene* createScene3();
Scene* createScene4();

inline Scene* getScene1(){ return scene1; };

protected:
Scene* scene1;
Scene* scene2;
Scene* scene3;
Scene* scene4;

public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();

virtual bool init();

// implement the "static create()" method manually
CREATE_FUNC(Chapter5);
};

#endif // __CHAPTER5_H__
43 changes: 43 additions & 0 deletions cpp/src/chapter5/Chapter5_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Chapter5_1.h"
#include "Chapter5_2.h"
#include "Chapter5.h"

USING_NS_CC;

Scene* Chapter5_1::createScene()
{
// some upfront items that we need
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

// create a scene
auto scene = Scene::create();

// add title
auto thisSceneLabel = Label::createWithTTF("This Is A New Scene", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));

// add the menu item for Make a New Scene Using a Transition Fade
auto menuItem = MenuItemFont::create("Make a New Scene Using a Transition Fade");
menuItem->setFontNameObj("Marker Felt.ttf");
menuItem->setFontSizeObj(32);
menuItem->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x,
(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - 80));
menuItem->setCallback([&] (cocos2d::Ref* sender) {
Director::getInstance()->replaceScene(TransitionFade::create(0.5, Chapter5_2::createScene(), Color3B(0, 255, 255)));
});

// add menu
auto menu = Menu::create(menuItem, NULL);
menu->setPosition(Vec2::ZERO);

// add node to hold menu
auto menuNode = Node::create();

// add child nodes to containers
menuNode->addChild(menu, 1);
scene->addChild(thisSceneLabel, -1);
scene->addChild(menuNode, 2);

return scene;
}
14 changes: 14 additions & 0 deletions cpp/src/chapter5/Chapter5_1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __CHAPTER_5_1_H__
#define __CHAPTER_5_1_H__

#include "cocos2d.h"

USING_NS_CC;

class Chapter5_1 : public Ref
{
public:
static cocos2d::Scene* createScene();
};

#endif // __CHAPTER_5_1_H__
43 changes: 43 additions & 0 deletions cpp/src/chapter5/Chapter5_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Chapter5_2.h"
#include "Chapter5_3.h"
#include "Chapter5.h"

USING_NS_CC;

Scene* Chapter5_2::createScene()
{
// some upfront items that we need
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

// create a scene
auto scene = Scene::create();

// add title
auto thisSceneLabel = Label::createWithTTF("This Scene used a Transition!", "Marker Felt.ttf", 32);
thisSceneLabel->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));

// add the menu item for Make a New Scene Using a Flip
auto menuItem = MenuItemFont::create("Make a New Scene Using a Flip");
menuItem->setFontNameObj("Marker Felt.ttf");
menuItem->setFontSizeObj(32);
menuItem->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x,
(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - 80));
menuItem->setCallback([&] (cocos2d::Ref* sender) {
Director::getInstance()->replaceScene(TransitionFlipX::create(2, Chapter5_3::createScene()));
});

// add menu
auto menu = Menu::create(menuItem, NULL);
menu->setPosition(Vec2::ZERO);

// add node to hold menu
auto menuNode = Node::create();

// add child nodes to containers
menuNode->addChild(menu, 1);
scene->addChild(thisSceneLabel, -1);
scene->addChild(menuNode, 2);

return scene;
}
14 changes: 14 additions & 0 deletions cpp/src/chapter5/Chapter5_2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __CHAPTER_5_2_H__
#define __CHAPTER_5_2_H__

#include "cocos2d.h"

USING_NS_CC;

class Chapter5_2 : public Ref
{
public:
static cocos2d::Scene* createScene();
};

#endif // __CHAPTER_5_2_H__
Loading