cocos2dx 利用随机数模拟雪花飘落、粒子系统

前端之家收集整理的这篇文章主要介绍了cocos2dx 利用随机数模拟雪花飘落、粒子系统前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

运行截图

随机

  • 注意随机种子的设定需要在for循环的外面
  • 用到了cocos2dx的 CCRANDOM_0_1
/** @def CCRANDOM_0_1 returns a random float between 0 and 1 */
#define CCRANDOM_0_1() ((@H_301_16@float)rand()/RAND_MAX)
TIMEVAL psv;
gettimeofday(&psv,NULL);
// 初始化随机种子
// timeval是个结构体,里边有俩个变量,一个是以秒为单位的,一个是以微妙为单位的 
@H_301_16@unsigned rand_seed = (@H_301_16@unsigned)(psv.tv_sec * 1000 + psv.tv_usec / 1000);    //都转化为毫秒 
srand(rand_seed);

@H_301_16@for (@H_301_16@int i = 0; i < N; i++)
{
    @H_301_16@int a = 0;
    @H_301_16@int b = visibleSize.width;
    @H_301_16@float xt = CCRANDOM_0_1() * (b - a + 1) + a;

    a = 0;
    b = visibleSize.height;
    @H_301_16@float yt = CCRANDOM_0_1() * (b - a + 1) + a;

    vx[i] = origin.x + xt;
    vy[i] = origin.y + yt;

    labels[i] = Label::createWithSystemFont("*","Arial",24);

    // position the label on the center of the screen
    labels[i]->setPosition(Point(vx[i],vy[i]));

    // add the label as a child to this layer
    @H_301_16@this->addChild(labels[i],1);
}

完整代码

.h

#ifndef __HELLOWORLD_SCENE_H__
#@H_301_16@define __HELLOWORLD_SCENE_H__

#include <vector>

#include "cocos2d.h"

#@H_301_16@define N 200

USING_NS_CC;

class HelloWorld : @H_301_16@public cocos2d::Layer
{
@H_301_16@public:
    // there's no 'id' @H_301_16@in cpp,so we recommend returning the @H_301_16@class instance pointer
    @H_301_16@static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
    @H_301_16@virtual @H_301_16@bool init();  

    // a selector callback
    @H_301_16@void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
@H_301_16@public:
    @H_301_16@virtual @H_301_16@void update(@H_301_16@float delta);

    @H_301_16@void menuStopCallback(cocos2d::Ref* pSender);
    @H_301_16@void menuSlowCallback(cocos2d::Ref* pSender);
    @H_301_16@void menuMidCallback(cocos2d::Ref* pSender);
    @H_301_16@void menuFastCallback(cocos2d::Ref* pSender);

    MenuItemFont *stop;
    //
    MenuItemFont *slow;
    //
    MenuItemFont *mid;
    //
    MenuItemFont *fast;
@H_301_16@public:
    Size visibleSize;
    Point origin;

    Label* labels[N];
    @H_301_16@float speed;
    @H_301_16@float vx[N];
    @H_301_16@float vy[N];
};

#@H_301_16@endif // __HELLOWORLD_SCENE_H__

.cpp

#include "HelloWorldScene.h"
#include <cstdlib>
#include <ctime>

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    @H_301_16@return scene;
}

// on "init" you need to initialize your instance
@H_301_16@bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    @H_301_16@if ( !Layer::init() )
    {
        @H_301_16@return @H_301_16@false;
    }

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


    // add a label shows "Hello World"
    // create and initialize a label

