现在的位置: 首页 > 综合 > 正文

用cocos2d-x画类似心电图的代码例子

2013年06月16日 ⁄ 综合 ⁄ 共 2821字 ⁄ 字号 评论关闭

本文代码例子下载:http://pan.baidu.com/share/link?shareid=2911046933&uk=3189484501

代码效果图

直接上代码了Terrain.h

//
//  Terrain.h
//  OCAndC++
//
//  Created by 杜甲 on 13-9-19.
//
//

#ifndef __OCAndC____Terrain__
#define __OCAndC____Terrain__

#include "cocos2d.h"
#define kMaxHillKeyPoints 1000
USING_NS_CC;

class Terrain:public CCNode {
    
public:
    Terrain();
    ~Terrain();
    
    CREATE_FUNC(Terrain);
    CC_SYNTHESIZE_RETAIN(CCSprite*, _stripes, Stripes);
    int _offsetX;
    CCPoint _hillKeyPoints[kMaxHillKeyPoints];
    
    void genrateHills();
    void draw();
    void setOffsetX(float newOffsetX);
    bool init();
    
    
};

#endif /* defined(__OCAndC____Terrain__) */

Terrain.cpp

//
//  Terrain.cpp
//  OCAndC++
//
//  Created by 杜甲 on 13-9-19.
//
//

#include "Terrain.h"

Terrain::Terrain()
{
    _stripes = NULL;
    _offsetX = 0;
}
Terrain::~Terrain()
{
    CC_SAFE_RELEASE_NULL(_stripes);
    
}

bool flag1 = true;
void Terrain::genrateHills()
{
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    float minDX = 1;
    float minDY = 10;
    int rangeDX = 10;
    int rangeDY = 30;
    
    float x = -minDX;
    float y = winSize.height / 2;
    float dy,ny;
    float sign = 1;
    
    float paddingTop = 20;
    float paddingBottom = 20;
    for (int i = 0 ; i < kMaxHillKeyPoints; ++i) {
        _hillKeyPoints[i] = ccp(x, y);
        if (i == 0) {
            x = 0;
            y = winSize.height / 2;
        }
        else
        {
            x += rand() % rangeDX + minDX;
            while (true) {
                
                dy = rand() % rangeDY +minDY;
                ny = winSize.height/ 2 + dy * sign;
                
                    
                    break;
                
            }
            y = ny;
        }
        sign *= -1;
    }
    
    
    
    
}


bool Terrain::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!CCNode::init());
        this->genrateHills();
        bRet = true;
    } while (0);
    return bRet;
}

void Terrain::draw()
{
    CCNode::draw();
    for (int i = 1; i < kMaxHillKeyPoints; ++i) {
        ccDrawLine(_hillKeyPoints[i - 1], _hillKeyPoints[i]);
    }
}

void Terrain::setOffsetX(float newOffsetX)
{
    _offsetX = newOffsetX;
    this->setPosition(ccp(-_offsetX * this->getScale(), 0));
    
}

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Terrain.h"


class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    
    Terrain* _terrain;
    
    void update(float delta);

    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *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 ( !CCLayer::init() )
    {
        return false;
    }
    _terrain = Terrain::create();
    this->addChild(_terrain,1);
    
    this->scheduleUpdate();
  
    
    return true;
}

void HelloWorld::update(float delta)
{
    float PIXELS_PER_SCECOND = 100;
    static float offset = 0;
    offset += PIXELS_PER_SCECOND * delta;
    _terrain->setOffsetX(offset);
    
}

抱歉!评论已关闭.