脱mm内衣游戏,王菲惠,外媒薄
首先创建两个场景,第一个设置为主界面,包含背景,开始游戏和退出的按钮。后一个设置为游戏界面,包含背景,炮台,切换炮台等级按钮,返回按钮。其实后面鱼类的游动,捕鱼网的动画以及子弹都是在游戏界面里面设计。在这里先不讲,只讲场景吧。
添加背景图片:
<span style=“white-space:pre”> </span>auto background = sprite::create(“background.png”); background->setposition(vec2(visiblesize.width / 2, visiblesize.height / 2)); this->addchild(background);
这里的visiblesize就是主场景的规格,创建背景精灵设置坐标后添加上去。对于两个界面都是一样的用法。
然后就是切换场景:
void helloworld::onstart(ref* psender){ auto ss = (menuitemimage*)psender; string str = ss->getname(); //sound->playeffect(“21.mp3”); //切换音效 if (str == “start”){ //切换场景 auto scene = gamescene::createscene(); director::getinstance()->pushscene(scene); } else{ director::getinstance()->end(); } }
<span style=”white-space:pre”> </span>//增加炮台等级的按钮 auto right = menuitemimage::create(“+.png”, “+++.png”, cc_callback_1(gamescene::changebattery, this)); right->setname(“right”); auto menu1 = menu::create(right,nullptr); //减少炮台等级的按钮 auto left = menuitemimage::create(“-.png”, “—.png”, cc_callback_1(gamescene::changebattery, this)); left->setname(“left”); auto menu2 = menu::create(left, nullptr); this->addchild(menu1, 9); menu1->setposition(vec2(visiblesize.width / 2 + 230, visiblesize.height / 2 – 365)); this->addchild(menu2, 9); menu2->setposition(vec2(visiblesize.width / 2 – 50, visiblesize.height / 2 – 365)); //添加一个初始炮台 battery = sprite::create(“paotai1.png”); battery->setposition(vec2(visiblesize.width / 2 + 90, visiblesize.height / 2 – 340)); addchild(battery, 9);</span>
首先创建两个按钮精灵,就是放上两个图片,设置名字为right和left,用来控制炮台的等级,后面创建添加一个初始炮台到游戏界面。后面给按钮写上回调方法changebattery();
<span style=”font-size:14px;”>void gamescene::changebattery(ref* psender){ size visiblesize = director::getinstance()->getvisiblesize(); auto ss = (menuitemimage*)psender; string str = ss->getname(); if (str == “right”){ //增加炮台等级 if (type == 3){ type = 1; } else{ type++; } } else if (str == “left”){ //减少炮台等级 if (type == 1){ type = 3; } else{ type–; } } <span style=”color:#ff0000;”>makebattery(type)</span>; }</span>
传入点击的对象转化字符串,判断字符串与设置的名字是否相等,进入不同的语句。这里的type是炮台类型,设置的是3个炮台,以上就是控制炮台类型,后面写了makebattery(type); 也就是生成炮台的关键:
<span style=”font-size:14px;”>void gamescene::makebattery(int type){ size visiblesize = director::getinstance()->getvisiblesize(); if (battery){ battery->removefromparent(); battery = null; } if (type == 1 && battery == null){ battery = sprite::create(“paotai1.png”); } else if (type == 2 && battery == null){ battery = sprite::create(“paotai2.png”); } else if (type == 3 && battery == null){ battery = sprite::create(“paotai3.png”); } battery->setposition(vec2(visiblesize.width / 2 + 90, visiblesize.height / 2 – 340)); this->addchild(battery,9); }</span>
传入一个炮台类型,先判断炮台是否存在,如果存在就删除它,再根据类型type进入不同语句从而生成新的炮台,给它设置坐标,添加到游戏界面,就可以通过两个按钮实现炮台循环切换,这样就完成了炮台部分。 三、炮弹
void gamescene::ontouchended(touch* touch, event* event){ size visiblesize = director::getinstance()->getvisiblesize(); auto location = touch->getlocation(); ? ? ? //获得坐标getlocation(); auto dx = location.x – battery->getpositionx(); auto dy = location.y – battery->getpositiony(); auto radian = atan2(dy , dx); auto inclination = radian * 180 / 3.14; rotation = -(inclination)+90; if (rotation <= 90 && rotation >= -90){ battery->setrotation(rotation); this->makebullet(type); auto move2 = moveby::create(2, vec2(dx * 10, dy * 10)); bullet->runaction(move2); } }
void gamescene::makebullet(int type){ size visiblesize = director::getinstance()->getvisiblesize(); if (coin>0){ if (type == 1){ bullet = sprite::create(“zidan1.png”); coin -= 1; } else if (type == 2){ bullet = sprite::create(“zidan2.png”); coin -= 10; } else if (type == 3){ bullet = sprite::create(“zidan3.png”); coin -= 100; } bullet->setrotation(rotation); bullet->setposition(vec2(visiblesize.width / 2 + 90, visiblesize.height / 2 – 333)); this->addchild(bullet, 8); bulletvec.pushback(bullet); } if (coin<t){ auto gg = sprite::create(“gg.png”); gg->setposition(visiblesize.width / 2, visiblesize.height / 2 + 290); addchild(gg, 9); } }
四、序列帧动画
? ? ccspriteframe:精灵帧信息,序列帧动画是依靠多个精灵帧信息来显示相应的纹理图像,一个精灵帧信息包包含了所使用的纹理,对应纹理块的位置以及纹理块是否经过旋转和偏移,这些信息可以取得对应纹理中正确的纹理块区域做为精灵帧显示的图像。 ? ? ccanimationframe:序列帧动画单帧信息,它存储了对应的精灵帧信息。 ? ? ccanimation:序列帧动画信息,它存储了所有的单帧信息,可以对单帧信息进行管理。 ? ? ccanimate:序列帧动画处理类,它是真正完成动画表演的类。 void gamescene::fishanim(){ <span style=”white-space:pre”> </span>spriteframecache *framecache = spriteframecache::getinstance(); framecache->addspriteframeswithfile(“fish.plist”, “fish.png”); ? ?//加载plist缓存 size visiblesize = director::getinstance()->getvisiblesize(); auto fish = sprite::createwithspriteframename(“fish1.png”); ? ? //创建一个鱼的首个帧 fishvec.pushback(fish); ? ? ?//放进数组 fish->setposition(vec2(dd, dt)); ? ? //设置坐标 this->addchild(fish, 5); ? ? //添加到场景 animation* animation = animation::create(); ? ? ? //创建序列帧动画 animation->setdelayperunit(0.01f); ? ? ? ? ? ? ? //0.01s播放完序列帧动画 animation->addspriteframe(framecache1->getspriteframebyname(“fish1.png”)); animation->addspriteframe(framecache1->getspriteframebyname(“fish2.png”)); animation->addspriteframe(framecache1->getspriteframebyname(“fish3.png”)); ? ? //3个序列帧动画顺序执行 animate* animate = animate::create(animation); ? ? ? ? ? ? //帧动画处理 fish->runaction(sequence::create(animate, callfunc::create(cc_callback_0(gamescene::remove, this)), nullptr)); }
<span style=”font-size:18px;”>void gamescene::xiaoyuupdate(float dt){ size visiblesize = director::getinstance()->getvisiblesize(); int s = rand() % 7; ? ? ? //随机的s用于传入switch语句 auto move = moveto::create(8, vec2(visiblesize.width + 650, rand() % 768)); ? ? //下面做两个移动的动作move1和move auto move1 = moveto::create(15, vec2(-650, rand() % 768)); switch (s){ case 0: xiaoyu = sprite::createwithspriteframename(“wugui1.png”); xiaoyu->setposition(vec2(-xiaoyu->getcontentsize().width / 2, rand() % 768)); playanim(0); xiaoyu->runaction(move); p = 2; break; case 1: xiaoyu = sprite::createwithspriteframename(“huangyu1.png”); xiaoyu->setposition(vec2(-xiaoyu->getcontentsize().width / 2, rand() % 768)); playanim(1); xiaoyu->runaction(move); p = 1; break; case 2: xiaoyu = sprite::createwithspriteframename(“dly01.png”); xiaoyu->setposition(vec2(visiblesize.width + xiaoyu->getcontentsize().width / 2, rand() % 768)); playanim(2); xiaoyu->runaction(move1); p = 2; break; case 3: xiaoyu = sprite::createwithspriteframename(“dm01.png”); xiaoyu->setposition(vec2(visiblesize.width + xiaoyu->getcontentsize().width / 2, rand() % 768)); playanim(3); p = 2; xiaoyu->runaction(move1); break; default: break; } addchild(xiaoyu); }</span>
<span style=”font-size:18px;”>void gamescene::playanim(int x){ size visiblesize = director::getinstance()->getvisiblesize(); if (x == 0){ animation* animation1 = animation::create(); animation1->setdelayperunit(0.4f); animation1->addspriteframe(framecache->getspriteframebyname(“wugui1.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui2.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui3.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui2.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui1.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui2.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui3.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui2.png”)); animation1->addspriteframe(framecache->getspriteframebyname(“wugui1.png”)); animate* animate1 = animate::create(animation1); xiaoyu->runaction(repeatforever::create(animate1)); } else if (x == 1){ animation* animation2 = animation::create(); animation2->setdelayperunit(0.1f); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu1.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu2.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu3.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu2.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu1.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu2.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu3.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu2.png”)); animation2->addspriteframe(framecache->getspriteframebyname(“huangyu1.png”)); animate* animate2 = animate::create(animation2); xiaoyu->runaction(repeatforever::create(animate2)); } else if (x == 2){ animation* animation3 = animation::create(); animation3->setdelayperunit(0.1f); animation3->addspriteframe(framecache1->getspriteframebyname(“dly01.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly02.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly03.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly04.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly05.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly06.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly07.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly08.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly09.png”)); animation3->addspriteframe(framecache1->getspriteframebyname(“dly10.png”)); animate* animate3 = animate::create(animation3); xiaoyu->runaction(repeatforever::create(animate3)); } else if (x == 3){ animation* animation5 = animation::create(); animation5->setdelayperunit(0.1f); animation5->addspriteframe(framecache1->getspriteframebyname(“dm01.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm02.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm03.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm04.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm05.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm06.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm07.png”)); animation5->addspriteframe(framecache1->getspriteframebyname(“dm08.png”)); animate* animate5 = animate::create(animation5); xiaoyu->runaction(repeatforever::create(animate5)); } fishvec.pushback(xiaoyu); ? //将鱼放进数组 }</span>
void gamescene::update(float dt){ for (int i = 0; i < fishvec.size(); i++){ for (int j = 0; j < bulletvec.size(); j++){ if (fishvec.at(i)->getboundingbox().intersectsrect(bulletvec.at(j)->getboundingbox())){ makeyuwang(type, bulletvec.at(j)->getpositionx(), bulletvec.at(j)->getpositiony()); remove2(bulletvec.at(j)); return; } } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论