cocos2d-x 翻牌效果 动画 flip card effect

//*** .h
class FakeRotateY : public cocos2d::RotateTo
{
public:
/* creates the action /
static FakeRotateY* create(float duration, float startAngle, float dstAngle, float depth);
/* creates the action with default dept = 6 /
static FakeRotateY* create(float duration, float startAngle, float dstAngle);
/* initializes the action /
bool init(float duration, float startAngle, float dstAngle, float depth);

virtual ~FakeRotateY() {}

protected:

virtual void startWithTarget(cocos2d::Node *target);

virtual void update(float time);

private:

float startAngle_;
float dstAngle_;
float diffAngle_;
float radius_;
float depth_;

cocos2d::Sprite *target_;

};

//**** .cpp

FakeRotateY* FakeRotateY::create(float duration, float startAngle, float dstAngle, float depth)
{
FakeRotateY *action = new (std::nothrow) FakeRotateY();
action->autorelease();

if (action->init(duration, startAngle, dstAngle, depth)) {
    return action;
}

return nullptr;

}

FakeRotateY* FakeRotateY::create(float duration, float startAngle, float dstAngle)
{
//create instance with default depth = 6
return create(duration, startAngle, dstAngle, 6.0);
}

bool FakeRotateY::init(float duration, float startAngle, float dstAngle, float depth)
{
if (RotateTo::initWithDuration(duration, dstAngle, dstAngle)) {
startAngle_ = startAngle;
dstAngle_ = dstAngle;
depth_ = depth;

    target_ = nullptr;

    return true;
}

return false;

}

void FakeRotateY::startWithTarget(Node *target)
{
RotateTo::startWithTarget(target);

radius_ = target->getContentSize().width / 2.0;

calculateAngles(startAngle_, diffAngle_, dstAngle_);

target_ = dynamic_cast<Sprite *>(target); //dynamic_cast maybe overhead, but this action works only with sprites. todo: find better solution

CCASSERT(target_, "this action can be used only with sprite");

}

void FakeRotateY::update(float time)
{
PolygonInfo pi = target_->getPolygonInfo();

pi.triangles.verts[0].vertices.x = cosf(M_PI + CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_ + radius_;
pi.triangles.verts[0].vertices.y = (sinf(M_PI + CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_) / depth_ + target_->getContentSize().height;

pi.triangles.verts[1].vertices.x = cosf(M_PI - CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_ + radius_;
pi.triangles.verts[1].vertices.y = (sinf(M_PI - CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_) / depth_;

pi.triangles.verts[2].vertices.x = cosf(CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_ + radius_;
pi.triangles.verts[2].vertices.y = (sinf(CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_) / depth_ + target_->getContentSize().height;

pi.triangles.verts[3].vertices.x = cosf(-CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_ + radius_;
pi.triangles.verts[3].vertices.y = (sinf(-CC_DEGREES_TO_RADIANS(startAngle_ + diffAngle_*time)) * radius_) / depth_;

target_->setPolygonInfo(pi);

}

//author
https://github.com/MrCapone/cocos2d-x-custom-actions
Demo: https://www.dropbox.com/s/64y0uuj02quextl/cards.webm21
土豆:http://video.tudou.com/v/XMzEzMTMxNjU3Mg==.html?spm=a2h3j.8428770.3416059.1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值