cocos3.2学习微信打飞机
来源:互联网 发布:杭州电信网络测速 编辑:程序博客网 时间:2024/05/06 00:23
先下个效果图:
这个是从网上下载的例子,用来学习的。。。
用到的类有上面这些:接下来我就把它用的好的地方给记录下来。
1.HelloWorld类:
先预加载资源。
void HelloWorld::PreloadMusicAndPicture()
{
//png加入全局cache中 plist存储了
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot_background.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot.plist");
// 音效
CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/background-music1.mp3");
}
auto copyRight = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("shoot_copyright.png"));
播放动画:
// Animation是由许多精灵帧组成,可以设置间隔时间,持续时间等,它实际上是包含着一组数据
Animation* animation=Animation::create();
animation->setDelayPerUnit(0.2f); // 间隔时间
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading1.png"));
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading2.png"));
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading3.png"));
animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading4.png"));
// 通过帧数据创建帧动作(创建序列帧动画)
Animate* animate=Animate::create(animation);
Repeat* repeat=Repeat::create(animate,3); // 重复一个动作的次数
CallFuncN* repeatdone=CallFuncN::create(CC_CALLBACK_1(HelloWorld::loadingDone, this)); // 创建回调函数 CC_CALLBACK_1 代表一个参数
Sequence* sequence=Sequence::create(repeat, repeatdone, NULL);// 让多个动作按照前后顺序逐一执行 repeatdone 放在 repeat前的话,就不会播放执行3次序列帧的动画
loading->runAction(sequence); // 执行上述动画
回调:注意它这里带了个Node参数,这样可以方便的获得对象。
void HelloWorld::loadingDone( Node* pNode )
{
auto scene = GameLayer::createScene();
TransitionCrossFade *pAnimateScene = TransitionCrossFade::create(1, scene);
Director::getInstance()->replaceScene(pAnimateScene);
}
// 背景无限滚动:
auto backgroundA = Sprite::create("ui/shoot_background/background.png");
backgroundA->setTag(e_BackgroundA);
backgroundA->setAnchorPoint(Point::ZERO);
backgroundA->setPosition(Point::ZERO);
this->addChild(backgroundA);
auto backgroundB = Sprite::create("ui/shoot_background/background.png");
backgroundB->setTag(e_BackgroundB);
backgroundB->setAnchorPoint(Point::ZERO);
backgroundB->setPosition(Point::ZERO);
this->addChild(backgroundB);
// 每帧都调用的函数
this->schedule(schedule_selector(GameLayer::backgroundMove));
void GameLayer::backgroundMove(float dt)
{
Sprite *pBackgroundA = (Sprite*)this->getChildByTag(EnBackground::e_BackgroundA);
Sprite *pBackgroundB = (Sprite*)this->getChildByTag(EnBackground::e_BackgroundB);
pBackgroundA->setPositionY(pBackgroundA->getPositionY() - 2);
pBackgroundB->setPositionY(pBackgroundA->getPositionY() + pBackgroundA->getContentSize().height);
if (0 == pBackgroundB->getPositionY())
{
pBackgroundA->setPositionY(0);
}
}
4.创建精灵,并且触摸移动不出屏幕。
auto sprite = Sprite::create("ui/shoot/hero1.png");
sprite->setPosition(Point(winSize.width/2,sprite->getContentSize().height/2));
sprite->setTag(AIRPLANE);
this->addChild(sprite);
// 我机触摸
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event *event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0,0,s.width,s.height);
if (rect.containsPoint(locationInNode))
{
return true;
}
else
{
return false;
}
};
listener->onTouchMoved =[](Touch* touch, Event *event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
};
listener->onTouchEnded = [=](Touch* touch, Event* event){
};
//将触摸监听添加到eventDispacher中去
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite);
// 每帧都调用的函数
this->schedule(schedule_selector(PlaneLayer::checkBorder));
return true;
}
void PlaneLayer::checkBorder( float dt )
{
//进行边界判断,不可超出屏幕
Point location = this->getChildByTag(AIRPLANE)->getPosition();
Size winSize=Director::sharedDirector()->getWinSize(); // 获取opengl视图窗口大小
Size planeSize=this->getChildByTag(AIRPLANE)->getContentSize(); // 返回的就是这个矩形的大小,只是是逻辑尺寸, 而不是像素的
if (location.x<planeSize.width/2)
{
location.x=planeSize.width/2;
}
if (location.x>winSize.width-planeSize.width/2)
{
location.x=winSize.width-planeSize.width/2;
}
if (location.y<planeSize.height/2)
{
location.y=planeSize.height/2;
}
if (location.y>winSize.height-planeSize.height/2)
{
location.y=winSize.height-planeSize.height/2;
}
this->getChildByTag(AIRPLANE)->setPosition(location);
}
- cocos3.2学习微信打飞机
- cocos2d-x的学习模仿微信打飞机
- Cocos3.0学习笔记-Acition-基本动作
- :微信打飞机
- 微信打飞机
- cocos3.2精灵跟随触点移动
- cocos3.9 【 Sublime Text 2 配置 】
- Crossin先生的微信打飞机游戏(2)
- 学习记录:cocos 2d-x 飞机游戏开发
- COCOS学习笔记--Cocos3.x引擎开发环境搭建
- 27讲学习,飞机彩带
- 飞机游戏源码学习小结
- 打飞机代码学习感想
- 学习飞机游戏的心得
- 【Python学习】飞机大战项目
- 微信打飞机--Java版
- 微信打飞机java代码
- 游戏‘微信打飞机’
- VS2010 颜色选择对话框
- 字符数组与字符串的转换
- mysql的一些小技巧
- jni快速入门
- 【创业说】零经验接手APP运营推广,聊聊这两个月我是怎么熬过来的
- cocos3.2学习微信打飞机
- 操作系统学习:线程 与 进程
- 杂店记录_002:c++中%s与string对象匹配
- 数据库操作案例
- 一道概率题(笔试)
- JAVA 加密算法简介
- MyEclipse重装后,如何使用原来装好的Adobe Flex Builder 4
- yum提示another app is currently holding the yum lock;waiting for it to exit
- linux设置语言环境