展会信息港展会大全

最新cocos2d-x 3.0博客教学 小游戏[史上最坑爹的游戏] 002第一关:关闭卡死的电脑
来源:互联网   发布日期:2015-09-26 21:16:13   浏览:2029次  

导读:昨天做了主画面的选择,今天做第一关的关闭卡死的电脑,这个小游戏,这个游戏是很多的小游戏组合起来的,所以我们逐个击破,第一个游戏到底要如何做呢?看看今天实现第一关的效果这个第一...

昨天做了主画面的选择,今天做第一关的关闭卡死的电脑,这个小游戏,这个游戏是很多的小游戏组合起来的,所以我们逐个击破,第一个游戏到底要如何做呢?

看看今天实现第一关的效果

这个第一个游戏不算太难

首先我做了一个工具类,这个类实现了精灵的动画播放

SWSpritePlayForTexturePacker.h 和 SWSpritePlayForTexturePacker.cpp

SWSpritePlayForTexturePacker.h

//

//SWSpritePlayForTexturePacker.h

//Holdtail

//精灵播放动画,让精灵动起来

//Created by 帅 印 on 13-8-17.

//

//

#ifndef __Holdtail__SWSpritePlayForTexturePacker__

#define __Holdtail__SWSpritePlayForTexturePacker__

#include

#include cocos2d.h

using namespace cocos2d;

class SWSpritePlayForTexturePacker:public cocos2d::Sprite{

public:

//实例化传入图片名称

static SWSpritePlayForTexturePacker *createPlay(const char *fileName,int startCount,int allCount,float sprit,bool isrun);

private:

//初始化

void playInit(const char *fileName,int startCount,int allCount,float sprit,bool isrun);

//精灵的动态表现文件名称 文件开始位置 文件帧数精灵速度是否循环

void createAnimate(const char *fileName,int startCount,int allCount,float sprit,bool isrun);

};

#endif /* defined(__Holdtail__SWSpritePlayForTexturePacker__) */

SWSpritePlayForTexturePacker.cpp

//

//SWSpritePlayForTexturePacker.cpp

//Holdtail

//

//Created by 帅 印 on 13-8-17.

//

//

#include SWSpritePlayForTexturePacker.h

#include cocos2d.h

using namespace cocos2d;

SWSpritePlayForTexturePacker *SWSpritePlayForTexturePacker::createPlay(const char *fileName,int startCount,int allCount,float sprit,bool isrun){

SWSpritePlayForTexturePacker *spritePlay = new SWSpritePlayForTexturePacker();

if(spritePlay && spritePlay->initWithFile(CloseSelected.png)){

spritePlay->autorelease();

spritePlay->playInit(fileName,startCount,allCount,sprit,isrun);

return spritePlay;

}

return NULL;

}

//初始化

void SWSpritePlayForTexturePacker::playInit(const char *fileName,int startCount,int allCount,float sprit,bool isrun){

//创建动物的动画

createAnimate(fileName,startCount,allCount,sprit,isrun);

}

void SWSpritePlayForTexturePacker::createAnimate(const char *fileName,int startCount,int allCount,float sprit,bool isrun){

SpriteFrameCache *cache = SpriteFrameCache::getInstance();//获取内存中的缓冲图片

Array *arrypeople = Array::create();

char str[100]={0};

for (int i = startCount; i<=allCount; i++) {

if (i<10) {

sprintf(str, %s_0%d.png,fileName,i);

}else{

sprintf(str, %s_%d.png,fileName,i);

}

SpriteFrame *spfr = cache->getSpriteFrameByName(str);

arrypeople->addObject(spfr);

}

Animation *animationpeople = Animation::createWithSpriteFrames(arrypeople,sprit);

animationpeople->createWithSpriteFrames(arrypeople);

if (isrun) {

animationpeople->setLoops(-1);

}else{

animationpeople->setLoops(1);

}

//Animate *animatepeople = Animate::create(animationpeople);

//this->runAction(RepeatForever::create(animatepeople->reverse()));

Animate *animate = Animate::create(animationpeople);

this->runAction(animate);

}

//下面就可以做游戏的第一关咯

HistoryGame01.h 和 HistoryGame01.cpp

HistoryGame01.h

