入门小游戏

来源:互联网 发布:js股票实时数据获取 编辑:程序博客网 时间:2024/05/22 01:51

从图书馆借了本木头的书籍,以下实例来自书籍,写在这里仅作笔记。。


游戏内容大概如下:

1.一个Sprite在地图上一直在跑,Sprite可以跳跃(其实是地图不断向左滚动)

2.途中有金币,Sprite吃金币,左上方的Score会++,并且会有+15的字样出现



1.创建实体类Entity,这是一个基类,主要用来绑定一个精灵,返回一个精灵

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef Entity_H
#define Entity_H
 
#include"cocos2d.h"
using namespace cocos2d;
 
classEntity:publicCCNode
{
public:
    Entity();
 
    ~Entity();
 
    CCSprite* getSprite();
 
    voidbindSprite(CCSprite* sprite);
 
private:
    CCSprite* m_sprite;
};
#endif

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include"Entity.h"
 
Entity::Entity()
{
    m_sprite=NULL;
}
Entity::~Entity()
{
 
};
 
CCSprite* Entity::getSprite()
{
    returnthis->m_sprite;
}
voidEntity::bindSprite(CCSprite* sprite){
    this->m_sprite=sprite;
    this->addChild(m_sprite);
}



2.创建一个玩家主角类,可以jump,吃金币等

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef _Player_H
#define _Player_H
 
#include"cocos2d.h"
#include"Entity.h"
 
 
using namespace cocos2d;
 
#define JUMP_ACTION_TAG 1;
 
classPlayer:publicEntity
{
public:
    Player();
    ~Player();
    CREATE_FUNC(Player);
    virtual bool init();
public:
    voidjump();
    voidjumpEnd();
    voidhit();//主角和怪物撞击(玩家受伤害)
    intgetMoney();
    CCRect getBoundingBox();/*获取碰撞范围*/
    voidresetData();
    voidactionEnd();
private:
    bool m_isJumping;
    intm_money;/*金钱*/
 
};
#endif
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include"Player.h"
#include"FlowWord.h"
 
Player::Player()
{
    m_isJumping=false;
    m_money=0;/*初始金钱为0*/
}
Player::~Player()
{
 
}
bool Player::init()
{
    returntrue;
}
 
voidPlayer::jump()
{
    if(!getSprite())
    {
        return;
    }
    if(m_isJumping)/*如果主角还在跳跃中,则不重复跳跃*/
    {
        return;
    }
 
    m_isJumping=true;
 
    /*创建动作,2s,原地跳跃(即落地的地点相对于起跳地点x偏移0,y偏移0),高度250,弹跳次数1*/
    CCJumpBy* jump=CCJumpBy::create(1.5f,ccp(0,0),200,1);
    /*callFunc也是一个动作,作用是调用一个函数*/
    CCCallFunc* callFunc = CCCallFunc::create(this, callfunc_selector(Player::jumpEnd));
    /*组合动作*/
    CCActionInterval* jumpActions=CCSequence::create(jump,callFunc,NULL);
     
    runAction(jumpActions);
}
 
voidPlayer::jumpEnd()
{
    m_isJumping=false;
}
voidPlayer::hit()
{
    if(getSprite()==NULL)
    {
        return;
    }
    /*加钱特效提示*/
    FlowWord* flowword=FlowWord::create();
    this->addChild(flowword);
    flowword->showWord("+15",getSprite()->getPosition());
     
    m_money+=15;
 
    /*创建4种动作对象*/
    CCMoveBy* backMove=CCMoveBy::create(0.1f,ccp(-20,0));
    CCMoveBy* forwardMove=CCMoveBy::create(0.1f,ccp(20,0));
    CCRotateBy* backRotate=CCRotateBy::create(0.1f,-5,0);
    CCRotateBy* forwardRotate=CCRotateBy::create(0.1f,5,0);
 
    /*分别组合成两种动作(同时执行)*/
    CCSpawn* backActions=CCSpawn::create(backMove,backRotate,NULL);
    CCSpawn* forwardActions=CCSpawn::create(forwardMove,forwardRotate,NULL);
 
    CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(Player::actionEnd));
 
    CCActionInterval* actions=CCSequence::create(backActions,forwardActions,callFunc,NULL);
     
    //this->stopAllActions();
    //resetData();
 
    this->runAction(actions);
 
 
}
voidPlayer::resetData()
{
    if(m_isJumping)
    {
        m_isJumping=false;
    }
    this->setPosition(ccp(200,500/4));
    this->setScale(1.0f);
    setRotation(0);
}
intPlayer::getMoney()
{
    returnm_money;
}
CCRect Player::getBoundingBox()
{
    if(getSprite()==NULL){
        returnCCRectMake(0,0,0,0);
    }
    /*由于Sprite是放在Player上的,所以要检测Player的碰撞范围*/
    CCSize spriteSize=getSprite()->getContentSize();
    CCPoint entityPos=this->getPosition();//获取player中心点
 
    //获取Player左下角的坐标值
    intx=entityPos.x-spriteSize.width/2;
    inty=entityPos.y-spriteSize.height/2;
 
    returnCCRectMake(x,y,spriteSize.width,spriteSize.height);
}
 
