翻牌

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/TX98337/article/details/78832465

#ifndef __HELLOWORLD_SCENE_H__

#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"

USING_NS_CC;


class HelloWorld : public cocos2d::Scene

{

public:

    static cocos2d::Scene* createScene();


    virtual bool init();

    

    // a selector callback

    void menuCloseCallback(cocos2d::Ref* pSender);

    

    // implement the "static create()" method manually

    CREATE_FUNC(HelloWorld);

    void UpdateImageLayer();

    void MakeImageScale(cocos2d::Sprite* image,float Arc,float XScale);

    ///////

    bool TouchBegan(cocos2d::Touch *touch, cocos2d::Event *unusedEvent);

    void TouchMoved(cocos2d::Touch *touch, cocos2d::Event *unusedEvent);

    void TouchEnded(cocos2d::Touch *touch, cocos2d::Event *unusedEvent);

    void TouchCancelled(cocos2d::Touch *touch, cocos2d::Event *unusedEvent);

    

    float m_HaveOpenPer;

    Vec2 m_TouchPoint;

    LayerColor* m_CardBgLayer;

};


#endif // __HELLOWORLD_SCENE_H__





#include "HelloWorldScene.h"

#include "SimpleAudioEngine.h"


USING_NS_CC;


Scene* HelloWorld::createScene()

{

    return HelloWorld::create();

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !Scene::init() )

    {

        return false;

    }

    

    auto 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(HelloWorld::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);

    

    auto listener = EventListenerTouchOneByOne::create();

    listener->setSwallowTouches(true);

    

    listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::TouchBegan, this);

    listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::TouchMoved, this);

    listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::TouchEnded, this);

    listener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::TouchEnded, this);

    

    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    

    m_CardBgLayer=LayerColor::create(Color4B(0,0,0,100),visibleSize.width,visibleSize.height);

    this->addChild(m_CardBgLayer);

    m_HaveOpenPer=24;

    

    Director::getInstance()->setProjection(cocos2d::DisplayLinkDirector::Projection::_3D);

    UpdateImageLayer();

    return true;

}

bool HelloWorld::TouchBegan(Touch *pTouch, Event *pEvent)

{

    m_TouchPoint=pTouch->getLocation();

    return true;

}

void HelloWorld::TouchMoved(Touch *pTouch, Event *pEvent)

{

    Vec2 TouchPoint = pTouch->getLocation();

    auto visibleSize = Director::getInstance()->getVisibleSize();

    visibleSize.height=visibleSize.height/2;

    float movePer=(TouchPoint.y-m_TouchPoint.y)/visibleSize.height*100;

    m_TouchPoint=TouchPoint;

    m_HaveOpenPer=m_HaveOpenPer+movePer;

    if (m_HaveOpenPer>60)

    {//翻牌

        m_HaveOpenPer=60;

    }else if(m_HaveOpenPer<0)

    {

        m_HaveOpenPer=0;

    }

    UpdateImageLayer();

    CCLOG("Move Per..%f",m_HaveOpenPer);

}

void HelloWorld::TouchEnded(Touch *pTouch, Event *pEvent)

{

}

void HelloWorld::UpdateImageLayer()

