先下个效果图:
这个是从网上下载的例子,用来学习的。。。
用到的类有上面这些:接下来我就把它用的好的地方给记录下来。
1.HelloWorld类:
先预加载资源。
void HelloWorld::PreloadMusicAndPicture()
{
@H_403_25@//png加入全局cache中 plist存储了
@H_403_25@SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot_background.plist");
@H_403_25@SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/shoot.plist");
@H_403_25@// 音效
@H_403_25@CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/background-music1.mp3");
}
auto copyRight = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("shoot_copyright.png"));
播放动画:
// Animation是由许多精灵帧组成,可以设置间隔时间,持续时间等,它实际上是包含着一组数据
@H_403_25@Animation* animation=Animation::create();
@H_403_25@animation->setDelayPerUnit(0.2f); // 间隔时间
@H_403_25@animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading1.png"));
@H_403_25@animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading2.png"));
@H_403_25@animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading3.png"));
@H_403_25@animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("game_loading4.png"));
@H_403_25@// 通过帧数据创建帧动作(创建序列帧动画)
@H_403_25@Animate* animate=Animate::create(animation);
@H_403_25@Repeat* repeat=Repeat::create(animate,3); // 重复一个动作的次数
@H_403_25@CallFuncN* repeatdone=CallFuncN::create(CC_CALLBACK_1(HelloWorld::loadingDone,this)); // 创建回调函数 CC_CALLBACK_1 代表一个参数
@H_403_25@Sequence* sequence=Sequence::create(repeat,repeatdone,NULL);// 让多个动作按照前后顺序逐一执行 repeatdone 放在 repeat前的话,就不会播放执行3次序列帧的动画
@H_403_25@loading->runAction(sequence); // 执行上述动画
回调:注意它这里带了个Node参数,这样可以方便的获得对象。
void HelloWorld::loadingDone( Node* pNode )
{
@H_403_25@auto scene = GameLayer::createScene();
@H_403_25@TransitionCrossFade *pAnimateScene = TransitionCrossFade::create(1,scene);
@H_403_25@Director::getInstance()->replaceScene(pAnimateScene);
}
@H_403_25@// 背景无限滚动:
@H_403_25@auto backgroundA = Sprite::create("ui/shoot_background/background.png");
@H_403_25@backgroundA->setTag(e_BackgroundA);
@H_403_25@backgroundA->setAnchorPoint(Point::ZERO);
@H_403_25@backgroundA->setPosition(Point::ZERO);
@H_403_25@this->addChild(backgroundA);
@H_403_25@auto backgroundB = Sprite::create("ui/shoot_background/background.png");
@H_403_25@backgroundB->setTag(e_BackgroundB);
@H_403_25@backgroundB->setAnchorPoint(Point::ZERO);
@H_403_25@backgroundB->setPosition(Point::ZERO);
@H_403_25@this->addChild(backgroundB);
@H_403_25@// 每帧都调用的函数
@H_403_25@this->schedule(schedule_selector(GameLayer::backgroundMove));
void GameLayer::backgroundMove(float dt)
{
@H_403_25@Sprite *pBackgroundA = (Sprite*)this->getChildByTag(EnBackground::e_BackgroundA);
@H_403_25@Sprite *pBackgroundB = (Sprite*)this->getChildByTag(EnBackground::e_BackgroundB);
@H_403_25@pBackgroundA->setPositionY(pBackgroundA->getPositionY() - 2);
@H_403_25@pBackgroundB->setPositionY(pBackgroundA->getPositionY() + pBackgroundA->getContentSize().height);
@H_403_25@if (0 == pBackgroundB->getPositionY())
@H_403_25@{
@H_403_25@pBackgroundA->setPositionY(0);
@H_403_25@}
}
4.创建精灵,并且触摸移动不出屏幕。
auto sprite = Sprite::create("ui/shoot/hero1.png");
@H_403_25@sprite->setPosition(Point(winSize.width/2,sprite->getContentSize().height/2));
@H_403_25@sprite->setTag(AIRPLANE);
@H_403_25@this->addChild(sprite);
@H_403_25@// 我机触摸
@H_403_25@auto listener = EventListenerTouchOneByOne::create();
@H_403_25@listener->setSwallowTouches(true);
@H_403_25@listener->onTouchBegan = [](Touch* touch,Event *event){
@H_403_25@auto target = static_cast<Sprite*>(event->getCurrentTarget());
@H_403_25@Point locationInNode = target->convertToNodeSpace(touch->getLocation());
@H_403_25@Size s = target->getContentSize();
@H_403_25@Rect rect = Rect(0,s.width,s.height);
@H_403_25@if (rect.containsPoint(locationInNode))
@H_403_25@{
@H_403_25@return true;
@H_403_25@}
@H_403_25@else
@H_403_25@{
@H_403_25@return false;
@H_403_25@}
@H_403_25@};
@H_403_25@listener->onTouchMoved =[](Touch* touch,Event *event){
@H_403_25@auto target = static_cast<Sprite*>(event->getCurrentTarget());
@H_403_25@target->setPosition(target->getPosition() + touch->getDelta());
@H_403_25@};
@H_403_25@listener->onTouchEnded = [=](Touch* touch,Event* event){
@H_403_25@};
@H_403_25@//将触摸监听添加到eventDispacher中去
@H_403_25@_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,sprite);
@H_403_25@// 每帧都调用的函数
@H_403_25@this->schedule(schedule_selector(PlaneLayer::checkBorder));
@H_403_25@return true;
}
void PlaneLayer::checkBorder( float dt )
{
@H_403_25@//进行边界判断,不可超出屏幕 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); }