voidPlayer::actionEnd()
{
    this->setScale(1.0f);
    setRotation(0);
}




3.创建怪物类(金币),继承于实体类
--

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef _Monster_H_
#define _Monster_H_
 
#include"Entity.h"
#include"cocos2d.h"
#include"Player.h"
#include"ccMacros.h"
 
USING_NS_CC;
 
classMonster:publicEntity
{
public:
    Monster();
    ~Monster();
    CREATE_FUNC(Monster);
    virtual bool init();
     
public:
    voidshow();
    voidhide();
    voidreset();//重置怪物数据
    bool isAlive();//是否活动状态
 
    bool isCollideWithPlayer(Player* player);//检测是否碰撞
private:
    bool m_isAlive;
};
#endif
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include"Monster.h"
 
Monster::Monster()
{
}
Monster::~Monster()
{
}
bool Monster::init()
{
    returntrue;
}
voidMonster::show()
{
    if(getSprite()!=NULL)
    {
        this->setVisible(true);
        m_isAlive=true;/*标记为活动状态*/
    }
}
voidMonster::hide()
{
    if(getSprite()!=NULL)
    {
        this->setVisible(false);
        reset();
        m_isAlive=false;/*标记为活动状态*/
    }
}
voidMonster::reset()
{
    if(getSprite()!=NULL)
    {
        /*初始化怪物坐标,宽度(800-2800),高度(100-200)*/
        this->setPosition(ccp(800+CCRANDOM_0_1()*2000,200-CCRANDOM_0_1()*100));
    }
}
bool Monster::isAlive()
{
    returnm_isAlive;
}
bool Monster::isCollideWithPlayer(Player* player)
{
    CCRect playerRect=player->getBoundingBox();
    CCPoint monsterPos=getPosition();
 
    /*判断是否有交集*/
    returnplayerRect.containsPoint(monsterPos);
}



4.创建怪物管理器类(MonsterManger),用来管理怪物的显示和隐藏


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef __MonsterManger_H__
#define __MonsterManger_H__
 
#include"cocos2d.h"
#include"Player.h"
USING_NS_CC;
 
#define MAX_MONSTER_NUM 10
 
classMonsterManger:publicCCNode
{
public:
    MonsterManger();
    ~MonsterManger();
    CREATE_FUNC(MonsterManger);
    virtual bool init();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        virtual voidupdate(floatdt);/*重写update函数*/
    voidbindPlayer(Player* player);
private:
    voidcreateMonsters();/*创建Monster对象*/
private:
    CCArray* m_monsterArr;/*存放怪物数组*/
    Player* m_player;
};
 
#endif

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include"MonsterManger.h"
#include"Monster.h"
 
MonsterManger::MonsterManger(){}
 
MonsterManger::~MonsterManger(){}
 
bool MonsterManger::init()
{
    bool bRet=false;
    do
    {
        createMonsters();/*创建怪物*/
        this->scheduleUpdate();/*启用update*/
        bRet=true;
    }while(0);
    returnbRet;
     
}
 
 
voidMonsterManger::createMonsters()
{
    m_monsterArr=CCArray::create();
    m_monsterArr->retain();/*防止数组被释放*/
 
    Monster* monster=NULL;
    CCSprite* sprite=NULL;
 
    for(inti=0;i<max_monster_num; i++)=""{=""monster="Monster::create();"monster-="">bindSprite(CCSprite::create("monster.png"));
        monster->reset();
 
        this->addChild(monster); /*将怪物添加到管理器(CCNode)中*/
        m_monsterArr->addObject(monster);/*添加到数组中,便于管理*/
    }
}
 
voidMonsterManger::update(floatdt)
{
    CCObject* obj=NULL;
    Monster* monster=NULL;
     
    CCARRAY_FOREACH(m_monsterArr,obj)/*循环遍历怪物数组,重复出现在屏幕上*/
    {
        monster=(Monster*) obj;
        if(monster->isAlive())/*活动状态*/
        {
            monster->setPositionX(monster->getPositionX()-3);//左移
            if(monster->getPositionX()<0)
            {
                monster->hide();
 
            }elseif(monster->isCollideWithPlayer(m_player)){
                m_player->hit();
                monster->hide();
            }
         
        }else/*非活动状态*/
        {
            monster->show();//
        }
    }
}
voidMonsterManger::bindPlayer(Player* player)
{
    this->m_player=player;
    this->m_player->retain();//引用计数 +1
}</max_monster_num;>



