bool LCBattleScene::init() { if(!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); //Point origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// auto edgeSp = Sprite::create(); //auto body = PhysicsBody::createEdgeBox(visibleSize,3); //此句仅3.0 alpha0 有效 auto body = PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3); edgeSp->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); edgeSp->setPhysicsBody(body); this->addChild(edgeSp); edgeSp->setTag(0); //setTouchEnabled(true); setKeyboardEnabled(true); initView(); Rect rect = Rect(0, 0, backGround->getContentSize().width, backGround->getContentSize().height); tree = new QuadTree(4, rect); return true; }
bool TrialPanel::init() { for(auto node:_buttons->getChildren()) { auto button = static_cast<Button*>(node); button->addTouchEventListener(CC_CALLBACK_2(TrialPanel::onButtonClicked, this)); } _container->setTouchEnabled(true); _container->setAnchorPoint(Vec2(0,0)); _listView = static_cast<ListView*>(Helper::seekWidgetByName(_root, "ListView_content")); _achieveUnit = static_cast<Layout*>(Helper::seekWidgetByName(_root, "Panel_achieveUnit")); _achieveUnit->retain(); _achieveUnit->removeFromParent(); _listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(TrialPanel::selectedItemEvent, this)); _listView->addEventListener((ui::ListView::ccScrollViewCallback)CC_CALLBACK_2(TrialPanel::selectedItemEventScrollView,this)); _container->setScale(0.85f); _container->setOpacity(200); _container->runAction(FadeIn::create(0.2)); _container->runAction(ScaleTo::create(0.2,1.0f,1.0f,1.0f)); _root->setTouchEnabled(true); _container->setTouchEnabled(false); _root->addTouchEventListener(CC_CALLBACK_2(TrialPanel::onButtonClicked, this)); setKeyboardEnabled(true); return true; }
void SinglePlayerScene::onEnter() { cocos2d::Node::onEnter(); CCBAnimationManager *animationManager = dynamic_cast<CCBAnimationManager*>(this->getUserObject()); animationManager->setDelegate(this); setKeyboardEnabled(true); if (!m_enterred) { _playerSprite->setSpriteFrame(StringUtils::format("%ssteady.png", m_framePrefix.c_str())); auto bg = Director::getInstance()->getTextureCache()->addImage(m_game->getBackground()); _background->setTexture(bg); this->setupPhysics(); this->setupListeners(); if (!m_game->isPracticeMode()) { m_game->getPlayer()->addPlays(1); } } m_enterred = true; }
// on "init" you need to initialize your instance bool LevelSelect::init() { Size visibleSize = Director::getInstance()->getVisibleSize(); auto BackGround = Sprite::createWithSpriteFrameName("b8.png"); BackGround->setPosition(visibleSize.width / 2, visibleSize.height / 2); addChild(BackGround, 0); auto Stage = Sprite::createWithSpriteFrameName("stage.png"); Stage->setPosition(visibleSize.width / 2, visibleSize.height / 2); addChild(Stage, 1); auto Back = Sprite::createWithSpriteFrameName("back.png"); Back->setAnchorPoint(Point(0, 0)); Back->setPosition(0, visibleSize.height - 400 - 80); addChild(Back, 1); auto IQ = Label::createWithTTF("", "fonts/Futura (Light).ttf", 86); IQ->setString("IQ:" + Value(UserDefault::getInstance()->getIntegerForKey("IQ")).asString()); IQ->setColor(Color3B(255, 255, 255)); IQ->setPosition(220, 370); addChild(IQ, 2); auto Pour = Sprite::createWithSpriteFrameName("start2.png"); Pour->setAnchorPoint(Point(0, 0)); Pour->setPosition(422, visibleSize.height - 282 - 70); addChild(Pour, 2); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [=](Touch* touch, Event* event) { if (Pour->getBoundingBox().containsPoint(touch->getLocation())) { Director::getInstance()->replaceScene(TableViewTestLayer::createScene()); } if (Back->getBoundingBox().containsPoint(touch->getLocation())) { Director::getInstance()->replaceScene(HelloScene::createScene()); } return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); setKeyboardEnabled(true); auto BacklListener = EventListenerKeyboard::create(); //和回调函数绑定 BacklListener->onKeyReleased = CC_CALLBACK_2(LevelSelect::onKeyReleased, this); //添加到事件分发器中 Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(BacklListener, this); return true; }
bool GameMap::init() { if (!Layer::init()) { return false; } setKeyboardEnabled(true); initFromCSBFile(); GameMap::getButtonsFromRoot(); return true; }
KeyboardTest::KeyboardTest() { Size s = Director::sharedDirector()->getWinSize(); LabelTTF* label = LabelTTF::create("Keyboard Test", "Arial", 28); addChild(label, 0); label->setPosition( ccp(s.width/2, s.height-50) ); setKeyboardEnabled(true); // create a label to display the tip string _label = LabelTTF::create("Please press any key and see console log...", "Arial", 22); _label->setPosition(ccp(s.width / 2, s.height / 2)); addChild(_label, 0); _label->retain(); }
void Layer::setKeypadEnabled(bool enabled) { setKeyboardEnabled(enabled); }
// on "init" you need to initialize your instance bool MainMenuLayer::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(MainMenuLayer::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); // add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); // Start GameLevelLayer button auto startGameButton = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(MainMenuLayer::startGameCallback, this)); startGameButton->setPosition(Vec2(origin.x + visibleSize.width / 3, origin.y + visibleSize.height - startGameButton->getContentSize().height)); auto menu2 = Menu::create(startGameButton, NULL); menu2->setPosition(Vec2::ZERO); this->addChild(menu2, 1); // adding a update function this->schedule(schedule_selector(MainMenuLayer::update)); setKeyboardEnabled(true); return true; }
bool ShopPanel::init() { /*for(auto node:_buttons->getChildren()) { auto button = static_cast<Button*>(node); button->addTouchEventListener(CC_CALLBACK_2(ShopPanel::onButtonClicked, this)); }*/ //_container->setTouchEnabled(true); _container->setAnchorPoint(Vec2(0,0)); _listView = static_cast<ListView*>(Helper::seekWidgetByName(_root, "ListView_content")); _goodPan = static_cast<Layout*>(Helper::seekWidgetByName(_root, "Panel_good")); _goodPan->retain(); _goodPan->removeFromParent(); _listView->addEventListener((ui::ListView::ccListViewCallback)CC_CALLBACK_2(ShopPanel::selectedItemEvent, this)); _listView->addEventListener((ui::ListView::ccScrollViewCallback)CC_CALLBACK_2(ShopPanel::selectedItemEventScrollView,this)); //_title = Label::create("","Arial",20); //_title->setColor(Color3B::BLACK); //_title->setPosition(0, _container->getSize().height / 2 - 30); //_container->addChild(_title); //_label = Label::create("","Arial",17); //_label->setColor(Color3B::BLACK); //_label->setLineBreakWithoutSpace(true); //_label->setAnchorPoint(Vec2(0,0)); //_label->setDimensions(_scrollView->getSize().width,0); ////_label->setPositionY(_scrollView->getSize().height / 2); ////_label->setPositionX(_scrollView->getSize().width / 2); //_scrollView->addChild(_label); //auto clipper = ClippingNode::create(); //clipper->setContentSize(_scrollView->getSize()); ////clipper->setPositionY(_scrollView->getSize().height / 2 + _scrollView->getPosition().y); ////clipper->setAnchorPoint(Vec2(0.5, 0.5)); //_container->addChild(clipper); //auto stencil = DrawNode::create(); //Vec2 rectangle[4]; //rectangle[0] = Vec2(-clipper->getContentSize().width / 2, -clipper->getContentSize().height / 2); //rectangle[1] = Vec2(-clipper->getContentSize().width / 2, clipper->getContentSize().height / 2); //rectangle[2] = Vec2(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2); //rectangle[3] = Vec2(clipper->getContentSize().width / 2, -clipper->getContentSize().height / 2); //Color4F white(1, 1, 1, 1); //stencil->drawPolygon(rectangle, 4, white, 1, white); //clipper->setStencil(stencil); //_scrollView->retain(); //_scrollView->removeFromParent(); //clipper->addChild(_scrollView); //_scrollView->release(); auto goodNum = cocos2d::Value(PlotScript::sharedHD()->getLuaVarString("script/Test.lua", "goodsNum")).asInt(); for(int i = 1; i <= goodNum; i++) { auto goodName = "good" + cocos2d::Value(i).asString(); auto content = PlotScript::sharedHD()->getLuaVarString("script/Test.lua", goodName.c_str()); ValueMap valueMap; stringToValueMap(std::string(content), valueMap); int lock = valueMap["lock"].asInt(); if(conditions[goodName+"_unlock"].intValue() == 1) lock = 0; if(lock == 0) { auto goodPan = _goodPan->clone(); auto goodUnit = GoodUnit::create(i); ValueMap model; model["typeId"] = valueMap["typeId"]; auto good = CellFactory::getInstance()->createCell(model); goodUnit->setGood(good); goodUnit->setPrice(valueMap["price"].asInt()); goodUnit->setNum(valueMap["num"].asInt()); goodUnit->setView(goodPan); _goodsUnit.pushBack(goodUnit); goodPan->setTag(i); _listView->pushBackCustomItem(goodPan); } } _container->setScale(0.85f); _container->setOpacity(200); _container->runAction(FadeIn::create(0.2)); _container->runAction(ScaleTo::create(0.2,1.0f,1.0f,1.0f)); _root->setTouchEnabled(true); _container->setTouchEnabled(false); _root->addTouchEventListener(CC_CALLBACK_2(ShopPanel::onButtonClicked, this)); setKeyboardEnabled(true); return true; }
// on "init" you need to initialize your instance bool MapLayer::init() { ////////////////////////////// // 1. super init first if ( !LayerColor::initWithColor( Color4B( 255, 0, 0, 255 ))) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); auto img = Sprite::create("image/map.png"); // img->setPosition( getPosition()); // img->setContentSize( Size( MAP_WIDTH, MAP_HEIGHT )); img->setPosition( MAP_WIDTH/2, MAP_HEIGHT/2); this->addChild( img, 0 ); setContentSize( Size( MAP_WIDTH, MAP_HEIGHT ) ); setPosition( ( visibleSize.width - MAP_WIDTH ) / 2, ( visibleSize.height - MAP_HEIGHT ) / 2 ); // this->setContentSize( Size( 100, 100 )); //////////////////////////////// this->setTouchEnabled( true ); auto m_touchListener = EventListenerTouchOneByOne::create(); // m_touchListener->setSwallowTouches(true); m_touchListener->onTouchBegan = CC_CALLBACK_2(MapLayer::onTouchBegan, this); m_touchListener->onTouchMoved = CC_CALLBACK_2(MapLayer::onTouchMoved, this); m_touchListener->onTouchEnded = CC_CALLBACK_2(MapLayer::onTouchEnded, this); EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); dispatcher->addEventListenerWithFixedPriority( m_touchListener, 1 ); //////////////////////////////// auto keyboardListener = EventListenerKeyboard::create(); // EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); keyboardListener->onKeyPressed = CC_CALLBACK_2( MapLayer::onKeyPressed, this ); keyboardListener->onKeyReleased = CC_CALLBACK_2( MapLayer::onKeyReleased, this ); dispatcher->addEventListenerWithFixedPriority( keyboardListener, 1 ); // setKeyboardEnabled( true ); setKeyboardEnabled( true ); ///////////////////////////////////////////////////////////////////////////////////////////////////////// auto label = LabelTTF::create("test test.", "Arial", 30); // position the label on the center of the screen label->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); label->setColor( Color3B( 255, 0, 0 )); // add the label as a child to this layer this->addChild(label, 1); return true; }