示例#1
0
// on "init" you need to initialize your instance
bool Dialog::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !LayerColor::initWithColor(Color4B(0, 0, 0, 127)) ) {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Point origin = Director::getInstance()->getVisibleOrigin();

    m_dialog = Layer::create();
    this->addChild(m_dialog);

    EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [] (Touch* touch, Event* e) {
        return true;
    };
    listener->setSwallowTouches(true);

    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    clearLayout();

    return true;
}
KDbool SampleLayer::init ( KDvoid )
{
	if ( !Layer::init ( ) )
	{
		return KD_FALSE;
	}

	m_nMaxJump = 0;

	EventListenerTouchOneByOne* pListner =  EventListenerTouchOneByOne::create ( );
	pListner->setSwallowTouches ( KD_TRUE );

	pListner->onTouchBegan = CC_CALLBACK_2 ( SampleLayer::onTouchBegan, this );

	m_pEventDispatcher->addEventListenerWithSceneGraphPriority ( pListner, this );

	SimpleAudioEngine::getInstance ( )->playBackgroundMusic ( "bgm_main.mp3", KD_TRUE );
	
	this->initBox2DWorld ( );
	this->initItem ( );

	this->addLevelBoundaries ( );
	this->addBackground ( );
	this->addFloor ( );
	this->addCharacter ( );
	
	this->schedule ( schedule_selector ( SampleLayer::step ) );
	//this->schedule ( schedule_selector ( SampleLayer::collisionCheck ), 0.2f );
	
	this->setItem ( );

	return KD_TRUE;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create( );
    listener->setSwallowTouches( true );
    
    listener->onTouchBegan = CC_CALLBACK_2( HelloWorld::onTouchBegan, this );
    listener->onTouchMoved = CC_CALLBACK_2( HelloWorld::onTouchMoved, this );
    listener->onTouchEnded = CC_CALLBACK_2( HelloWorld::onTouchEnded, this );
    
    Director::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );
    
    sprite1 = Sprite::create( "HelloWorld.png" );
    sprite2 = Sprite::create( "HelloWorld.png" );
    
    
    sprite1->setPosition( Vec2( 100, 200 ) );
    sprite2->setPosition( Vec2( 400, 200 ) );
    
    sprite1->setScale( 0.1 );
    
    this->addChild( sprite1 );
    this->addChild( sprite2 );
    
    return true;
}
示例#4
0
void GameLayer::registerEventHandlers() {
    Director* director = Director::getInstance();
    EventDispatcher* eventDispatcher = director->getEventDispatcher();
    
    EventListenerTouchOneByOne* eventListener = EventListenerTouchOneByOne::create();
    eventListener->setSwallowTouches(true);
    
    eventListener->onTouchEnded = [=](Touch* touch, Event* event){
        if (!getAcceptInput()) {
            return;
        }
        
        Vec2 position = m_tileLayer->convertTouchToNodeSpace(touch);
        for (auto iterator = m_tileLayer->getChildren().begin(); iterator != m_tileLayer->getChildren().end(); iterator++) {
            auto child = *iterator;
            GameTileView* gameTileView = dynamic_cast<GameTileView*>(child);
            if(gameTileView->getBoundingBox().containsPoint(position)) {
                if (gameTileView->getGameTile()->getCanMove()) {
                    doSelectGameTileView(gameTileView);
                }
                break;
            }
            
        }
    };
    
    eventListener->onTouchBegan = [](Touch* touch, Event* event){
        return true;
    };
    eventListener->onTouchMoved = [](Touch* touch, Event* event){};
    eventListener->onTouchCancelled = [](Touch* touch, Event* event){};
    eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, m_tileLayer);
}
示例#5
0
 void PianoLayer::onTouchEnded(Touch* touch, Event* event)
 {
	 pauseFlag = true;
	 slider->setEnabled(false);
	 //获取可见区域尺寸
	 Size visibleSize = Director::getInstance()->getVisibleSize();
	 selection->setTexture(pic_RESOURE_PATH + "back.png");
	 underside = Sprite::create(pic_RESOURE_PATH + "blackBackGround.png");
	 underside->setPosition(visibleSize.width/2, visibleSize.height/2);
	 this->addChild(underside, 10);
	 menuBack = Sprite::create(pic_RESOURE_PATH + "kuang.png");
	 menuBack->setPosition(visibleSize.width/2, visibleSize.height/2);
	 menuSize = menuBack->getContentSize();
	 underside->addChild(menuBack, 5);
	 for(int i=1, row=0, col=0; i<9; i++)
	 {
		Sprite* selects = Sprite::create(pic_RESOURE_PATH + "instrument.png");
		sSize = selects->getContentSize();
		//-----------------------------------------------------
		string instrument = getInstrumentChinese(i);
		Label* labels = Label::createWithTTF(instrument, fonts_RESOURE_PATH + "FZKATJW.ttf", 40, Size::ZERO, TextHAlignment::LEFT, TextVAlignment::TOP);
		labels->setColor(Color3B::BLACK);
		selects->addChild(labels, 2);
		selects->setScale(0.9);
		labels->setPosition(Point(sSize.width/2 , sSize.height/2));
		selects->setPosition(Point(220 + 260*col, 300 - 80*row));
		if(i%2 == 0)
		{
			 row++;
			 col--;
		}else
		{
			 col++;
		}
		string number = StringUtils::format("%d", i);
		select.insert(pair<string,Sprite*>(number, selects));
	 }
	 map<string,Sprite*>::iterator iter;
	 for(iter = select.begin(); iter!=select.end(); iter++)
	 {
		 int number = atoi(iter->first.c_str());
		 if(number == instrumentNumber)
		 {
			 iter->second->setTexture(pic_RESOURE_PATH + "yellow.png");
		 }
		 menuBack->addChild(iter->second, 1);
	 }

	 //创建单点触摸监听
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	//设置下传触摸
	listener->setSwallowTouches(false);
	//开始触摸时回调onTouchBegan方法
	listener->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchBeganInstrument, this);
	listener->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchEndedInstrument, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, underside);

 }