5.创建文字飘动效果,(在主角身上显示"+15"特效)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef __FlowWord_H__
#define __FlowWord_H__
 
#include"cocos2d.h"
USING_NS_CC;
 
classFlowWord:publicCCNode
{
public:
    FlowWord();
    ~FlowWord();
    CREATE_FUNC(FlowWord);
    virtual bool init();
public:
    voidshowWord(constchar* text, CCPoint pos);
    voidflowEnd();
private:
    CCLabelTTF* m_textLab;
};
 
#endif


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include"FlowWord.h"
 
FlowWord::FlowWord(){}
 
FlowWord::~FlowWord(){}
 
bool FlowWord::init()
{
    m_textLab=CCLabelTTF::create("","Arial",30);
    m_textLab->setColor(ccc3(255,0,0));
    m_textLab->setVisible(false);
 
    this->addChild(m_textLab);
 
    returntrue;
}
 
voidFlowWord::showWord(constchar* str,CCPoint pos)
{
    m_textLab->setString(str);
    m_textLab->setPosition(pos);
    m_textLab->setAnchorPoint(ccp(1,0));
    m_textLab->setVisible(true);
 
    //先放大后缩小
    CCActionInterval* scaleLarge=CCScaleTo::create(0.3f,2.5f,2.5f);
    CCActionInterval* scaleSmall=CCScaleTo::create(0.4f,0.5f,0.5f);
    //回调动作,移除效果
    CCCallFunc* callFunc=CCCallFunc::create(this,callfunc_selector(FlowWord::flowEnd));
 
    CCActionInterval* actions=CCSequence::create(scaleLarge,scaleSmall,callFunc,NULL);
 
    m_textLab->runAction(actions);
}
 
voidFlowWord::flowEnd()
{
    m_textLab->setVisible(false);
    /*true: 从父节点移除,并移除节点的动作和回调函数*/
    m_textLab->removeFromParentAndCleanup(true);
}

6.创建游戏场景类,所有游戏的效果都在这上面展示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef __TollgateScene_H__
#define __TollgateScene_H__
 
#include"cocos2d.h"
#include"cocos-ext.h"
#include"Player.h"
using namespace cocos2d;
using namespace cocos2d::extension;
 
 
classTollgateScene : publicCCLayer {
public:
    staticCCScene* scene();
    virtual bool init();
    CREATE_FUNC(TollgateScene);
 
    virtualvoidupdate(floatdelta);
 
private:
    voidinitBG();  /* 初始化关卡背景 */
    voidcreateJumpBtn();/*创建跳跃按钮*/
    voidjumpEvent(CCObject* pSender,CCControlEvent event);/*响应按钮点击事件*/
 
    voidcreateScoreLab();/*创建分数标签*/
    voidcreateTimeSlider();/*创建时间条*/
private:
    CCSprite* m_bgSprite1;
    CCSprite* m_bgSprite2;
 
    Player* m_player;
 
    CCLabelTTF* m_scoreLab;//分数标签
    CCControlSlider* m_TimeSlider;//时间条
 
    intm_score;    /*得分*/
    intm_curTime; /*当前时间*/
};
 
#endif

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include"TollgateScene.h"
#include"MonsterManger.h"
 
CCScene* TollgateScene::scene() {
    CCScene* scene = NULL;
    do
    {
        scene = CCScene::create();
        CC_BREAK_IF(!scene);
 
        TollgateScene* layer = TollgateScene::create();
        CC_BREAK_IF(!layer);
 
        scene->addChild(layer,1);
    }while(0);
 
    returnscene;
}
 
bool TollgateScene::init() {
    bool bRet = false;
 
    do
    {
        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
 
        /*游戏标题图片*/
//      CCSprite* titleSprite = CCSprite::create("title.png");
//      titleSprite->setPosition(ccp(visibleSize.width / 2, visibleSize.height - 50));
//      this->addChild(titleSprite, 2);
 
        /*创建猪脚*/
        CCSprite* sprite = CCSprite::create("sprite.png");
        //sprite->setFlipX(true);
        m_player = Player::create();
        m_player->bindSprite(sprite);
        m_player->setPosition(ccp(200, visibleSize.height / 4));
         
        this->addChild(m_player,1);
         
        /*初始化背景图片*/
        initBG();
 
        /*创建按钮*/
        createJumpBtn();
 
        /*设置启用CCNode的update()函数,游戏会在每一帧调用update()函数*/
        this->scheduleUpdate();
 
        /*创建怪物管理器(管理器里面放了很多怪物)*/
        MonsterManger* monsterManger=MonsterManger::create();
        monsterManger->bindPlayer(m_player);
        this->addChild(monsterManger,4);
 
        /*创建分数标签*/
        createScoreLab();
        /*创建时间条*/
        createTimeSlider();
 
        bRet = true;
    }while(0);
 
    returnbRet;
}
 
