Cocos2d-x实现Android的Toast功能

Toast
Android的Toast是一个View视图,快速为用户显示少量的信息。主要用于一些提示和帮助。本文实现了Toast最基本的操作能。
代码
PacToast.h

#include "cocos2d.h"
#include "cocos-ext.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
class PacToast : public LayerColor {
    public:
    static void makeText(Node* node,const std::string& msg,const float& time);//静态函数,方便类直接调用
    void removeToast(Node* node);
};

PacToast.cpp

#include "PigSprite.h"
#include "PlaneLayer.h"

PigSprite::PigSprite() {
}

PigSprite::~PigSprite() {
}

bool PigSprite::init() {
    if (!Sprite::init()) {
        return false;
    }
    Size winSize = Director::getInstance()->getWinSize();
    spritepig = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("hero_01.png"));
    this->setPosition(Point(winSize.width / 2, winSize.height / 2));
    this->addChild(spritepig);
    //设置缩放倍数为0.6,也就是变为原来的0.6
    this->setScale(0.6);
    //穿件小猪飞行动画
    f_createAnimate(3, 8);
    //时间调度函数,使每一帧都调用f_followPlane函数来保持小猪在飞机周围
    this->schedule(schedule_selector(PigSprite::f_followPlane));
    return true;
}
/**
 * 获取飞机的位置信息,使小猪的位置始终在飞机周围,函数中判断是否到达边界,以更新小猪
 * 在飞机的左边还是右边
 */
void PigSprite::f_followPlane(float dt) {
    Size winSize = Director::getInstance()->getWinSize();
    auto PlanePos = PlaneLayer::sharedPlane->getChildByTag(AIRPLANE)->getPosition();
    if (PlanePos.x + 60 + spritepig->getContentSize().width <= winSize.width) {
        this->setPosition(Point(PlanePos.x + 60 + spritepig->getContentSize().width / 2,PlanePos.y));
    } else {
        this->setPosition(Point(PlanePos.x - 60 - spritepig->getContentSize().width / 2,PlanePos.y));
    }

}
/**
 * 创建小猪飞行的动画,count为帧动画的数量,fps为每帧的间隔时间,
 * RepeatForever创建无限重复动画,让spritepig来执行这个动画
 */
void PigSprite::f_createAnimate(int count, int fps) {
    char buff[16];
    Animation *panimation = Animation::create();
    panimation->setDelayPerUnit(1.0 / fps);
    for (int id = 1; id <= count; id++) {
        sprintf(buff, "hero_0%d.png", id);
        panimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(buff));
    }
    spritepig->runAction(RepeatForever::create(Animate::create(panimation)));

}

使用代码:

PacToast::makeText(this, "我是Toast!", 2.5f);

感谢:http://www.cfanz.cn/index.php?c=article&a=read&id=207916

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值