Chromium Code Reviews| 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 | |
|
noelallen1
2013/02/12 23:35:52
Are these IDs global? Meaning we don't when them
| |
| 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(); |
|
noelallen1
2013/02/12 23:35:52
Is there a reason that "scene" in this one returns
| |
| 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(); |
|
noelallen1
2013/02/12 23:35:52
static_cast<Gameplay*>(getParent())->
| |
| 43 } | 38 } |
| 44 | 39 |
| 45 void Gameplay::updateGame(float dt) { | 40 |
| 41 void UILayer::exit(CCObject* sender) { | |
| 42 CCLog("exit pressed"); | |
| 43 CCDirector::sharedDirector()->popScene(); | |
| 46 } | 44 } |
| 45 | |
| 46 | |
| 47 bool UILayer::init() { | |
| 48 if (!CCLayer::init()) | |
| 49 return false; | |
| 50 setTouchEnabled(true); | |
| 51 | |
| 52 // Create the menu. | |
| 53 CCLabelTTF* restart_label = CCLabelTTF::create("Restart", "Arial.ttf", 24); | |
| 54 CCLabelTTF* exit_label = CCLabelTTF::create("Exit", "Arial.ttf", 24); | |
| 55 | |
| 56 CCMenuItemLabel* restart = CCMenuItemLabel::create( | |
| 57 restart_label, this, menu_selector(UILayer::restart)); | |
| 58 | |
| 59 CCMenuItemLabel* exit = CCMenuItemLabel::create( | |
| 60 exit_label, this, menu_selector(UILayer::exit)); | |
| 61 | |
| 62 CCMenu* menu = CCMenu::create(restart, exit, NULL); | |
| 63 addChild(menu); | |
| 64 | |
| 65 // Position the menu | |
| 66 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); | |
| 67 CCSize label_size = restart_label->getContentSize(); | |
| 68 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); | |
| 69 menu->alignItemsVertically(); | |
| 70 float xpos = origin.x + visibleSize.width*0.05 + label_size.width/2; | |
| 71 float ypos = origin.y + visibleSize.height*0.95 - label_size.height; | |
| 72 menu->setPosition(ccp(xpos, ypos)); | |
| 73 return true; | |
| 74 } | |
| OLD | NEW |