#@H_301_16@if 0
    //LabelTTF* label = LabelTTF::create("1","Arial",24);
    Label* label = Label::createWithSystemFont("1",24);

    // position the label on the center of the screen
    label->setPosition(Point(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    @H_301_16@this->addChild(label,1);
#@H_301_16@endif

    TIMEVAL psv;
    gettimeofday(&psv,NULL);
    // 初始化随机种子
    // timeval是个结构体,里边有俩个变量,一个是以秒为单位的,一个是以微妙为单位的 
    unsigned rand_seed = (unsigned)(psv.tv_sec * 1000 + psv.tv_usec / 1000);    //都转化为毫秒 
    srand(rand_seed);

    @H_301_16@for (@H_301_16@int i = 0; i < N; i++)
    {
        @H_301_16@int a = 0;
        @H_301_16@int b = visibleSize.width;
        @H_301_16@float xt = CCRANDOM_0_1() * (b - a + 1) + a;

        a = 0;
        b = visibleSize.height;
        @H_301_16@float yt = CCRANDOM_0_1() * (b - a + 1) + a;

        vx[i] = origin.x + xt;
        vy[i] = origin.y + yt;

        labels[i] = Label::createWithSystemFont("*",24);

        // position the label on the center of the screen
        labels[i]->setPosition(Point(vx[i],vy[i]));

        // add the label as a child to this layer
        @H_301_16@this->addChild(labels[i],1);
    }

    speed = 1;

    @H_301_16@this->scheduleUpdate(); // 定时器
    //
    MenuItemFont::setFontSize(22);// 系统设定字体大小
stop = MenuItemFont::create("stop");
    stop->setTarget(@H_301_16@this,menu_selector(HelloWorld::menuStopCallback));
    //
slow = MenuItemFont::create("slow");
    slow->setColor(Color3B::RED);
    slow->setTarget(@H_301_16@this,menu_selector(HelloWorld::menuSlowCallback));
    //
mid = MenuItemFont::create("mid");
    mid->setTarget(@H_301_16@this,menu_selector(HelloWorld::menuMidCallback));
    //
fast = MenuItemFont::create("fast");
    fast->setTarget(@H_301_16@this,menu_selector(HelloWorld::menuFastCallback));

    //创建一个菜单容器 把菜单栏目放上去
    Menu *menu = Menu::create(stop,slow,mid,fast,NULL);
    //显示菜单
    @H_301_16@this->addChild(menu);
    //菜单显示位置
    menu->setPosition(Point(50,visibleSize.height - 100));
    //设置栏目间的宽度距离
    menu->alignItemsVerticallyWithPadding(10.0);


    @H_301_16@return @H_301_16@true;
}

@H_301_16@void HelloWorld::update(@H_301_16@float delta)
{
    @H_301_16@for (@H_301_16@int i = 0; i < N; i++)
    {
        vy[i] -= speed;//2.0;
        @H_301_16@if (vy[i] <= 0)
        {
            @H_301_16@int a = 0;
            @H_301_16@int b = visibleSize.width;
            vx[i] = origin.x + CCRANDOM_0_1() * (b - a + 1) + a;
            vy[i] = visibleSize.height;
        }
        labels[i]->setPosition(Point(vx[i],vy[i]));
    }
}

// 改变下落的速度
@H_301_16@void HelloWorld::menuStopCallback(cocos2d::Ref* pSender)
{
    @H_301_16@this->unscheduleUpdate();
    speed = -1.0;

    stop->setColor(Color3B::RED);
    slow->setColor(Color3B::WHITE);
    mid->setColor(Color3B::WHITE);
    fast->setColor(Color3B::WHITE);
}
@H_301_16@void HelloWorld::menuSlowCallback(cocos2d::Ref* pSender)
{
    @H_301_16@if (speed < 0)
        @H_301_16@this->scheduleUpdate(); 
    speed = 1.0;

    stop->setColor(Color3B::WHITE);
    slow->setColor(Color3B::RED);
    mid->setColor(Color3B::WHITE);
    fast->setColor(Color3B::WHITE);
}
@H_301_16@void HelloWorld::menuMidCallback(cocos2d::Ref* pSender)
{
    @H_301_16@if (speed < 0)
        @H_301_16@this->scheduleUpdate();
    speed = 3.0;

    stop->setColor(Color3B::WHITE);
    slow->setColor(Color3B::WHITE);
    mid->setColor(Color3B::RED);
    fast->setColor(Color3B::WHITE);
}
@H_301_16@void HelloWorld::menuFastCallback(cocos2d::Ref* pSender)
{
    @H_301_16@if (speed < 0)
        @H_301_16@this->scheduleUpdate();
    speed = 6.0;

    stop->setColor(Color3B::WHITE);
    slow->setColor(Color3B::WHITE);
    mid->setColor(Color3B::WHITE);
    fast->setColor(Color3B::RED);
}

@H_301_16@void HelloWorld::menuCloseCallback(Ref* pSender)
{
#@H_301_16@if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    @H_301_16@return;
#@H_301_16@endif

    //Director::getInstance()->end();
    speed = 10;

#@H_301_16@if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#@H_301_16@endif
}

粒子系统实现snow

参考
cocos2dx中一些自定义的例子系统
http://www.2cto.com/kf/201409/333598.html

Particle System(粒子系统)讲解
http://www.cnblogs.com/shangdahao/archive/2012/04/14/2447571.html

bg.png

snow.png

#include "HelloWorldScene.h"
#include <cstdlib>
#include <ctime>

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    @H_301_16@return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    @H_301_16@if ( !Layer::init() )
    {
        @H_301_16@return false;
    }

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

    auto bg = Sprite::create("bg.png");
    bg->setPosition(Point(visibleSize.width / 2,visibleSize.height / 2));
    this->addChild(bg);

#if 0
    ParticleSystem* ps = ParticleSnow::createWithTotalParticles(9999);
    //ParticleSystem* ps = ParticleSnow::create();
    ps->setTexture(Director::getInstance()->getTextureCache()->addImage("snow.png"));
    ps->setPosition(Point(visibleSize.width/2,visibleSize.height + 10));//生成的雪花从这个坐标往下落
    //ps->setEmissionRate(10);
    ps->setSpeed(200);
    ps->setLife(10);
    this->addChild(ps);
#endif

#if 1
    auto particleSystem = ParticleSystemQuad::createWithTotalParticles(200);
    //设置雪花粒子纹理图片
    particleSystem->setTexture(TextureCache::getInstance()->addImage("snow.png"));
    //设置发射粒子的持续时间-1表示永远持续
    particleSystem->setDuration(-1);
    //这个点是相对发射点,x正方向为右,y正方向为上,可以设置发射的方向
    particleSystem->setGravity(Point(-10,-20));

    //设置角度以及偏差
    particleSystem->setAngle(90);
    particleSystem->setAngleVar(360);

    //设置径向加速度以及偏差
    particleSystem->setRadialAccel(10);
    particleSystem->setRadialAccelVar(0);

    //设置粒子的切向加速度以及偏差
    particleSystem->setTangentialAccel(30);
    particleSystem->setTangentialAccelVar(30);

    // 设置粒子初始化位置偏差
    //particleSystem->setPosition(CCPoint(400,500));
    particleSystem->setPosVar(Point(400,0));


    //设置粒子生命期以及偏差
    particleSystem->setLife(4);
    particleSystem->setLifeVar(2);

    //设置粒子开始时候旋转角度以及偏差
    particleSystem->setStartSpin(30);
    particleSystem->setStartSpinVar(60);

    //设置结束时候的旋转角度以及偏差
    particleSystem->setEndSpin(60);
    particleSystem->setEndSpinVar(60);

    //设置开始时候的颜色以及偏差
    particleSystem->setStartColor(Color4F(1,1,1));
    //设置结束时候的颜色以及偏差
    particleSystem->setEndColor(Color4F(0.9,0.9,1));

    //设置开始时候粒子大小以及偏差
    particleSystem->setStartSize(30);
    particleSystem->setStartSizeVar(0);

    //设置粒子结束时候大小以及偏差
    particleSystem->setEndSize(20.0f);
    particleSystem->setEndSizeVar(0);

    //设置每秒钟产生粒子的数量
    particleSystem->setEmissionRate(100);

    // 粒子系统位置
    particleSystem->setPosition(Point(visibleSize.width * 0.75,visibleSize.height + 50));

    this->addChild(particleSystem);
#endif

    @H_301_16@return true;
}

void HelloWorld::update(float delta)
{

}

猜你在找的Cocos2d-x相关文章