示例#6
0
bool MyEditText::init(const std::string& placehold, const std::string& fontName, float fontSize, const Size& constraintSize) {
    if(!Node::init())
        return false;

    this->setCascadeOpacityEnabled(true);
    this->setAnchorPoint(Vec2::ANCHOR_MIDDLE);

    //touch
    EventListenerTouchOneByOne *touchEventListener = EventListenerTouchOneByOne::create();
    touchEventListener->setSwallowTouches(true);
    touchEventListener->onTouchBegan = [=](Touch* touch, Event* event)->bool{
        if(event->getCurrentTarget() == this) {
            Vec2 touchLocation = touch->getLocation();
            touchLocation = this->convertToNodeSpace(touchLocation);

            Rect rect;
            rect.size = _contentSize;

            if(rect.containsPoint(touchLocation)) {
                if(m_isEnable || m_isAttachWithIME)
                    return true;
            } else {
                if(m_isAttachWithIME) {
                    m_textField->detachWithIME();
                    return true;
                }
            }
        }
        return false;
    };
    touchEventListener->onTouchEnded = [=](Touch* touch, Event* event) {
        if(m_isEnable && event->getCurrentTarget() == this) {
            m_textField->attachWithIME();
        }
    };

    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchEventListener, this);

    //textField
    m_textField = MyTextField::create(placehold, fontName, fontSize);
    m_textField->setContentSize(constraintSize);
    m_textField->setPosition(Vec2::ZERO);
    m_textField->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
    m_textField->setVerticalAlignment(TextVAlignment::TOP);
    m_textField->setHorizontalAlignment(TextHAlignment::LEFT);
    m_textField->setOrientation(TextFieldOrientation::HORIZONTAL);
    //限制宽度
    m_textField->setConstraintSize(constraintSize);
    m_textField->setIsUseContraintSize(true);
    this->addChild(m_textField);

    m_textField->setDelegate(this);

    m_isAttachWithIME = false;
    m_isCursorDirty = false;
    m_isEnable = true;

    return true;
}
示例#7
0
void GamePlayScene::initTouchListener() {
    setTouchEnabled(true);
    EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    listener->onTouchBegan = CC_CALLBACK_2(GamePlayScene::onTouchBegan, this);
    listener->onTouchEnded = CC_CALLBACK_2(GamePlayScene::onTouchEnded, this);
    getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
}
void ChatScene::setEventHadlers()
{
    EventDispatcher* pDispatcher = Director::getInstance()->getEventDispatcher();
    EventListenerTouchOneByOne* pEventListener = EventListenerTouchOneByOne::create();
    pEventListener->setSwallowTouches(true);
    
    pEventListener->onTouchBegan = CC_CALLBACK_2(ChatScene::onTouchBegan, this);
    pEventListener->onTouchMoved = CC_CALLBACK_2(ChatScene::onTouchMoved, this);
    pEventListener->onTouchEnded = CC_CALLBACK_2(ChatScene::onTouchEnded, this);
    
    pDispatcher->addEventListenerWithSceneGraphPriority(pEventListener, this);
}
示例#9
0
void GameScreen::CreateListener()
{
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);

	listener->onTouchBegan = CC_CALLBACK_2(GameScreen::onTouchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(GameScreen::onTouchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(GameScreen::onTouchEnded, this);
	listener->onTouchCancelled = CC_CALLBACK_2(GameScreen::onTouchCancelled, this);

	this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
}
示例#10
0
void GameLayer::registerHandler(Node* node, std::function<void(Touch*, Event*)> onTouchEnded) {
    Director* director = Director::getInstance();
    EventDispatcher* eventDispatcher = director->getEventDispatcher();
    
    EventListenerTouchOneByOne* eventListener = EventListenerTouchOneByOne::create();
    eventListener->setSwallowTouches(true);
    eventListener->onTouchEnded = onTouchEnded;
    eventListener->onTouchBegan = [](Touch* touch, Event* event){
        return true;
    };
    
    eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, node);
}
示例#11
0
EventListenerTouchOneByOne* TestTouch::onCreateTouchListen()
{
	EventListenerTouchOneByOne* touchEvent = EventListenerTouchOneByOne::create();
	// 当设置为 true, 则在 onTouchBegan 方法发返回 'true' 将会吃掉触控事件, 防止其他监听器使用这个事件.
	touchEvent->setSwallowTouches(true);
	// 高端的用法,c11中可以直接定义函数回调
	// touchEvent->onTouchBegan = [](Touch* touch, Event* event){return true; };
	touchEvent->onTouchBegan = CC_CALLBACK_2(TestTouch::onMyTouchBegan, this);
	touchEvent->onTouchEnded = CC_CALLBACK_2(TestTouch::onMyTouchEnded, this);
	touchEvent->onTouchMoved = CC_CALLBACK_2(TestTouch::onMyTouchMoved, this);
	touchEvent->onTouchCancelled = CC_CALLBACK_2(TestTouch::onMyTouchCancelled, this);
	return touchEvent;
}
示例#12
0
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
	
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	// add backgroundLayer
	Sprite* background = Sprite::create("background.jpg");
	background->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	addChild(background);

	// create a fish sprite and run animation
  // add the fish sprite to the fishLayer
  fishLayer = Layer::create();
	m_fish = Sprite::createWithSpriteFrameName("fish13_01.png");
	Animate* fishAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("fishAnimation"));
	m_fish->runAction(RepeatForever::create(fishAnimate));
	m_fish->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	fishLayer->addChild(m_fish);

  // create a weapon sprite.
  // add the weapon sprite to the weaponLayer
  weaponLayer = Layer::create();
  m_weapon = Sprite::create("CloseSelected.png");
  m_weapon->setPosition(visibleSize.width / 2, 50);
  weaponLayer->addChild(m_weapon);

  // create a shoot button
  LabelTTF *label = LabelTTF::create("Shoot", "Arial", 20);
  MenuItemLabel *shootLabel = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::onShootClick, this));
  Menu *shoot = Menu::create(shootLabel, NULL);
  shoot->setPosition(visibleSize.width-40, 20);

  // add layers. first add buttom layer.
  addChild(weaponLayer);
  addChild(fishLayer);
  addChild(shoot);      // the menu must be the uppest layer.

	// add touch listener
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    return true;
}
void GamePlayingScene::initial() {
	this->getPhysicsWorld()->setGravity(ParameterManager::getGravity());
	this->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

	SetGameCacheController::getInstance()->setGameCache();

	auto map = CSLoader::createNode(Constant::getLevel1MapCsdPath());
	this->addChild(map,0);

	//加入弓箭手
	this->addChild(ArcherController::getInstance(),1);
	
	//进度条
	this->addChild(ProgressTimeController::getInstance(), 1);

	//地板
	this->addChild(GroundController::getInstance(),1);

	//产生敌人
	this->addChild(EnemyController::getInstance(),1);

	//物理世界监听
	auto listener = EventListenerPhysicsContact::create();
	listener->onContactBegin = CC_CALLBACK_1(GamePlayingScene::onContactBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

	//触屏 listener
	EventListenerTouchOneByOne* screenListener = EventListenerTouchOneByOne::create();
	screenListener->setSwallowTouches(true);// true不向下触摸,简单点来说,比如有两个sprite ,A和 B,A在上B在下(位置重叠)
	screenListener->onTouchBegan = CC_CALLBACK_2(GamePlayingScene::onTouchBegan, this);
	screenListener->onTouchEnded = CC_CALLBACK_2(GamePlayingScene::onTouchEnded, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(screenListener, this);

	auto menu = Menu::create();
	menu->setPosition(Vec2::ZERO);
	menu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
	this->addChild(menu,1);

	Size visibleSize = ParameterManager::getVisibleSize();
	auto gameEndedItem = MenuItemImage::create(Constant::getGameEndedPath(), Constant::getGameEndedPath(), CC_CALLBACK_1(GamePlayingScene::ClickGameEnded, this));
	gameEndedItem->setPosition(visibleSize.width, visibleSize.height);
	gameEndedItem->setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
	menu->addChild(gameEndedItem);

	auto keyboardListener = EventListenerKeyboard::create();
	keyboardListener->onKeyPressed = CC_CALLBACK_2(GamePlayingScene::onKeyPressed,this); 
	_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener,this);
}
// on "init" you need to initialize your instance
bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    SonarCocosHelper::UI::AddCentredBackground( GAME_BACKGROUND_FILEPATH, this );
    
    gridSprite = Sprite::create( GRID_FILEPATH );
    gridSprite->setPosition( SonarCocosHelper::UI::GetScreenCenter( ) );
    this->addChild( gridSprite );
    
    InitGridRects( );
    InitGridPieces( );
    
    for ( int x = 0; x < 3; x++ )
    {
        for ( int y = 0; y < 3; y++ )
        {
            gridArray[x][y] = EMPTY_PIECE;
        }
    }
    
    turn = PLAYER_PIECE;
    
    ai = new AI::AI( turn );
    
    ui = new UI::UI( this );
    
    gameState = STATE_PLAYING;
    
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create( );
    listener->setSwallowTouches( true );
    
    listener->onTouchBegan = CC_CALLBACK_2( GameScene::onTouchBegan, this );
    listener->onTouchMoved = CC_CALLBACK_2( GameScene::onTouchMoved, this );
    listener->onTouchEnded = CC_CALLBACK_2( GameScene::onTouchEnded, this );
    listener->onTouchCancelled = CC_CALLBACK_2( GameScene::onTouchCancelled, this );
    
    Director::getInstance( )->getEventDispatcher( )->addEventListenerWithSceneGraphPriority( listener, this );
    
    return true;
}
示例#15
0
void Plane::addTouchEvent()
{
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(false);
    listener->onTouchBegan = [=](Touch* touch, Event *event) {
        Plane *plane = dynamic_cast<Plane*>(event->getCurrentTarget());
        assert((plane && (plane == this)));
        Point screenPos = touch->getLocation();
        Point pos = plane->convertToNodeSpace(screenPos);
        Size s = plane->getContentSize();
        Rect rect(0, 0, s.width, s.height);
        if (rect.containsPoint(pos) && !Director::getInstance()->isPaused()) {
            this->m_isMoving = true;
            return true;
        }
        return false;
    };

    listener->onTouchMoved = [=](Touch *touch, Event *event) {
        Plane *plane = dynamic_cast<Plane*>(event->getCurrentTarget());
        assert((plane && (plane == this)));
        Point pos = touch->getLocation();
        Point pos1 = touch->getPreviousLocation();
        Point diff = pos - pos1;
        if (this->m_isMoving && !this->m_dead) {
            Point newPos = plane->getPosition() + diff;
            Size size = Director::getInstance()->getWinSize();
            Size content = plane->getContentSize();
            float x = fmax(content.width * 0.5, fmin(newPos.x, size.width - content.width * 0.5));
            float y = fmax(content.height * 0.5, fmin(newPos.y, size.height - content.height * 0.5));

            plane->setPosition(Point(x, y));
        };
    };

    listener->onTouchEnded = [=](Touch *touch, Event *event) {
        Plane *plane = dynamic_cast<Plane*>(event->getCurrentTarget());
        assert((plane && (plane == this)));
        this->m_isMoving = false;
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
示例#16
0
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = std::bind(&HelloWorld::onTouchBegan, this, std::placeholders::_1, std::placeholders::_2);
	listener->onTouchMoved = std::bind(&HelloWorld::onTouchMoved, this, std::placeholders::_1, std::placeholders::_2);
	listener->onTouchEnded = std::bind(&HelloWorld::onTouchEnded, this, std::placeholders::_1, std::placeholders::_2);
	listener->onTouchCancelled = std::bind(&HelloWorld::onTouchCancelled, this, std::placeholders::_1, std::placeholders::_2);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

	_map = TMXTiledMap::create("tmx/map.tmx");
	this->addChild(_map);

	_label = LabelTTF::create();
	_label->setString("Click anywhere on the screen");
	_label->setFontSize(30);
	_label->setPosition(visibleSize.width / 2, visibleSize.height*0.8);
	this->addChild(_label);

	_player = CCSprite::create("tmx/red.png");
	//_player->setAnchorPoint(ccp(0,0));
	Vec2 pos =_map->getLayer("road")->getPositionAt(Vec2(2, 5));
	_player->setPosition(pos.x+_player->getContentSize().width/2 , pos.y+_player->getContentSize().height/2);
	this->addChild(_player);

	_path = nullptr;

	this->schedule(std::bind(&HelloWorld::update,this,std::placeholders::_1), 0.5f,"HelloWorld");

    return true;
}
示例#17
0
bool myScene::init()
{
	if (!Layer::init())
	{
		return false;
	}


	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	
	//add background
	//Sprite* background = Sprite::create("denglu.jpg");
	//background->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	//addChild(background);
	
	//create a fish sprite and run animation
	//mybutton = Sprite::createWithSpriteFrameName("fish13_01.png");
	//Animate* fishAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("fishAnimation"));
	//m_fish->runAction(RepeatForever::create(fishAnimate));
	//mybutton->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	//addChild(button);
	auto button = MenuItemLabel::create(Label::createWithTTF("Start", "fonts/Marker Felt.ttf", 36), CC_CALLBACK_1(myScene::myClick, this));
	//button->setPosition(Vec2(origin.x + visibleSize.width - button->getContentSize().width / 2,
		//origin.y + button->getContentSize().height / 2));
	auto menu = Menu::create(button, NULL);
	//menu->setPosition(Vec2(origin.x + visibleSize.width - menu->getContentSize().width / 2,
		//origin.y + visibleSize.height - menu->getContentSize().height / 2));
	menu->setPosition(visibleSize/2);
	this->addChild(menu,1);
	//addChild(mybutton);
	
	
	//add touch listener
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(myScene::onTouchBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

	return true;
}
示例#18
0
// on "init" you need to initialize your instance
bool TestScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    EventListenerTouchOneByOne *touchListener = EventListenerTouchOneByOne::create();
    touchListener->setSwallowTouches(true);
    touchListener->onTouchBegan = CC_CALLBACK_2(TestScene::onTouchBegan, this);
    
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
    
    return true;
    
}
示例#19
0
void MyScene::onEnter(){
	CCLayer::onEnter();

	EventListenerTouchOneByOne* touchListener = EventListenerTouchOneByOne::create();
	touchListener->setSwallowTouches(true);
	touchListener->onTouchBegan = CC_CALLBACK_2(MyScene::onTouchBegan, this);
	touchListener->onTouchMoved = CC_CALLBACK_2(MyScene::onTouchMoved, this);
	touchListener->onTouchEnded = CC_CALLBACK_2(MyScene::onTouchEnded, this);

	EventListenerKeyboard* keyListener = EventListenerKeyboard::create();
	keyListener->onKeyPressed = CC_CALLBACK_2(InputHandler::onKeyPressed, InputHandler::getInstance());
	keyListener->onKeyReleased = CC_CALLBACK_2(InputHandler::onKeyReleased, InputHandler::getInstance());

	EventListenerMouse* mouseListener = EventListenerMouse::create();
	mouseListener->onMouseMove = CC_CALLBACK_1(MyScene::onMouseMove, this);
	mouseListener->onMouseDown = CC_CALLBACK_1(MyScene::onMouseDown, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
}
示例#20
0
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    this->addChild(label, 1);

    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);
    
	tiledMap = TMXTiledMap::create("res/test2.tmx");
	addChild(tiledMap);
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
	listener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled,this);
	listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
	listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, tiledMap);
    return true;
}
示例#21
0
    void setupKeyListener(IKeyListener* l) {
        // key listener
        keyListener = l;
        listener = EventListenerKeyboard::create();
        listener->onKeyPressed = CC_CALLBACK_2(HeartBeatLayer::onKeyPressed,
                                               this);
        listener->onKeyReleased = CC_CALLBACK_2(HeartBeatLayer::onKeyReleased,
                                                this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
                                                                 this);
        touchListener = EventListenerTouchOneByOne::create();
        touchListener->setSwallowTouches(true);
        touchListener->onTouchBegan = CC_CALLBACK_2(
            HeartBeatLayer::onTouchBegan, this);
        touchListener->onTouchCancelled = CC_CALLBACK_2(
            HeartBeatLayer::onTouchCancelled, this);
        touchListener->onTouchEnded = CC_CALLBACK_2(
            HeartBeatLayer::onTouchEnded, this);

        _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener,
                                                                 this);
    }
