点击精灵区域消除,点击其他区域创建新精灵。(模拟放置炸d,及引爆)

点击精灵区域消除,点击其他区域创建新精灵。(模拟放置炸d,及引爆),第1张

概述.h文件 #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"using namespace cocos2d;class HelloWorld : public cocos2d::CCLayer{public: // Here's a difference. Metho

.h文件

#ifndef __HELLOWORLD_SCENE_H__#@R_502_5552@ __HELLOWORLD_SCENE_H__#include "cocos2d.h"using namespace cocos2d;class HelloWorld : public cocos2d::cclayer{public:    // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone    virtual bool init();      // there's no 'ID' in cpp,so we recommend returning the class instance pointer    static cocos2d::CCScene* scene();        // a selector callback    voID menuCloseCallback(CCObject* pSender);        // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);        virtual voID onEnter();    virtual voID onExit();    virtual bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent);    voID update();    CCSprite* pSprite;    bool first_touch = 1;    int i = 0;    bool createnewobject(int i,CCPoint location);    };#endif // __HELLOWORLD_SCENE_H__

.cpp
#include "HelloWorldScene.h"USING_NS_CC;CCScene* HelloWorld::scene(){    // 'scene' is an autorelease object    CCScene *scene = CCScene::create();        // 'layer' is an autorelease object    HelloWorld *layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !cclayer::init() )    {        return false;    }        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();    /////////////////////////////    // 2. add a menu item with "X" image,which is clicked to quit the program    //    you may modify it.    // add a "close" icon to exit the progress. it's an autorelease object    CcmenuItemImage *pCloseItem = CcmenuItemImage::create(                                        "Closenormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback));    	pCloseItem->setposition(ccp(origin.x + visibleSize.wIDth - pCloseItem->getContentSize().wIDth/2,origin.y + pCloseItem->getContentSize().height/2));    // create menu,it's an autorelease object    Ccmenu* pMenu = Ccmenu::create(pCloseItem,NulL);    pMenu->setposition(CCPointZero);    this->addChild(pMenu,1);            /////////////////////////////    // 3. add your codes below...    // add a label shows "Hello World"    // create and initialize a label    /*        cclabelTTF* pLabel = cclabelTTF::create("Hello World","Arial",24);        // position the label on the center of the screen    pLabel->setposition(ccp(origin.x + visibleSize.wIDth/2,origin.y + visibleSize.height - pLabel->getContentSize().height));    // add the label as a child to this layer    this->addChild(pLabel,1);    // add "HelloWorld" splash screen"    CCSprite* pSprite = CCSprite::create("HelloWorld.png");    // position the sprite on the center of the screen    pSprite->setposition(ccp(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));    // add the sprite as a child to this layer    this->addChild(pSprite,0);     */    //this->schedule(schedule_selector(HelloWorld::update),3,5,1);        return true;}voID HelloWorld::menuCloseCallback(CCObject* pSender){    CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}voID HelloWorld::onEnter(){    cclayer::onEnter();    this->settouchEnabled(true);    CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,true);}voID HelloWorld::onExit(){    cclayer::onExit();    CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this);}bool HelloWorld::cctouchBegan(CCtouch *ptouch,CCEvent* pEvent){ //取坐标    CCPoint touchloaction = ptouch->getLocation();        CCPoint location = convertToNodeSpace(touchloaction);        cclog("NodeLocacion is x=%f y=%f",location.x,location.y);    //第一次触摸创建一个精灵    //再次触摸输出坐标信息,判断是否点中该精灵,如果点中:输出“击中物体”;删除该精灵; 如果没有点中:输出“没有击中物体”,在该位置新建一个精灵        if (first_touch)    {        createnewobject(i,location);        first_touch =0;    }    else    {                CCRect pobject = pSprite->boundingBox();                bool flag = pobject.containsPoint(location);                if (flag) {            cclog("hit the object");            removeChildByTag(i);            first_touch = 1;        }        else        {            cclog("miss the object");            removeChildByTag(i);            i++;            createnewobject(i,location);                    }            }       return false;}bool HelloWorld::createnewobject(int i,CCPoint location){    pSprite = CCSprite::create("Icon-72.png");        //pSprite->setposition(ccp(visibleSize.wIDth/2,visibleSize.height/2));        pSprite->setposition(ccp(location.x,location.y));        this->addChild(pSprite,i);        // move the psprite            CCMoveBy* move1 = CCMoveBy::create(2,ccp(200,150));    //CCMoveto* move2 = move1->reverse();    CCMoveBy* move2 = CCMoveBy::create(3,ccp(-200,-150));    CCScaleto* pScale= CCScaleto::create(5,pSprite->getScale()*2);    CCSequence* seq = CCSequence::create(move1,move2,NulL);    CCSpawn* spa = CCSpawn::create(seq,pScale);    CCRepeatForever* req =CCRepeatForever::create(spa);    pSprite->runAction(req);    return true;}voID HelloWorld::update(){    cclog("i am ok");        }
总结

以上是内存溢出为你收集整理的点击精灵区域消除,点击其他区域创建新精灵。(模拟放置炸d,及引爆)全部内容,希望文章能够帮你解决点击精灵区域消除,点击其他区域创建新精灵。(模拟放置炸d,及引爆)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1060255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存