运行截图

随机数

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

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

    a = 0;
    b = visibleSize.height;
    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
    this->addChild(labels[i],1);
}

完整代码

.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include <vector>

#include "cocos2d.h"

#define N 200

USING_NS_CC;

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

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

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

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

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

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

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

#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
    return scene;
}

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

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


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

#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
    this->addChild(label,1);
#endif

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

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

        a = 0;
        b = visibleSize.height;
        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
        this->addChild(labels[i],1);
    }

    speed = 1;

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

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


    return true;
}

void HelloWorld::update(float delta)
{
    for (int i = 0; i < N; i++)
    {
        vy[i] -= speed;//2.0;
        if (vy[i] <= 0)
        {
            int a = 0;
            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]));
    }
}

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

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

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

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

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

void HelloWorld::menuCloseCallback(Ref* pSender)
{
#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");
    return;
#endif

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

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#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
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        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

    return true;
}

void HelloWorld::update(float delta)
{

}

cocos2dx 利用随机数模拟雪花飘落、粒子系统的更多相关文章

  1. iOS和Android的常用随机数生成器

    如果我们在两者中都提供相同的种子,我需要一个在iOS和Android中产生相同数字序列的随机数生成器.我用srand(1000)尝试了rand()函数.但它给出了不同的输出.然后我尝试了mersennetwister.但这也为同一种子提供了不同的序列.有谁可以帮我这个.我正在使用cocos2d-x进行开发.解决方法我已经改编了一个在线CRandomMersenne库,我真的很抱歉,我再也找不到那个

  2. Swift - Swift生成随机数

    在Swift中生成随机数有很多方法可以达到目的这里介绍最简单的两种方法,第一种是使用arc4random()函数,第二种是使用arc4random_uniform()函数1.funcarc4random()->UInt32如果要生成一个生成在一定范围内的随机整数,可以这么写:该方法会生成[min,max]范围内的随机整数如果需要生成随机浮点数,基本思路相同,只不过多了一步因为arc4random返

  3. Swift 中随机数的使用

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  4. Swift随机数产生

    参考Swifterarc4random是一个十分优秀的随机数算法,并且在Swift中也可以使用。它会返回给我们一个任意整数,我们想要在某个范围里的数的话,可以做模运算取余数就行了。但是Swift的Int是和cpu构架有关的:在32位的cpu上实际上他是Int32,而在64位cpu是Int64。arc4random所返回的值不论在什么平台上都是一个UInt32,于是32位的平台就有几率进行Int转换时越界。

  5. Swift - 使用arc4random()、arc4random_uniform()取得随机数

    arc4random()这个全局函数会生成9位数的随机整数1,下面是使用arc4random函数求一个1~100的随机数1vartemp:Int=Int+12,下面是使用arc4random_uniform函数求一个1~100的随机数temp:Int=Int+1

  6. 如何在苹果的Swift语言中生成一个随机数?

    我意识到Swift的书提供了一个随机数生成器的实现。最好的做法是在自己的程序中复制和粘贴此实现?还是有一个库,这样做,我们可以使用吗?使用标准库函数来获得高质量的随机数:arc4random()或arc4random_uniform(),就像在Objective-C中一样。它们在Darwin模块中,因此如果您没有导入AppKit,UIKit或Foundation,则需要导入Darwin。

  7. 用Swift生成随机数

    我需要生成一个随机数。它看起来arc4random函数不再存在以及arc4random_uniform函数。我具有的选项是arc4random_stir(),arc4random_buf和arc4random_addrandom。我找不到任何文档的功能,没有在头文件中的注释提示。Tolearnmoreaboutdrand48()andits“family”ofsimilarfunctionscheckthe07000arc4random_uniformReturnsarandomnumberbetween

  8. 如何在Swift中不重复生成随机数?

    25个如何在不使用Swift语言两次调用数字的情况下生成0到31之间的随机数?您可以创建一个返回随机数生成闭包的函数,如下所示:要使用它,首先调用生成器一次以创建随机序列生成器,然后根据需要多次调用返回值.这将打印出1到6之间的随机值,在重新开始之前循环遍历所有值:

  9. swift – SceneKit:应用程序在SCNView上渲染SpriteKit粒子系统时崩溃,当所有代码似乎都是系统代码的一部分时如何调试

    在SCNView的overlaySKScene属性中运行SpriteKit粒子系统会导致应用程序崩溃,并显示下面的堆栈跟踪.基于堆栈跟踪,似乎所有系统代码都在运行,所以你应该如何调试崩溃,更重要的是,确定它是否是SceneKit/SpriteKit的错误或应用程序中的错误?

  10. 什么是Swift3中的种子随机数(Xcode8 beta 1)

    我需要在每次执行应用程序时启动相同的随机数列表.srand/rand不再存在了.那我该怎么办?您可以使用Swift3中的srand48(种子)和drand48().

