Ejemplo n.º 1
0
//移动事件
void HRocker::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
	if (!isCanMove)
	{
		return;
	}
	CCPoint point = pTouch->getLocation();
	CCSprite *rocker = (CCSprite *)this->getChildByTag(tag_rocker);
	//得到摇杆与触屏点所形成的角度
	float angle = getRad(rockerBGPosition,point);
	//判断两个圆的圆心距是否大于摇杆背景的半径
	if (sqrt(pow((rockerBGPosition.x - point.x),2) + pow((rockerBGPosition.y - point.y),2)) >= rockerBGR)
	{
	
		//保证内部小圆运动的长度限制
		rocker->setPosition(ccpAdd(getAngelePosition(rockerBGR,angle),ccp(rockerBGPosition.x,rockerBGPosition.y)));
	}
	else
		rocker->setPosition(point);
	//判断方向
	if(angle>=-PI/4&&angle<PI/4)
	{
		rocketDirection=rocker_right;
		rocketRun=false;
	}
	else if(angle>=PI/4&&angle<3*PI/4)
	{
		rocketDirection=rocker_up;
	}
	else if((angle>=3*PI/4&&angle<=PI)||(angle>=-PI&&angle<-3*PI/4))
	{
		rocketDirection=rocker_left;
		rocketRun=true;
	}
	else if(angle>=-3*PI/4&&angle<-PI/4)
	{
		rocketDirection=rocker_down;
	}
}
Ejemplo n.º 2
0
//启动摇杆(显示摇杆、监听摇杆触屏事件)
void HRocker::startRocker(bool _isStopOther)
{
	Sprite *rocker = (Sprite*)this->getChildByTag(tag_rocker);
	rocker->setVisible(true);

	Sprite *rockerBG = (Sprite *)this->getChildByTag(tag_rockerBG);
	rockerBG->setVisible(true);

    auto listenerTouch = EventListenerTouchOneByOne::create();//创建一个触摸监听
    listenerTouch->setSwallowTouches(true);//设置是否想下传递触摸


    //3.0 后可以直接在touchBegan后添加它的实现代码,而不用特意去写一个touchBegan的函数
    listenerTouch->onTouchBegan = [=](Touch* touch, Event* event){ //[]中间的是传入的参数
        CCPoint point = touch->getLocation();
        Sprite *rocker = (Sprite *)this->getChildByTag(tag_rocker);
        if (rocker->boundingBox().containsPoint(point))
            isCanMove = true;
        return true;
    };

    //拖动精灵移动
    listenerTouch->onTouchMoved = [this](Touch* touch, Event* event){
        if (!isCanMove)
        {
            return;
        }
        CCPoint point = touch->getLocation();
        Sprite *rocker = (Sprite *)this->getChildByTag(tag_rocker);
        //得到摇杆与触屏点所形成的角度
        float angle = getRad(rockerBGPosition,point);
        //判断两个圆的圆心距是否大于摇杆背景的半径
        if (sqrt(pow((rockerBGPosition.x - point.x),2) + pow((rockerBGPosition.y - point.y),2)) >= rockerBGR)
        {
            //保证内部小圆运动的长度限制
            rocker->setPosition(ccpAdd(getAngelePosition(rockerBGR,angle),ccp(rockerBGPosition.x,rockerBGPosition.y)));
        }
        else
        {
            rocker->setPosition(point);
        }

        //判断方向
        if(angle>=-PI/4&&angle<PI/4)
        {
            rocketDirection=rocker_right;
            rocketRun=false;
        }
        else if(angle>=PI/4&&angle<3*PI/4)
        {
            rocketDirection=rocker_up;
        }
        else if((angle>=3*PI/4&&angle<=PI)||(angle>=-PI&&angle<-3*PI/4))
        {
            rocketDirection=rocker_left;
            rocketRun=true;
        }
        else if(angle>=-3*PI/4&&angle<-PI/4)
        {
            rocketDirection=rocker_down;
        }
    };

    listenerTouch->onTouchEnded = [=](Touch* touch, Event* event){ // =在c++11里面代表这个lambda表达式中能使用外面的变量
//        if (!isCanMove)
//        {
//            return;
//        }
        Sprite *rockerBG = (Sprite*)this->getChildByTag(tag_rockerBG);
        Sprite *rocker = (Sprite*)this->getChildByTag(tag_rocker);
        rocker->stopAllActions();
        rocker->runAction(CCMoveTo::create(0.08f,rockerBG->getPosition()));
        isCanMove = false;
        rocketDirection=rocker_stay;
    };


    auto listenerKey = EventListenerKeyboard::create();//创建一个触摸监听
    listenerKey->onKeyPressed = [this](EventKeyboard::KeyCode code, Event* event)
    {
        //isCanMove = true;
        switch(code)
        {
        case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
            rocketDirection=rocker_right;
            rocketRun=false;
            break;
        case EventKeyboard::KeyCode::KEY_UP_ARROW:
            rocketDirection=rocker_up;
            break;
        case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
            rocketDirection=rocker_left;
            rocketRun=true;
            break;
        case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
            rocketDirection=rocker_down;
            break;
        default:
            break;
        }
        log("press key");
    };

    listenerKey->onKeyReleased = [this](EventKeyboard::KeyCode code, Event* event)
    {
        Sprite *rockerBG = (Sprite*)this->getChildByTag(tag_rockerBG);
        Sprite *rocker = (Sprite*)this->getChildByTag(tag_rocker);
        rocker->stopAllActions();
        rocker->runAction(CCMoveTo::create(0.08f,rockerBG->getPosition()));
        //isCanMove = false;
        rocketDirection=rocker_stay;
        log("released key");
    };


    //将触摸监听添加到eventDispacher中去
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch ,this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKey ,this);
}