定义 新类 继承CCSprite ,记得重写工厂方式。

gamesprite* gamesprite::gameSpriteWithFile( const char *pszFileName) {
gamesprite* sprite = new gamesprite();
if(sprite&&sprite->initWithFile(pszFileName))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;
}

======
.h

ifndef HELLOWORLD_SCENE_H

define HELLOWORLD_SCENE_H

include “cocos2d.h”

using namespace cocos2d;

class gamesprite : public cocos2d::CCSprite
{
public:
gamesprite(void);
~gamesprite(void);
static gamesprite* gameSpriteWithFile(const char * pszFileName);

//virtual void setPosition(const CCPoint&pos);

};
class HelloWorld : public cocos2d::CCLayer
{
public:
enum{
ksprite
};
// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone
gamesprite* _player1;
gamesprite* _player2;
CCArray* _players;

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 ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);

CCSprite* pSprite;
bool first_touch = 1;
int i = 0;
bool createnewobject(CCPoint location);

};

endif // HELLOWORLD_SCENE_H

========
.cpp

include “HelloWorldScene.h”

USING_NS_CC;

gamesprite::gamesprite(void){

}
gamesprite::~gamesprite(void){

}

gamesprite* gamesprite::gameSpriteWithFile( const char *pszFileName) {
gamesprite* sprite = new gamesprite();
if(sprite&&sprite->initWithFile(pszFileName))
{
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;
}

/*
void gamesprite::setPosition(const CCPoint&pos) {
CCSprite::setPosition(pos);
}
*/
/*
gamesprite* gamesprite::gameSpriteWithFile(const char * pszFileName) {

gamesprite * sprite = new gamesprite();
if (sprite && sprite->initWithFile(pszFileName)) {
    sprite->autorelease();
    return sprite;
}
CC_SAFE_DELETE(sprite);
return NULL;

}
*/
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 instance
bool 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);
*/

//ms add here


this->setTouchEnabled(true);
return true;

}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();

if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

endif

}

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
//多点触摸
CCSetIterator iter = pTouches->begin();
for(iter;iter!=pTouches->end();iter++)
{
CCTouch pTouch = (CCTouch)(*iter);
CCPoint touchlocation = pTouch->getLocation();
CCPoint location = convertToNodeSpace(touchlocation);
CCLog(“Touch Location x=%f y=%f”,location.x,location.y);

//转化为单点触摸。
/*
CCTouch* ptouch = (CCTouch*)(pTouches->anyObject());
CCPoint touchlocation = ptouch->getLocation();
CCPoint location = convertToNodeSpace(touchlocation);
CCLog("Touch Location x= %f y=%f",location.x,location.y);
*/

if (first_touch)
{
    createnewobject(location);
    first_touch =0;
}
else
{

    CCRect pobject = _player1->boundingBox();

    bool flag = pobject.containsPoint(location);

    if (flag) {
        CCLog("hit the object");
        removeChildByTag(ksprite);
        first_touch = 1;
        }
    else
    {
        CCLog("miss the object");
        removeChildByTag(ksprite);
        createnewobject(location);

        }

    }
}

}

bool HelloWorld::createnewobject(CCPoint location)
{
    //pSprite = CCSprite::create("Icon-72.png");
    //ms added here
    _player1 = gamesprite::gameSpriteWithFile("Icon-72.png");

    //pSprite->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));

    //pSprite->setPosition(ccp(location.x,location.y));
    _player1->setPosition(ccp(location.x,location.y));

    //this->addChild(pSprite,0,ksprite);
    this->addChild(_player1,0,ksprite);

    // move the psprite

    CCSequence *easeSwing = CCSequence::create(
                                                       CCEaseInOut::create(CCRotateTo::create(0.3f, -10),1),
                                                       CCEaseInOut::create(CCRotateTo::create(0.3f,10),1),
                                                       NULL
                                                       );
    CCRepeat* rep = CCRepeat::create(easeSwing,6);

    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, _player1->getScale()*2);
    CCSequence* seq = CCSequence::create(move1,move2,NULL);
    CCSpawn* spa = CCSpawn::create(rep,seq,pScale,NULL);
    CCRepeatForever* req =CCRepeatForever::create(spa);
    _player1->runAction(req);
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值