OLD | NEW |
1 // Copyright (c) 2013 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium 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() | 22 bool Gameplay::init() { |
17 { | 23 if (!CCScene::init()) |
18 CCScene* scene = CCScene::create(); | 24 return false; |
19 Gameplay* layer = Gameplay::create(); | 25 CCLayer* ui = UILayer::create(); |
20 scene->addChild(layer); | 26 addChild(ui, 1, UI_TAG); |
21 return scene; | |
22 } | |
23 | 27 |
24 bool Gameplay::init() | 28 CCLayer* physics = PhysicsLayer::create(); |
25 { | 29 addChild(physics, 2, PHYSICS_TAG); |
26 CCLayerColor::initWithColor(ccc4(0,0x8F,0xD8,0xD8)); | |
27 setTouchEnabled(true); | |
28 schedule(schedule_selector(Gameplay::updateGame)); | |
29 return true; | 30 return true; |
30 } | 31 } |
31 | 32 |
32 void Gameplay::ccTouchesEnded(CCSet* touches, CCEvent* event) | 33 void UILayer::Restart(CCObject* sender) { |
33 { | 34 CCLog("Restart pressed"); |
34 for (CCSetIterator it = touches->begin(); it != touches->end(); it++) | 35 ((Gameplay*)getParent())->Restart(); |
35 { | |
36 CCTouch* touch = (CCTouch*)(*it); | |
37 if (!touch) | |
38 break; | |
39 | |
40 CCPoint location = touch->getLocation(); | |
41 CCLog("touch x:%f, y:%f", location.x, location.y); | |
42 } | |
43 } | 36 } |
44 | 37 |
45 void Gameplay::updateGame(float dt) { | 38 void UILayer::Exit(CCObject* sender) { |
| 39 CCLog("Exit pressed"); |
| 40 CCDirector::sharedDirector()->popScene(); |
46 } | 41 } |
| 42 |
| 43 bool UILayer::init() { |
| 44 if (!CCLayer::init()) |
| 45 return false; |
| 46 setTouchEnabled(true); |
| 47 |
| 48 // Create the menu. |
| 49 CCLabelTTF* restart_label = CCLabelTTF::create("Restart", "Arial.ttf", 24); |
| 50 CCLabelTTF* exit_label = CCLabelTTF::create("Exit", "Arial.ttf", 24); |
| 51 |
| 52 CCMenuItemLabel* restart = CCMenuItemLabel::create( |
| 53 restart_label, this, menu_selector(UILayer::Restart)); |
| 54 |
| 55 CCMenuItemLabel* exit = CCMenuItemLabel::create( |
| 56 exit_label, this, menu_selector(UILayer::Exit)); |
| 57 |
| 58 CCMenu* menu = CCMenu::create(restart, exit, NULL); |
| 59 addChild(menu); |
| 60 |
| 61 // Position the menu |
| 62 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); |
| 63 CCSize label_size = restart_label->getContentSize(); |
| 64 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); |
| 65 menu->alignItemsVertically(); |
| 66 float xpos = origin.x + visibleSize.width*0.05 + label_size.width/2; |
| 67 float ypos = origin.y + visibleSize.height*0.95 - label_size.height; |
| 68 menu->setPosition(ccp(xpos, ypos)); |
| 69 return true; |
| 70 } |
OLD | NEW |