voidTollgateScene::initBG() {
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
 
    m_bgSprite1 = CCSprite::create("tollgateBG.jpg");
    m_bgSprite1->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2));
    this->addChild(m_bgSprite1,0);
 
    m_bgSprite2 = CCSprite::create("tollgateBG.jpg");
    m_bgSprite2->setPosition(ccp(visibleSize.width + visibleSize.width / 2, visibleSize.height / 2));
    m_bgSprite2->setFlipX(true);
    this->addChild(m_bgSprite2,0);
}
 
voidTollgateScene::update(floatdelta)
{
    CCSize mapSize=m_bgSprite1->getContentSize();//地图大小
 
    intposX1=m_bgSprite1->getPositionX();  //地图1的x坐标
    intposX2=m_bgSprite2->getPositionX();   //地图2的x坐标
 
    intiSpeed = 2;//地图滚动的速度
 
    posX1-=iSpeed; //两张地图一起向左滚动
    posX2-=iSpeed;
 
    //创建无限循环
    if(posX1<=-mapSize.width/2)
    {
        posX1=mapSize.width+mapSize.width/2;
        posX2=mapSize.width/2;
    }
    if(posX2<=-mapSize.width/2)
    {
        posX1=mapSize.width/2;
        posX2=mapSize.width+mapSize.width/2;
    }
 
    m_bgSprite1->setPositionX(posX1);
    m_bgSprite2->setPositionX(posX2);
 
    /*分数增加*/
    m_score=m_player->getMoney();
    m_scoreLab->setString(CCString::createWithFormat("Score:%d",m_score)->getCString());
 
    m_TimeSlider->setValue(--m_curTime);
     
}
voidTollgateScene::createJumpBtn()
{
    CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize();
 
    /*按钮标题*/
    CCLabelTTF* jumpLabel=CCLabelTTF::create("Jump","Arial",35);
 
    /*按钮状态图片*/
    CCScale9Sprite* jumpNorBG=CCScale9Sprite::create("button.png");
    CCScale9Sprite* jumpLightBG=CCScale9Sprite::create("buttonHighlighted.png");
     
    /*创建按钮*/
    CCControlButton* jumpBtn=CCControlButton::create(jumpLabel,jumpNorBG);
    jumpBtn->setPosition(ccp(visibleSize.width-80,50));
    jumpBtn->setBackgroundSpriteForState(jumpLightBG,CCControlStateHighlighted);
     
    /*添加事件*/
    jumpBtn->addTargetWithActionForControlEvents(this,
        cccontrol_selector(TollgateScene::jumpEvent),
        CCControlEventTouchDown);
 
    this->addChild(jumpBtn);
}
 
voidTollgateScene::jumpEvent(CCObject* pSender,CCControlEvent event)
{
    m_player->jump();
}
 
voidTollgateScene::createScoreLab()
{
    m_score=m_player->getMoney();
    CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize();
    m_scoreLab=CCLabelTTF::create("Score:"+m_score,"Arial",35);
    m_scoreLab->setAnchorPoint(ccp(0,1));
    m_scoreLab->setPosition(ccp(0,visibleSize.height));
    this->addChild(m_scoreLab);
 
}
 
voidTollgateScene::createTimeSlider()
{
    CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize();
    m_curTime=10000;
    m_TimeSlider=CCControlSlider::create(
        CCSprite::create("background.png"),
        CCSprite::create("progress.png"),
        CCSprite::create("sliderThumb.png")
        );
    m_TimeSlider->setPosition(ccp(
        m_TimeSlider->getContentSize().width/2,
        visibleSize.height-m_TimeSlider->getContentSize().height-m_scoreLab->getContentSize().height
        ));
    m_TimeSlider->setTouchEnabled(false);
    m_TimeSlider->setMaximumValue(10000);
    m_TimeSlider->setMinimumValue(0);
    m_TimeSlider->setValue(m_curTime);
    this->addChild(m_TimeSlider,3);
}



运行效果如下

<img src="http://www.2cto.com/uploadfile/Collfiles/20140318/201403180932002.jpg" alt="n块ズ膻歓喎�" http:="" www.2cto.com="" ym"="" target="_blank" class="keylink" style="border-width: 0px; padding: 0px; margin: 0px; list-style: none; width: 630px; height: 406.40099626401px;">源码

0 0
原创粉丝点击