先下个效果图:
这个是从网上下载的例子,用来学习的。。。
用到的类有上面这些:接下来我就把它用的好的地方给记录下来。
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); }
以上是内存溢出为你收集整理的cocos3.2学习微信打飞机全部内容,希望文章能够帮你解决cocos3.2学习微信打飞机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)