{

    auto visibleSize = Director::getInstance()->getVisibleSize();

    m_CardBgLayer->removeAllChildren();

    

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

    int MovePost=(int)m_HaveOpenPer/5+1;

    

    {

        std::vector<std::vector<float > > List;

        List.push_back({0,1,1});

        List.push_back({10,1.008,0.95});

        List.push_back({30,1.03,0.9});

        List.push_back({60,1.07,0.78});

        List.push_back({85,1.14,0.63});


        float ChangeHeight=0;

        for (int i=0; i<5 && i<=MovePost ; i++)

        {

            ChangeHeight=0;

            auto sprite = Sprite::create("Card.png");

            Size cardSize=sprite->getContentSize();

            if(i==0){

                sprite->setTextureRect(Rect(0,0,cardSize.width*(1-m_HaveOpenPer/100),cardSize.height));

            }else if(m_HaveOpenPer>0.1)

            {

                float Per=(m_HaveOpenPer-(i-1)*5)/100;

                if(m_HaveOpenPer<i*5 && Per>0)

                {

                    sprite->setTextureRect(Rect(cardSize.width*(1-Per),0,cardSize.width*Per,cardSize.height));

                    ChangeHeight=cardSize.width*(0.05-(m_HaveOpenPer/100-(i-1)*0.05));

                    //CCLOG("展示最末端区域:%d>>>%f,位置偏移:%f",i,(1-Per),ChangeHeight);

                }else if(Per>0)

                {

                    sprite->setTextureRect(Rect(cardSize.width*(1-m_HaveOpenPer/100+(i-1)*0.05),0,cardSize.width*0.05,cardSize.height));

                    //CCLOG("展示中间区域:>>%d-->%f",i,(1-m_HaveOpenPer/100+(i-1)*0.05));

                }

            }else{

                return;

            }

            sprite->setRotation(90);

            MakeImageScale(sprite,List[i][0],List[i][1]);

            sprite->setAnchorPoint(Vec2(1,0.5));

            sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y-i*cardSize.width*0.05*List[i][2]+ChangeHeight));//

            m_CardBgLayer->addChild(sprite);

        }

    }

    MovePost=(int)m_HaveOpenPer/5-4//

    CCLOG("当前区间:%d",MovePost);

    if(m_HaveOpenPer>20)

    {

        std::vector<std::vector<float > > List;

        List.push_back({-60,1.135,0.83});

        List.push_back({-30,1.18,1.05});

        List.push_back({-10,1.21,1.24});

        float CurrentPerInCell=m_HaveOpenPer-20;

        

        CCLOG("当前比例:%f",CurrentPerInCell);

        for (int i=0; i<3 && i<=MovePost ; i++)

        {

            auto sprite = Sprite::create("CardValue.png");

            Size cardSize=sprite->getContentSize();

            if(CurrentPerInCell-i*5<5)

            {

                sprite->setTextureRect(Rect(0,0,cardSize.width*(CurrentPerInCell-i*5)/100,cardSize.height));

                CCLOG("展示最末端区域:%d>>>%f",i,CurrentPerInCell);

            }else{

                sprite->setTextureRect(Rect(cardSize.width*(CurrentPerInCell/100-(i+1)*0.05),0,cardSize.width*0.05,cardSize.height));

                CCLOG("展示中间区域:>>%d",i);

            }

            sprite->setRotation(90);

            MakeImageScale(sprite,List[i][0],List[i][1]);

            

            sprite->setAnchorPoint(Vec2(1,0.5));

            sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y-(4-i-1)*cardSize.width*0.05*List[i][2]));

            m_CardBgLayer->addChild(sprite);

        }

    }

    if(m_HaveOpenPer>35)

    {

        float CurrentPerInCell=m_HaveOpenPer-35;

        auto sprite = Sprite::create("CardValue.png");

        Size cardSize=sprite->getContentSize();

        sprite->setTextureRect(Rect(0,0,cardSize.width*CurrentPerInCell/100,cardSize.height));

        sprite->setRotation(90);

        sprite->setScaleY(1.22);

        sprite->setAnchorPoint(Vec2(1,0.5));

        sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y-cardSize.width*0.013));//

        m_CardBgLayer->addChild(sprite);

    }

}

void HelloWorld::MakeImageScale(Sprite* image,float Arc,float XScale)

{

//    Director::getInstance()->setProjection(cocos2d::DisplayLinkDirector::Projection::_2D);

    image->runAction(Sequence::create(CallFunc::create([image](){

        image->setRotation(90);

    }),OrbitCamera::create(0.0, 1, 0, 0, Arc, 0, 0),CallFunc::create([image,XScale](){

       image->setScaleY(XScale);

    }), NULL));


//    PolygonInfo polygonInfo = image->getPolygonInfo();

//    polygonInfo.triangles.verts[0].vertices = Vec3((LengthDown-LengthTop)/2, LengthHeight, 0); //top left

//    polygonInfo.triangles.verts[1].vertices = Vec3(0, 0, 0); //bottom left

//    polygonInfo.triangles.verts[2].vertices = Vec3((LengthTop-LengthDown)/2+LengthDown, LengthHeight, 0); //top right

//    polygonInfo.triangles.verts[3].vertices = Vec3(LengthDown,0, 0); //bottom right

//    image->setPolygonInfo(polygonInfo);

}

void HelloWorld::menuCloseCallback(Ref* pSender)

{

    //Close the cocos2d-x game scene and quit the application

    Director::getInstance()->end();


    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

    

    /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/

    

    //EventCustom customEndEvent("game_scene_close_event");

    //_eventDispatcher->dispatchEvent(&customEndEvent);

    

    

}




展开阅读全文

没有更多推荐了,返回首页