#ifndef __HistoryGame01_SCENE_H__

#define __HistoryGame01_SCENE_H__

#include cocos2d.h

USING_NS_CC;

class HistoryGame01 : public cocos2d::Layer

{

public:

static cocos2d::Scene* createScene();

virtual bool init();

//判断精灵是否点击

bool isInSprite(Touch *theTouch,Sprite *sp);

//点击

bool onTouchBegan(Touch *pTouch, Event*pEvent);

//游戏结束

void gameover(float over);

//游戏胜利

void gamewin(float win);

void menuCloseCallback(Object* pSender);

CREATE_FUNC(HistoryGame01);

};

#endif // __HistoryGame01_SCENE_H__

HistoryGame01.cpp

#include HistoryGame01.h

#include SWSpritePlayForTexturePacker.h

#include ScrollViewScene.h

#include CCEventDispatcher.h

#include CCEventListenerTouch.h

USING_NS_CC;

static bool isClickGo;

Scene* HistoryGame01::createScene()

{

isClickGo = true;

auto scene = Scene::create();

auto layer = HistoryGame01::create();

scene->addChild(layer);

return scene;

}

bool HistoryGame01::init()

{

if ( !Layer::init() )

{

return false;

}

//cocos2d-x3.0中的点击事件

auto dispatcher = Director::getInstance()->getEventDispatcher();

auto touchListener = EventListenerTouchOneByOne::create();

touchListener->onTouchBegan = CC_CALLBACK_2(HistoryGame01::onTouchBegan, this);

dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

//加入背景

Size size = CCDirector::getInstance()->getWinSize();

SpriteFrameCache *cache = SpriteFrameCache::getInstance();

cache->addSpriteFramesWithFile(shutdown.plist);

Sprite* sprite =Sprite::createWithSpriteFrameName(background.png);

sprite->setPosition(Point(size.width*0.5,size.height*0.5));

addChild(sprite);

//加入电脑

auto sp_company =Sprite::createWithSpriteFrameName(computer.png);

sp_company->setPosition(Point(size.width*0.5-50,size.height*0.5));

addChild(sp_company);

//加入播放的代码动画

auto sp_codes = SWSpritePlayForTexturePacker::createPlay(code,1, 2, 0.2f,true);

sp_codes->setPosition(Point(size.width*0.5-245,size.height*0.5+68));

sp_codes->setTag(104);

addChild(sp_codes);

//加入电源线

auto sp_tishi =Sprite::createWithSpriteFrameName(wire.png);

sp_tishi->setPosition(Point(size.width*0.5-95,size.height*0.5+20));

addChild(sp_tishi);

//加入电源开关

auto sp_open =Sprite::createWithSpriteFrameName(button.png);

sp_open->setPosition(Point(size.width*0.5-376,size.height*0.5+76));

sp_open->setTag(103);

addChild(sp_open);

//加入文字

auto sp_wenzi1 =Sprite::createWithSpriteFrameName(word_01.png);

sp_wenzi1->setPosition(Point(size.width*0.5,size.height*0.5+200));

addChild(sp_wenzi1);

auto sp_wenzi2 =Sprite::createWithSpriteFrameName(word_02.png);

sp_wenzi2->setPosition(Point(size.width*0.5+180,size.height*0.5+200));

addChild(sp_wenzi2);

auto sp_wenzi3 =Sprite::createWithSpriteFrameName(word_03.png);

sp_wenzi3->setPosition(Point(size.width*0.5+360,size.height*0.5+200));

addChild(sp_wenzi3);

//加入星星播放的代码动画

auto sp_star = SWSpritePlayForTexturePacker::createPlay(star,1, 3, 0.5f,true);

sp_star->setPosition(Point(size.width*0.5-95,size.height*0.5+40));

sp_star->setTag(102);

addChild(sp_star);

//加入人物的代码动画(显示等待)

auto sp_people = SWSpritePlayForTexturePacker::createPlay(hero,1, 4, 0.5f,true);

sp_people->setPosition(Point(size.width*0.5+150,size.height*0.5-20));

sp_people->setTag(101);

addChild(sp_people);

MenuItemImage *closeItem = MenuItemImage::create();

closeItem->setNormalSpriteFrame(cache->spriteFrameByName(back-1.png));

closeItem->setSelectedSpriteFrame(cache->spriteFrameByName(back-2.png));

closeItem->initWithTarget(this, menu_selector(HistoryGame01::menuCloseCallback));

closeItem->setPosition(Point(80,80));

auto menu = Menu::create(closeItem, NULL);

menu->setPosition(Point::ZERO);

this->addChild(menu, 1);

return true;

}

