Cocos2dx (C++) MenuItemImages/Sprite declarations – refining code

Posted on Updated on

My objective this morning was to turn something like this:

menuComponent_1 = MenuItemImage::create(component.c_str(), component.c_str(), CC_CALLBACK_1(HogeHoge::touchCallBack, this));
menuComponent_1->setPosition(Point(winSize.width/2-60, winSize.height/2+80));

menuComponent_2 = MenuItemImage::create(component.c_str(), component.c_str(), CC_CALLBACK_1(HogeHoge::touchCallBack, this));
menuComponent_2->setPosition(Point(winSize.width/2-360, winSize.height/2-200));

menuComponent_3 = MenuItemImage::create(component.c_str(), component.c_str(), CC_CALLBACK_1(HogeHoge::touchCallBack, this));
menuComponent_3->setPosition(Point(winSize.width/2+240, winSize.height/2-200));

into something that looks more compact and neat. Why did i think this was necessary? because it gives me a headache. There are 2 ways i can think of that will solve this problem:

1) Move my MenuItemImage declaration into a separate method/function, then add menuComponent_1-3, their respective positions and tags into 3 separate arrays then use a for loop to iterate through each array and call me MenuItemImage declaration method.

2) Move my MenuItemImage declaration into a separate method/function, declare each menuItemMenu component as a variable that calls the method i just declared and use those variables as placeholders later.

The 2nd option seems much simpler and logical to my needs but it sounds strange … to make it easier to understand here’s how i modified my code above:

.h header file:

	MenuItemImage* menuComponent;

	Hoge();      // constructor
	~hogeHoge(); // destructor

\.cpp implementation

// somewhere near the top
: menuComponent(NULL)
	// constructor

HogeHoge::~HogeHoge() {
	// destructor

auto component1 =  HogeHoge::createTouchComponents("component1.png", winSize.width/2-60, winSize.height/2+80, 100);
auto component2 =  HogeHoge::createTouchComponents("component2.png", winSize.width/2-360, winSize.height/2+200, 200);
auto component3 =  HogeHoge::createTouchComponents("component3.png", winSize.width/2-240, winSize.height/2+200, 300);

// somewhere below my source code ..

MenuItemImage* HogeHoge::createTouchComponents(std::string component, int x, int y, int tag) {
	menuComponent = MenuItemImage::create(component.c_str(), component.c_str(), CC_CALLBACK_1(HogeHoge::touchCallBack, this));
	menuComponent->setPosition(Point(x, y));
	return menuComponent;

By the way, the method call hogeHoge::touchCallBack is just something I use to process seperate functions in my program. Like playing a sound file or load sprites, but this can be whatever you want. So don’t mind that part, but if whatever your needs are requires to know, please drop me a message!

This was my way of optimizing code, and I hope it helps. Please throw me some questions if you have any!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s