随机推荐

  1. 【cocos2d-x 3.x 学习笔记】对象内存管理

    Cocos2d-x的内存管理cocos2d-x中使用的是上面的引用计数来管理内存,但是又增加了一些自己的特色。cocos2d-x中通过Ref类来实现引用计数,所有需要实现内存自动回收的类都应该继承自Ref类。下面是Ref类的定义:在cocos2d-x中创建对象通常有两种方式:这两中方式的差异可以参见我另一篇博文“对象创建方式讨论”。在cocos2d-x中提倡使用第二种方式,为了避免误用第一种方式,一般将构造函数设为protected或private。参考资料:[1]cocos2d-x高级开发教程2.3节[

  2. 利用cocos2dx 3.2开发消灭星星六如何在cocos2dx中显示中文

    由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面Chinese.h这里的tex_vec是cocos2dx提供的一个保存文档内容的一个容器。这里给出ChineseWords,xml的格式再看看ChineseWord的实现Chinese.cpp就这样,以后在需要用到中文字的地方,就先include这个头文件然后调用ChineseWord函数,获取一串中文字符串。

  3. 利用cocos2dx 3.2开发消灭星星七关于星星的算法

    在前面,我们已经在GameLayer中利用随机数初始化了一个StarMatrix,如果还不知道怎么创建星星矩阵请回去看看而且我们也讲了整个游戏的触摸事件的派发了。

  4. cocos2dx3.x 新手打包APK注意事项!

    这个在编译的时候就可以发现了比较好弄这只是我遇到的,其他的以后遇到再补充吧。。。以前被这两个问题坑了好久

  5. 利用cocos2dx 3.2开发消灭星星八游戏的结束判断与数据控制

    如果你看完之前的,那么你基本已经拥有一个消灭星星游戏的雏形。开始把剩下的两两互不相连的星星消去。那么如何判断是GameOver还是进入下一关呢。。其实游戏数据贯穿整个游戏,包括星星消除的时候要加到获得分数上,消去剩下两两不相连的星星的时候的加分政策等,因此如果前面没有做这一块的,最好回去搞一搞。

  6. 利用cocos2dx 3.2开发消灭星星九为游戏添加一些特效

    needClear是一个flag,当游戏判断不能再继续后,这个flag变为true,开始消除剩下的星星clearSumTime是一个累加器ONE_CLEAR_TIME就是每颗星星消除的时间2.连击加分信息一般消除一次星星都会有连击信息和加多少分的信息。其实这些combo标签就是一张图片,也是通过控制其属性或者runAction来实现。源码ComboEffect.hComboEffect.cpp4.消除星星粒子效果消除星星时,为了实现星星爆裂散落的效果,使用了cocos2d提供的粒子特效引擎对于粒子特效不了

  7. 02 Cocos2D-x引擎win7环境搭建及创建项目

    官网有搭建的文章,直接转载记录。环境搭建:本文介绍如何搭建Cocos2d-x3.2版本的开发环境。项目创建:一、通过命令创建项目前面搭建好环境后,怎样创建自己的Cocos2d-x项目呢?先来看看Cocos2d-x3.2的目录吧这就是Cocos2d-x3.2的目录。输入cocosnew项目名–p包名–lcpp–d路径回车就创建成功了例如:成功后,找到这个项目打开proj.win32目录下的Hello.slnF5成功了。

  8. 利用cocos2dx 3.2开发消灭星星十为游戏添加音效项目源码分享

    一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。Audio.hAudio.cpp好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。最后附上整个项目的源代码点击打开链接

  9. 03 Helloworld

    程序都有一个入口点,在C++就是main函数了,打开main.cpp,代码如下:123456789101112131415161718#include"main.h"#include"AppDelegate.h"#include"cocos2d.h"USING_NS_CC;intAPIENTRY_tWinMain{UNREFERENCED_ParaMETER;UNREFERENCED_ParaMETER;//createtheapplicationinstanceAppDelegateapp;return

  10. MenuItemImage*图标菜单创建注意事项

    学习cocos2dx,看的是cocos2d-x3.x手游开发实例详解,这本书错误一大把,本着探索求知勇于发现错误改正错误的精神,我跟着书上的例子一起调试,当学习到场景切换这个小节的时候,出了个错误,卡了我好几个小时。

返回
顶部