void HistoryGame01::menuCloseCallback(Object* pSender)

{

//实现

CCDirector::getInstance()->replaceScene(CCTransitionFade::create(0.5, ScrollViewScene::create()));

}

bool HistoryGame01::onTouchBegan(Touch *pTouch, Event*pEvent){

if (isInSprite(pTouch, (Sprite *)this->getChildByTag(102))) {

//电线被点击

//隐藏当前播放动画,显示结束动画

Sprite *peo = (Sprite *)getChildByTag(101);

peo->setOpacity(0);

//加入人物的代码动画(显示等待)

Size size = CCDirector::getInstance()->getWinSize();

auto sp_people2 = SWSpritePlayForTexturePacker::createPlay(hero,5, 11, 0.5f,false);

sp_people2->setPosition(Point(size.width*0.5+150,size.height*0.5-20));

addChild(sp_people2);

this->scheduleOnce(schedule_selector(HistoryGame01::gameover), 3.0f);

return false;

}else if(isInSprite(pTouch, (Sprite *)this->getChildByTag(103))){

//按钮被点击

//隐藏当前播放动画,显示结束动画

Sprite *peo = (Sprite *)getChildByTag(104);

peo->setOpacity(0);

//加入人物的代码动画(显示等待)

Size size = CCDirector::getInstance()->getWinSize();

auto sp_people2 = SWSpritePlayForTexturePacker::createPlay(computerclose,1, 5, 0.5f,false);

sp_people2->setPosition(Point(size.width*0.5-245,size.height*0.5+68));

addChild(sp_people2);

this->scheduleOnce(schedule_selector(HistoryGame01::gamewin), 3.0f);

return false;

}

return false;

}

bool HistoryGame01::isInSprite(Touch *theTouch,Sprite *sp){

//返回当前触摸位置得OpenGL坐标

Point touchPoint = theTouch->getLocation();

//将世界坐标转换为当前得父View的本地坐标

Point reallyPoint = this->getParent()->convertToNodeSpace(touchPoint);

//获取当前基于父View的坐标系

Rect rect = sp->boundingBox();

if (rect.containsPoint(reallyPoint)) {

//判断是否让用户再点击

if (isClickGo) {

isClickGo = false;

return true;

}else{

return false;

}

}

return false;

}

void HistoryGame01::gameover(float over){

//游戏失败

Size size = CCDirector::getInstance()->getWinSize();

LayerColor *layer = LayerColor::create(Color4B(0, 0, 0, 200), size.width, size.height);

auto sp = Sprite::createWithSpriteFrameName(bg.png);

sp->setPosition(Point(size.width*0.5, size.height*0.5));

layer->addChild(sp);

LabelTTF *scorettf = LabelTTF::create(失败, MarkerFelt-Thin, 60);

scorettf->setPosition(Point(size.width*0.5, size.height*0.5));

layer->addChild(scorettf);

this->addChild(layer);

}

void HistoryGame01::gamewin(float win){

Size size = CCDirector::getInstance()->getWinSize();

LayerColor *layer = LayerColor::create(Color4B(0, 0, 0, 200), size.width, size.height);

auto sp = Sprite::createWithSpriteFrameName(bg.png);

sp->setPosition(Point(size.width*0.5, size.height*0.5));

layer->addChild(sp);

LabelTTF *scorettf = LabelTTF::create(成功, MarkerFelt-Thin, 60);

scorettf->setPosition(Point(size.width*0.5, size.height*0.5));

layer->addChild(scorettf);

this->addChild(layer);

}

总结一下,做游戏要动用大量的脑力,跟时间,做起来也相当的废力,希望大家一起共同加油噢,今天就把源码偷偷的放出来,给大家参考参考哈。

赞助本站

人工智能实验室

相关热词: cocos2d 游戏开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港