先下个效果图:
这个是从网上下载的例子,用来学习的。。。
用到的类有上面这些:接下来我就把它用的好的地方给记录下来。
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,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); } (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|