示例#22
0
bool PauseLayer::init()
{
    if (!Layer::init()) {
        return false;
    }

    const Size &size=Director::getInstance()->getWinSize();
    
    //灰色背景
    LayerColor* backLayerColor = LayerColor::create(Color4B(25, 25, 25, 125));
    this->addChild(backLayerColor);

    // 暂停
    MenuItemFont *menuItemPause = MenuItemFont::create("Continue", CC_CALLBACK_1(PauseLayer::menuContinueCallBack, this));
    menuItemPause->setPosition(Point(size.width / 2 - 100, size.height / 2));
   // 重新开始
    MenuItemFont *menuItemRestart = MenuItemFont::create("Restart", CC_CALLBACK_1(PauseLayer::menuRestartCallBack, this));
    menuItemRestart->setPosition(Point(size.width / 2 + 100, size.height / 2));
    Menu *menu=Menu::create(menuItemPause, menuItemRestart, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu);

//不声明 不定义 ontouchbegan的使用方法
//    auto callback = [](Touch * ,Event *)
//    {
//        return true;
//    };
//    auto listener = EventListenerTouchOneByOne::create();
//    listener->onTouchBegan = callback;
//    listener->setSwallowTouches(true);
//    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
    EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(PauseLayer::onTouchBegan, this);
    listener->setSwallowTouches(true);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    return true;
}
示例#23
0
bool MenuScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    m_menu = Menu::create();
    m_menu->setPosition(Point::ZERO);
    this->addChild(m_menu);
    
    for (size_t i = 0; i < g_fractals_size; ++i)
    {
        LabelTTF * label = LabelTTF::create(g_fractals[i].fractal_name, "fonts/Marker Felt.ttf", 24);
        MenuItemLabel * menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(MenuScene::menuCallback, this));
        menuItem->setPosition(Point(VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE)));
        
        m_menu->addChild(menuItem, i);
    }
    
   
    EventListenerTouchOneByOne * touchListener = EventListenerTouchOneByOne::create();
    touchListener->setSwallowTouches(true);
    touchListener->onTouchBegan = CC_CALLBACK_2(MenuScene::onTouchBegan, this);
    touchListener->onTouchMoved = CC_CALLBACK_2(MenuScene::onTouchMoved, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);
    
    
    EventListenerMouse * mouseListener = EventListenerMouse::create();
    mouseListener->onMouseScroll = CC_CALLBACK_1(MenuScene::onMouseScroll, this);
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(mouseListener, this);
    
    
    return true;
}
示例#24
0
bool PopLayer::init()
{
    bool bRef = false;
    do{
        CC_BREAK_IF(!CCLayer::init());
        this->setContentSize(Size::ZERO);
        
        // 初始化需要的 Menu
        Menu* menu = CCMenu::create();
        menu->setPosition(Vec2::ZERO);
        setMenuButton(menu);
        
        //屏蔽下方的触摸
        EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
        listener->onTouchBegan = [](Touch *touch, Event *event){
            return true;
        };
        this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
        
        bRef = true;
    }while(0);
    return bRef;
}
示例#25
0
bool ChooseLevelLayer::init()
{
	//调用父类的初始化
	if (!Layer::init())
	{
		return false;
	}
	//标题精灵
	Sprite* ttSprite = Sprite::create("mainpic/title.png");
	ttSprite->setPosition(Point(270,780));
	this->addChild(ttSprite,1);

	bgSpriteO = Sprite::create("scene/scene1.jpg");
	bgSpriteO->setPosition(270,480);
	this->addChild(bgSpriteO,BGZORDER-1);

	bgSpriteT = Sprite::create("scene/scene1.jpg");
	bgSpriteT->setPosition(270,-480);
	this->addChild(bgSpriteT,BGZORDER);

	for(int i=0;i<3;i++)
	{
		gate0Sprite[i] = Sprite::create(StringUtils::format("choose/gate%d.png",i));
		gate0Sprite[i]->setPosition(Point(110+i*160,550));
		this->addChild(gate0Sprite[i],BGZORDER+1);

		int lock = UserDefault::getInstance()->getIntegerForKey(
				Constant::uesrGKLock[i].c_str());
		if(lock == 0)
		{
			Sprite* lockSp = Sprite::create("choose/lock.png");
			lockSp->setPosition(Point(110+i*160,550));
			this->addChild(lockSp,BGZORDER+2);
		}
	}
	Sprite* txtund = Sprite::create("choose/txtund.png");
	txtund->setPosition(Point(270,300));
	this->addChild(txtund,BGZORDER+1);

	taskLabel = Label::createWithTTF("\u70b9\u51fb\u5173\u5361\u67e5\u770b\u4efb\u52a1","fonts/FZKATJW.ttf",48);
	taskLabel->setPosition(Point(250,67));
	txtund->addChild(taskLabel,1);

	//创建下一个菜单项
	MenuItemImage* backMenu = MenuItemImage::create(
			   "mainpic/goback.png",	//平时的图片
			   "mainpic/gobackp.png",	//选中时的图片
			   CC_CALLBACK_0(ChooseLevelLayer::backMenuCallback, this) //点击时执行的回调方法
		);
	//设置下一个菜单项的位置
	backMenu->setPosition(Point(40,920));
	//创建菜单对象
	auto menu = Menu::create(backMenu,NULL);
	//设置菜单位置
	menu->setPosition(Point::ZERO);
	//将菜单添加到布景中
	this->addChild(menu, 1);

	//设置定时回调指定方法干活
	auto director = Director::getInstance();
	auto sched = director->getScheduler();
	//背景滚屏定时回调
	sched->scheduleSelector(SEL_SCHEDULE(&ChooseLevelLayer::bgUpdate),this,0.005,false);

	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(ChooseLevelLayer::onTouchBegan, this);
	listener->onTouchEnded = CC_CALLBACK_2(ChooseLevelLayer::onTouchEnded, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,gate0Sprite[0]);
	for(int i=1;i<3;i++)
	{
		_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),gate0Sprite[i]);
	}
	return true;
}
示例#26
0
// on "init" you need to initialize your instance
bool MenuSence::init()
{

	if (!Layer::init())
	{
		return false;
	}
	//add touch listener
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(MenuSence::onTouchBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto bg_sky = Sprite::create("menu-background-sky.jpg");
	bg_sky->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y + 150));
	this->addChild(bg_sky, 0);

	auto bg = Sprite::create("menu-background.png");
	bg->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y - 60));
	this->addChild(bg, 0);

	//caveAnimation of Spangle
	auto cave = Sprite::createWithSpriteFrameName("cave-0.png");
	Animate* caveAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("caveAnimation"));
	cave->runAction(RepeatForever::create(caveAnimate));
	cave->setPosition(visibleSize.width / 2 + origin.x + 160, visibleSize.height / 2 + origin.y - 60);
	this->addChild(cave, 1);

	auto miner = Sprite::create("menu-miner.png");
	miner->setPosition(Vec2(150 + origin.x, visibleSize.height / 2 + origin.y - 60));
	this->addChild(miner, 1);

	auto leg = Sprite::createWithSpriteFrameName("miner-leg-0.png");
	Animate* legAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("legAnimation"));
	leg->runAction(RepeatForever::create(legAnimate));
	leg->setPosition(110 + origin.x, origin.y + 102);
	this->addChild(leg, 1);

	
	auto gold = Sprite::create("menu-start-gold.png");
	gold->setPosition(Vec2(visibleSize.width / 1.3 + origin.x, visibleSize.height / 2 + origin.y - 160));
	this->addChild(gold, 0);

	//Click the Start button to switch scenes and change
	auto startItem = MenuItemImage::create(
		"start-0.png",
		"start-1.png",
		CC_CALLBACK_1(MenuSence::menuCallback, this));

	startItem->setPosition(Vec2(visibleSize.width / 1.3 + origin.x, visibleSize.height / 2 + origin.y - 120));

	// create menu, it's an autorelease object
	auto menu = Menu::create(startItem, NULL);
	menu->setPosition(Vec2::ZERO);
	this->addChild(menu, 1);

	auto text = Sprite::create("gold-miner-text.png");
	text->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 1.2 + origin.y));
	this->addChild(text, 1);

	return true;
}
示例#27
0
//实现PianoLayer类中的init方法,初始化布景
bool PianoLayer::init()
{
	//调用父类的初始化
    if ( !Layer::init() )
    {
        return false;
    }
    
    degree = 0;
    musicFlag = false;
    pauseFlag = false;
    instrumentName = "piano";
    instrumentNumber = 1;
    changeFlag = true;
    musicNum = 0;

    //获取可见区域尺寸
    Size visibleSize = Director::getInstance()->getVisibleSize();
    //获取可见区域原点坐标
    Point origin = Director::getInstance()->getVisibleOrigin();

    Sprite* volume = Sprite::create(pic_RESOURE_PATH + "volume_cn.png");				//音量
    volumeSize = volume->getContentSize();
    volume->setScale(1.2);
    volume->setPosition(Point(volumeSize.width/2 + 20, visibleSize.height - volumeSize.height/2));
    this->addChild(volume, 2);

    exit = Sprite::create(pic_RESOURE_PATH + "exit.png");			//退出
    Size exitSize = exit->getContentSize();
    exit->setScale(1.2);
    exit->setPosition(Point(visibleSize.width - exitSize.width/2, visibleSize.height - exitSize.height/2));
    this->addChild(exit, 2);

    slider = Slider::create();
	slider->loadBarTexture(pic_RESOURE_PATH + "soundBackGround.png");
	slider->loadSlidBallTextures(pic_RESOURE_PATH + "transparent.png",pic_RESOURE_PATH + "transparent.png", "");
	slider->loadProgressBarTexture(pic_RESOURE_PATH + "sound.png");
	slider->setAnchorPoint(Point(0, 0.5));
	sliderSize = slider->getContentSize();
	slider->setPercent(CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume()*100);
	slider->setPosition(Point(volumeSize.width*1.2 + 20,  visibleSize.height - volumeSize.height/2));
	slider->addEventListener(CC_CALLBACK_2(PianoLayer::sliderEvent, this));
	this->addChild(slider, 5);

	instrument = Sprite::create(pic_RESOURE_PATH + "piano_cn.png");	//选择
    Size selectSize = instrument->getContentSize();
    instrument->setPosition(
    		Point(
    				visibleSize.width - exitSize.width - selectSize.width/2 -20,
    				visibleSize.height - selectSize.height/2
    		));
    this->addChild(instrument, 2);

    selection = Sprite::create(pic_RESOURE_PATH + "back.png");			//背景
    selection->setPosition(
    		Point(
    				visibleSize.width - exitSize.width - selectSize.width/2 -20,
    				visibleSize.height - selectSize.height/2
    		));
    this->addChild(selection, 1);

    Sprite* left = Sprite::create(pic_RESOURE_PATH + "left.png");			//左键
    leftSize = left->getContentSize();
    left->setScale(SCALE);
    left->setPosition(
    		Point(
    				leftSize.width/2*SCALE + 10,
    				visibleSize.height - volumeSize.height - leftSize.height*SCALE/2 - 5
    		));
    this->addChild(left, 2);

    Sprite* right = Sprite::create(pic_RESOURE_PATH + "right.png");		//右键
    rightSize = right->getContentSize();
    right->setScale(SCALE);
    right->setPosition(
    		Point(
    				visibleSize.width - rightSize.width*SCALE/2 - 10,
    				visibleSize.height - volumeSize.height - rightSize.height*SCALE/2 - 5
    		));
    this->addChild(right, 2);

    //第二个参数为最大储存数
    SpriteBatchNode* batchNode = SpriteBatchNode::create(pic_RESOURE_PATH + "white_small.jpg", 50); 		//小键盘
	batchNode->setAnchorPoint(Point(0, 0));
	batchNode->setPosition(
			Point(
					leftSize.width*SCALE + LEFT_INTERVAL,
					visibleSize.height - volumeSize.height - ABOVE_INTERVAL
				));
	this->addChild(batchNode);

	for(int i = 0; i<PIANO_KEY; i++)									//小键盘
	{
		Sprite* whiteSmall = Sprite::createWithTexture(batchNode->getTexture(), Rect(0, 0, 34, 57));
		whiteSmall->setScale(SCALE);
		whiteSmall->setAnchorPoint(Point(0, 0));
		smallSize = whiteSmall->getContentSize();
		whiteSmall->setPosition(Point(i*smallSize.width*SCALE,0));
		batchNode->addChild(whiteSmall, 2);
	}

	selectBack = Sprite::create(pic_RESOURE_PATH + "selectBack.png");
	backSize = selectBack->getContentSize();
	selectBack->setPosition(
			Point(
					leftSize.width*SCALE + LEFT_INTERVAL + smallSize.width*SCALE*7 + backSize.width/2,
					visibleSize.height - volumeSize.height - 37
			));
	this->addChild(selectBack, 4);

	float width = PIANO_KEY*smallSize.width*SCALE - backSize.width;   						//总长
	percent = (selectBack->getPosition().x - backSize.width/2 - leftSize.width*SCALE - LEFT_INTERVAL) / width;

	float positionX = -percent*WHITE_INTERVAL*13;									//
	for(int i=0; i<PIANO_KEY; i++)
	{
		Sprite* white = Sprite::create(pic_RESOURE_PATH + "white.png");
		white->setScale(1.12);
		white->setAnchorPoint(Point(0, 0));
		white->setPosition(Point(positionX + i*WHITE_INTERVAL, 0));
		string tempNumber = StringUtils::format("%d", i);
		whiteSize = white->getContentSize();
		piano.insert(pair<string,Sprite*>(tempNumber, white));
	}
	for(int i = PIANO_KEY,j=0;i<38; i++)
	{
		Sprite* black = Sprite::create(pic_RESOURE_PATH + "black.png");
		black->setScale(1.12);
		black->setAnchorPoint(Point(0.5, 0));
		blackSize = black->getContentSize();
		black->setPosition(Point(positionX + (i-21+j)*WHITE_INTERVAL, (whiteSize.height - blackSize.height)*1.12));
		string tempNumber = StringUtils::format("%d", i);
		if((i-21)%5==0||(i-21)==2||(i-21)==7||(i-21)==12)
		{
			j++;
		}
		piano_black.insert(pair<string,Sprite*>(tempNumber, black));
	}

	map<string, Sprite*>::iterator iter;
	for(iter=piano.begin(); iter != piano.end();iter++)
	{
		this->addChild(iter->second, 5);
	}
	for(iter=piano_black.begin(); iter != piano_black.end(); iter++)
	{
		this->addChild(iter->second, 6);
	}

	for(int i = PIANO_KEY,j = 0; i<38; i++)
	{
		Sprite* blackSmall;
		float positionX;
		if(i==37)
		{
			blackSmall = Sprite::create(pic_RESOURE_PATH + "black_small_half.jpg");
			Size size = blackSmall->getContentSize();
			positionX = leftSize.width*SCALE + LEFT_INTERVAL + (i-21+j)*smallSize.width*SCALE-size.width/2;
		}else
		{
			blackSmall = Sprite::create(pic_RESOURE_PATH + "black_small.jpg");
			positionX = leftSize.width*SCALE + LEFT_INTERVAL + (i-21+j)*smallSize.width*SCALE;
		}
		blackSmall->setScale(SCALE);
		blackSmall->setAnchorPoint(Point(0.5, 0));
		blackSmall->setPosition(Point(
				positionX,
				visibleSize.height - volumeSize.height - ABOVE_INTERVAL + smallSize.height*SCALE/2));
		if((i-21)%5==0||(i-21)==2||(i-21)==7||(i-21)==12)
		{
			j++;
		}
		this->addChild(blackSmall, 2);
	}

	//设置定时回调指定方法干活
	auto director = Director::getInstance();
	schedRound = director->getScheduler();

	//创建退出单点触摸监听
	EventListenerTouchOneByOne* listenerExit = EventListenerTouchOneByOne::create();
	//开始触摸时回调onTouchBegan方法
	listenerExit->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchExitBegan, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerExit, exit);
	//创建乐器单点触摸监听
	EventListenerTouchOneByOne* listenerSelect = EventListenerTouchOneByOne::create();
	listenerSelect->setSwallowTouches(true);
	//开始触摸时回调onTouchBegan方法
	listenerSelect->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchBegan, this);
	listenerSelect->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchEnded, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerSelect, instrument);
	//创建左箭头单点触摸监听
	EventListenerTouchOneByOne* listenerLeft = EventListenerTouchOneByOne::create();
	listenerLeft->setSwallowTouches(true);
	//开始触摸时回调onTouchBegan方法
	listenerLeft->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchLeftBegan, this);
	listenerLeft->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchLeftEnded, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerLeft, left);
	//创建右箭头单点触摸监听
	EventListenerTouchOneByOne* listenerRight= EventListenerTouchOneByOne::create();
	listenerRight->setSwallowTouches(true);
	//开始触摸时回调onTouchBegan方法
	listenerRight->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchRightBegan, this);
	listenerRight->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchRightEnded, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerRight, right);
	//创建单点触摸监听
	EventListenerTouchOneByOne* listenerBack = EventListenerTouchOneByOne::create();
	//设置下传触摸
	listenerBack->setSwallowTouches(true);
	//开始触摸时回调onTouchBegan方法
	listenerBack->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchSelectBegan, this);
	listenerBack->onTouchMoved = CC_CALLBACK_2(PianoLayer::onTouchSelectMoved, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerBack, selectBack);
	//---------------------------------单点触控,用于选择钢琴键-----------------------------
	//创建单点触摸监听
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	//开始触摸时回调onTouchBegan方法
	listener->onTouchBegan = CC_CALLBACK_2(PianoLayer::onMyTouchBegan, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

	//---------------------------------多点触控,用于点击钢琴键------------------------------
	//创建一个多点触摸监听
	auto listenerTouch = EventListenerTouchAllAtOnce::create();
	//开始触摸时回调onTouchBegan方法
	listenerTouch->onTouchesBegan = CC_CALLBACK_2(PianoLayer::onMyTouchesBegan, this);
	//触摸移动时回调onTouchMoved方法
	listenerTouch->onTouchesMoved = CC_CALLBACK_2(PianoLayer::onMyTouchesMoved, this);
	//触摸结束时回调onTouchEnded方法
	listenerTouch->onTouchesEnded = CC_CALLBACK_2(PianoLayer::onMyTouchesEnded, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch, this);

    return true;
}
示例#28
0
bool HelpLayer::init()
{
	//调用父类的初始化
	if ( !Layer::init())
	{
		return false;
	}

	MenuItemImage* backItem = MenuItemImage::create
	(
		PIC_PATH+std::string("choose_game_exit_btn.png"),
		PIC_PATH+std::string("choose_game_exit_btn.png"),
		CC_CALLBACK_0(HelpLayer::goBack, this)
	);
	backItem->setPosition(Point(40,510));
	//创建菜单对象
	auto backMenu = Menu::create(backItem, NULL);
	//设置菜单位置
	backMenu->setPosition(Point::ZERO);
	//将菜单添加到布景中
	this->addChild(backMenu,5);

	bgSprite = Sprite::create(PIC_PATH+std::string("background.png"));
	//设置精灵对象的位置
	bgSprite->setPosition(Point(480,270));
	//将精灵添加到布景中
	this->addChild(bgSprite,2);

	for(int i=0;i<5;i++)
	{
		spIntro[i] = Sprite::create((PIC_PATH+StringUtils::format("help%d.png",i)).c_str());//创建显示精灵
		spIntro[i]->setAnchorPoint(Point(0,0));
		spIntro[i]->setPosition(160+i*960,90);
		TBGPoint[i] = spIntro[i]->getPosition();
		this->addChild(spIntro[i],3);
	}

	for(int i=0;i<5;i++)
	{
		if(currIntroIndex==i)
		{
			bSprite[i]=Sprite::create(PIC_PATH+std::string("white.png"));
		}else
		{
			bSprite[i]=Sprite::create(PIC_PATH+std::string("black.png"));
		}
		bSprite[i]->setPosition(Point(300+i*90,60));
		this->addChild(bSprite[i],3);
	}
	//创建一个单点触摸监听
	EventListenerTouchOneByOne* listenerTouch = EventListenerTouchOneByOne::create();
	//设置下传触摸
	listenerTouch->setSwallowTouches(true);
	//开始触摸时回调onTouchBegan方法
	listenerTouch->onTouchBegan = CC_CALLBACK_2(HelpLayer::onMyTouchBegan, this);
	//开始触摸时回调onTouchMoved方法
	listenerTouch->onTouchMoved = CC_CALLBACK_2(HelpLayer::onMyTouchMoved, this);
	//触摸结束时回调onTouchEnded方法
	listenerTouch->onTouchEnded = CC_CALLBACK_2(HelpLayer::onMyTouchEnded, this);
	//添加到监听器
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch,this);

	return true;
}
bool NewGameScene_japan::init()
{
	if (!Layer::init()){
		return false;
	}
	//this->schedule(schedule_selector(TollgateScene::logic));
	Size visibleSize = Director::getInstance()->getVisibleSize();

	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	//set up the border

	// position the label on the center of the screen
	auto label = Label::createWithTTF("Japan Mission!", "fonts/Marker Felt.ttf", 24);
	label->setPosition(Vec2(origin.x + visibleSize.width / 2,
		label->getContentSize().height/2));

	// add the label as a child to this layer
	this->addChild(label, 6);

	



	//menu
	auto menu_item_1 = MenuItemImage::create("Menu/menu.png","Menu/menu_1.png", CC_CALLBACK_1(NewGameScene_japan::GoBack, this));
	
    menu_item_1->setScale(0.8);
    //menu_item_1->setPosition(Vec2(visibleSize.width - menu_item_1->getContentSize().width - borderWidth/2,
    //                              menu_item_1->getContentSize().height / 2));
	menu_item_1->setPosition(Vec2(origin.x + visibleSize.width - menu_item_1->getContentSize().width / 2,
		origin.y + menu_item_1->getContentSize().height / 2));

	
	////background
	auto bg_sprite_1 = Sprite::create("japan/Nihon1.png");

	if (visibleSize.width / bg_sprite_1->getContentSize().width >= visibleSize.height / bg_sprite_1->getContentSize().height)
		bg_sprite_1->setScale(bg_scale = visibleSize.height / bg_sprite_1->getContentSize().height);
	else bg_sprite_1->setScale(bg_scale = visibleSize.width / bg_sprite_1->getContentSize().width);

	bg_sprite_1->setPosition(Point(visibleSize.width / 2, visibleSize.height/2));

	bg_origin = Vec2(bg_sprite_1->getBoundingBox().getMinX(), bg_sprite_1->getBoundingBox().getMinY());
	bg_size = Vec2(bg_sprite_1->getBoundingBox().size);

//    bg_sprite_1->setScale(0.8);
	// position the sprite on the center of the screen
	

	auto Move_Down_1 = MoveTo::create(100, Point(bg_size.width / 2 + bg_origin.x, -bg_size.height / 2));
//	auto action_1 = RepeatForever::create(Move_Down_1);
	bg_sprite_1->runAction(Move_Down_1);
	// add the sprite as a child to this layer
	this->addChild(bg_sprite_1, 4);

	auto bg_sprite_2 = Sprite::create("japan/Nihon2.png");
//    bg_sprite_2->setScale(0.5);
	// position the sprite on the center of the screen
	bg_sprite_2->setScale(bg_scale);
	bg_sprite_2->setPosition(Point(bg_size.width / 2 + bg_origin.x, bg_size.height / 2 + bg_origin.y));
	auto Move_Down_2 = MoveTo::create(300, Point(bg_size.width / 2 + bg_origin.x, -bg_size.height / 2));
//	auto action_2 = RepeatForever::create(Move_Down_2);
	bg_sprite_2->runAction(Move_Down_2);

	// add the sprite as a child to this layer
	this->addChild(bg_sprite_2, 3);
	auto bg_sprite_3 = Sprite::create("japan/Nihon3.png");

	// position the sprite on the center of the screen
	bg_sprite_3->setScale(bg_scale);
	bg_sprite_3->setPosition(Point(bg_size.width / 2 + bg_origin.x, bg_size.height / 2 + bg_origin.y));
	auto Move_Down_3 = MoveTo::create(500, Point(bg_size.width / 2 + bg_origin.x, -bg_size.height / 2));
//	auto action_3 = RepeatForever::create(Move_Down_3);
	bg_sprite_3->runAction(Move_Down_3);

	// add the sprite as a child to this layer
	this->addChild(bg_sprite_3, 2);
	auto bg_sprite_4 = Sprite::create("japan/Nihon4.png");

	

	// position the sprite on the center of the screen
	bg_sprite_4->setScale(bg_scale);
	bg_sprite_4->setPosition(Point(bg_size.width / 2 + bg_origin.x, bg_size.height / 2 + bg_origin.y));
	bg_sprite_4->setTag(1);
	// add the sprite as a child to this layer
	this->addChild(bg_sprite_4, 1);


    // HP label
    
    auto labelHp = Label::createWithSystemFont(StringUtils::format("Hp: %d", HP), "fonts/Marker Felt.ttf", 40);
    labelHp->setColor(Color3B::RED);
    labelHp->setPosition(Vec2(origin.x + visibleSize.width / 2,
                              origin.y + visibleSize.height - label->getContentSize().height));
    labelHp->setTag(110);
    this->addChild(labelHp, 2);

	
	//height label
	score = 0;
	auto labelHeight = Label::createWithSystemFont(StringUtils::format("%d", score), "fonts/Marker Felt.ttf", 40);
	labelHeight->setColor(Color3B::RED);
	labelHeight->setPosition(Vec2(bg_origin.x + bg_size.width / 2,
		bg_origin.y + bg_size.height - labelHp->getContentSize().height - labelHeight->getContentSize().height));
	labelHeight->setTag(111);
	this->addChild(labelHeight, 2);



    
	//boarder

	{
		auto border1 = Sprite::create("japan/border_j.png");
		auto border3 = Sprite::create("japan/border_j.png");
		auto border2 = Sprite::create("japan/border_j_2.png");
		auto border4 = Sprite::create("japan/border_j_2.png");
		border1->setScale(bg_scale);
		border3->setScale(bg_scale);
		border2->setScale(bg_scale);
		border4->setScale(bg_scale);

		borderWidth = border1->getBoundingBox().size.width;

		border1->setPosition(Point(bg_origin.x + borderWidth / 2, bg_origin.y + bg_size.height / 2));
		this->addChild(border1, 10, Scene_Tag::border1);

		border3->setPosition(Point(bg_origin.x + borderWidth / 2, bg_origin.y + bg_size.height *3 / 2));
		this->addChild(border3, 10, Scene_Tag::border3);

		border2->setPosition(Point(bg_origin.x + bg_size.width - borderWidth / 2, bg_origin.y + bg_size.height / 2));
		this->addChild(border2, 10, Scene_Tag::border2);

		border4->setPosition(Point(bg_origin.x + bg_size.width - borderWidth / 2, bg_origin.y + bg_size.height *3 / 2));
		this->addChild(border4, 10, Scene_Tag::border4);

	}


	//player

	m_player = Player::create();
	m_player->setScale(bg_scale);
	m_player->playerWidth *= bg_scale;
	/*
	m_player->allframe.clear();
	for (int i = 1; i < 6; i++){
		auto sf = SpriteFrame::create(StringUtils::format("run/run_left%d.png", i), Rect(0, 0, m_player->playerWidth, m_player->playerWidth));
		m_player->allframe.pushBack(sf);
	}
	*/

	//m_player->spPlayer->setPosition(Point(bg_origin.x + borderWidth + m_player->playerWidth / 2, bg_origin.y + bg_size.height * 0.16f));
	//m_player->setTag(0);
	m_player->setPosition(Point(bg_origin.x + borderWidth + m_player->playerWidth / 2, bg_origin.y + bg_size.height * 0.16f));
    this->addChild(m_player, 11, 0);

	//skill button

	auto abilityButtonItem = MenuItemImage::create(
		"bang.png",
		"bang.png",
		CC_CALLBACK_1(NewGameScene_japan::playerAbility_Teleportation, this)
		);

	abilityButtonItem->setScale(bg_scale);

	abilityButtonItem->setPosition(bg_origin + abilityButtonItem->getBoundingBox().size/2);
    //abilityButtonItem->setTag(112);
	// create menu, it's an autorelease object
	auto *menu = Menu::create(menu_item_1, abilityButtonItem, NULL);
    
	menu->setPosition(Point(0, 0));
	//menu->setColor(Color3B::BLUE);
	this->addChild(menu, 20);

	//===================

	EventListenerTouchOneByOne * event = EventListenerTouchOneByOne::create();
	event->setSwallowTouches(true);
	event->onTouchBegan = CC_CALLBACK_2(NewGameScene_japan::onTouchBegan, this);
	event->onTouchMoved = CC_CALLBACK_2(NewGameScene_japan::onTouchMoved, this);
	event->onTouchEnded = CC_CALLBACK_2(NewGameScene_japan::onTouchEnded, this);
	event->onTouchCancelled = CC_CALLBACK_2(NewGameScene_japan::onTouchCancelled, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(event, this);

    
    //
    enemyLeftRightDis = -1;
    
	//Enemy
    this->schedule(schedule_selector(NewGameScene_japan::logic)); // borderSpeed;
    this->schedule(schedule_selector(NewGameScene_japan::moveEnemyRoof)); // same as borderSpeed
    this->schedule(schedule_selector(NewGameScene_japan::moveEnemyFallen));
    this->schedule(schedule_selector(NewGameScene_japan::moveStar));
    this->schedule(schedule_selector(NewGameScene_japan::moveEnemyLeft));
    this->schedule(schedule_selector(NewGameScene_japan::moveEnemyRight));
    
    
    this->schedule(schedule_selector(NewGameScene_japan::newEnemy),1.5);

	//http://www.hawstein.com/posts/ctci-solutions-contents.html

	this->scheduleUpdate();



	return true;
}
示例#30
0
// on "init" you need to initialize your instance
bool GameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    srand(time(NULL));
    
    Vec2 size = Director::getInstance()->getVisibleSize();
    
    mV = 0;
    
    
    // 创建背景
    auto BackgroundSprite = Sprite::create("background.jpg");
    BackgroundSprite->setAnchorPoint(Vec2(0, 0));
    BackgroundSprite->setPosition(Vec2(0, 0));
    addChild(BackgroundSprite);
    
    // 记分板
    auto SwordSprite = Sprite::createWithSpriteFrameName("knife3.png");
    SwordSprite->setAnchorPoint(Vec2::ZERO);
    SwordSprite->setPosition(Vec2(size.x-155, size.y-118));
    SwordSprite->setScale(0.4, 0.18);
    addChild(SwordSprite);
    
    auto xSprite = Sprite::createWithSpriteFrameName("x.png");
    xSprite->setAnchorPoint(Vec2::ZERO);
    xSprite->setPosition(Vec2(size.x-115, size.y-95));
    xSprite->setScale(0.7);
    addChild(xSprite);
    Score::getInstance()->reset();
    displayScore();
    
    // 添加主角人物
    auto PersonSprite = Sprite::createWithSpriteFrameName("person.png");
    PersonSprite->setAnchorPoint(Vec2(0, 0));
    PersonSprite->setPosition(Vec2(20, 30));
    PersonSprite->setScale(0.7);
    addChild(PersonSprite);
    
    // 添加脸部
    mFaceSprite = Sprite::createWithSpriteFrameName("face1.png");
    mFaceSprite->setAnchorPoint(Vect::ZERO);
    mFaceSprite->setPosition(Vect(73, 546));
    mFaceSprite->setScale(0.7);
    addChild(mFaceSprite);
    
    mFaceAnimationSucc.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("face3.png"));
    mFaceAnimationSucc.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("face1.png"));
    
    mFaceAnimationFail.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("face4.png"));
    mFaceAnimationFail.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("face1.png"));
    
    mFaceAnimationMiss.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("face2.png"));
    mFaceAnimationMiss.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("face1.png"));
    
    // 添加手部
    mHandSprite = Sprite::createWithSpriteFrameName("hand1.png");
    mHandSprite->setAnchorPoint(Vec2(0,0));
    mHandSprite->setScale(0.7);
    mHandSprite->setPosition(Vec2(365,511));
    addChild(mHandSprite);
    
    mHandAnimations.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("hand1.png"));
    mHandAnimations.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("hand2.png"));
    mHandAnimations.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("hand4.png"));
    mHandAnimations.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("hand1.png"));
    
    EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan,this);
    listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded,this);
    listener->setSwallowTouches(true);
    
    Director::getInstance()->getEventDispatcher()->
    addEventListenerWithSceneGraphPriority(listener,this);
    
    // 刀子测试
    //sword->setPosition(Vec2(420, orign.y));
    mKnife = createKnife();
    addChild(mKnife->getKnife());
    
    /*
     663.365173
     ng
     474.713837
     ok
     429.050385
     */
    
    MusicButton *mb = MusicButton::getInstance();
    addChild(mb->getStopSprite(),7);
    addChild(mb->getStartSprite(),7);
    
    this->schedule(schedule_selector(GameScene::downSword), 0.05);
    return true;
}