OLD | NEW |
1 // Copyright (c) 2013 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2013 The Native Client Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #include "GameplayScene.h" | 4 #include "GameplayScene.h" |
| 5 #include "PhysicsLayer.h" |
| 6 |
| 7 #define PHYSICS_TAG 101 |
| 8 #define UI_TAG 102 |
5 | 9 |
6 USING_NS_CC; | 10 USING_NS_CC; |
7 | 11 |
8 Gameplay::~Gameplay() | 12 CCScene* Gameplay::scene() { |
9 { | 13 return Gameplay::create(); |
10 } | 14 } |
11 | 15 |
12 Gameplay::Gameplay() | 16 void Gameplay::restart() { |
13 { | 17 removeChild(getChildByTag(PHYSICS_TAG)); |
| 18 CCLayer* physics = PhysicsLayer::create(); |
| 19 addChild(physics, 2, PHYSICS_TAG); |
14 } | 20 } |
15 | 21 |
16 CCScene* Gameplay::scene() | |
17 { | |
18 CCScene* scene = CCScene::create(); | |
19 Gameplay* layer = Gameplay::create(); | |
20 scene->addChild(layer); | |
21 return scene; | |
22 } | |
23 | 22 |
24 bool Gameplay::init() | 23 bool Gameplay::init() { |
25 { | 24 if (!CCScene::init()) |
26 CCLayerColor::initWithColor(ccc4(0,0x8F,0xD8,0xD8)); | 25 return false; |
27 setTouchEnabled(true); | 26 CCLayer* ui = UILayer::create(); |
28 schedule(schedule_selector(Gameplay::updateGame)); | 27 addChild(ui, 1, UI_TAG); |
| 28 |
| 29 CCLayer* physics = PhysicsLayer::create(); |
| 30 addChild(physics, 2, PHYSICS_TAG); |
29 return true; | 31 return true; |
30 } | 32 } |
31 | 33 |
32 void Gameplay::ccTouchesEnded(CCSet* touches, CCEvent* event) | |
33 { | |
34 for (CCSetIterator it = touches->begin(); it != touches->end(); it++) | |
35 { | |
36 CCTouch* touch = (CCTouch*)(*it); | |
37 if (!touch) | |
38 break; | |
39 | 34 |
40 CCPoint location = touch->getLocation(); | 35 void UILayer::restart(CCObject* sender) { |
41 CCLog("touch x:%f, y:%f", location.x, location.y); | 36 CCLog("restart pressed"); |
42 } | 37 ((Gameplay*)getParent())->restart(); |
| 38 |
43 } | 39 } |
44 | 40 |
45 void Gameplay::updateGame(float dt) { | 41 |
| 42 void UILayer::exit(CCObject* sender) { |
| 43 CCLog("exit pressed"); |
| 44 CCDirector::sharedDirector()->popScene(); |
46 } | 45 } |
| 46 |
| 47 |
| 48 bool UILayer::init() { |
| 49 if (!CCLayer::init()) |
| 50 return false; |
| 51 setTouchEnabled(true); |
| 52 |
| 53 // Create the menu. |
| 54 CCLabelTTF* restart_label = CCLabelTTF::create("Restart", "Arial.ttf", 24); |
| 55 CCLabelTTF* exit_label = CCLabelTTF::create("Exit", "Arial.ttf", 24); |
| 56 |
| 57 CCMenuItemLabel* restart = CCMenuItemLabel::create(restart_label, |
| 58 this, menu_selector(UILayer::restart)); |
| 59 |
| 60 CCMenuItemLabel* exit = CCMenuItemLabel::create(exit_label, |
| 61 this, menu_selector(UILayer::exit)); |
| 62 |
| 63 CCMenu* menu = CCMenu::create(restart, exit, NULL); |
| 64 addChild(menu); |
| 65 |
| 66 // Position the menu |
| 67 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); |
| 68 CCSize label_size = restart_label->getContentSize(); |
| 69 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); |
| 70 menu->alignItemsVertically(); |
| 71 float xpos = origin.x + visibleSize.width*0.05 + label_size.width/2; |
| 72 float ypos = origin.y + visibleSize.height*0.95 - label_size.height; |
| 73 menu->setPosition(ccp(xpos, ypos)); |
| 74 return true; |
| 75 } |
OLD | NEW |