一个简单的小游戏

//HelloWorldScene.h

class HelloWorld : public cocos2d::CCLayerColor
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init(); 

	void addTarget();
	void spriteMoveFinished(CCNode* sender);
	void gameLogic(float dt);

	virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
   static cocos2d::CCScene* scene();

    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

#include "HelloWorldScene.h"

using namespace cocos2d;

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{

    bool bRet = false;

	if ( !CCLayerColor::initWithColor( ccc4(255,255,255,255) ) )
	{
      return false;
	}

    do 
    {
        //
        // super init first
        //
        CC_BREAK_IF(! CCLayer::init());

		CCSize size = CCDirector::sharedDirector()->getWinSize();

		CCSprite* pSprite = CCSprite::create("Player.png",CCRectMake(0,0,27,40));
		CC_BREAK_IF(!pSprite);
		pSprite->setPosition(ccp(pSprite->getContentSize().width/2,size.height/2));
		this->addChild(pSprite);

		//schedule_selector 监听
		this->schedule(schedule_selector(HelloWorld::gameLogic),1.0);

		this->setTouchEnabled(true);

        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

void HelloWorld::gameLogic(float dt)
{
	this->addTarget();

}

void HelloWorld::addTarget()
{
   //首先先始化精灵
	CCSprite *pTarget = CCSprite::create("Target.png",CCRectMake(0,0,27,40));
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();

	//计算可绘制的范围
	int minY = pTarget->getContentSize().width/2;
	int maxY = winSize.height-minY;
    //计算可随机基数
	int rangeY = maxY-minY;
	//随机出的基数+半身高 = 最后的坐标点
	int actualY = rand()%rangeY + minY;
    
	pTarget->setPosition(ccp(winSize.width +pTarget->getContentSize().width/2,actualY));
	this->addChild(pTarget);


    //计算移动速度 最慢4秒,最快2秒
    
	int minDuration = (int) 2.0;
	int maxDuration = (int) 4.0;
	int rangeDuration = maxDuration - minDuration;
	int actuaDuration = (rand()%rangeDuration)+minDuration;

	//初始化耗时动作
	CCFiniteTimeAction* actionMove=
		CCMoveTo::create((float)actuaDuration,
		ccp(0-pTarget->getContentSize().width/2,actualY));

	//动作完了干什么
	CCFiniteTimeAction* actionMoveDone=
		CCCallFuncN::create(this,
		callfuncN_selector(HelloWorld::spriteMoveFinished));

	 //绑定动作 CCSequence是用来按顺序执行一系列的动作,即动作按排列的顺序一个接一个的执行

	pTarget->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));

}

void HelloWorld::spriteMoveFinished(CCNode* sender)
{
	CCSprite* pSprite = (CCSprite*)sender;
	this->removeChild(pSprite,true);
}

void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
	CCTouch* touch = (CCTouch*) touches->anyObject();
	CCPoint location = touch->getLocation();
	//location = CCDirector::sharedDirector()->convertToGL(location);
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCSprite *pProjectile = CCSprite::create("Projectile.png",CCRectMake(0,0,20,20));

	pProjectile->setPosition(ccp(20,size.height/2));

	int offX = location.x - pProjectile->getPosition().x;
	int offY = location.y - pProjectile->getPosition().y;

    if(offX<=0) return;

	//添加发子弹声音

	//添加子弹
	this->addChild(pProjectile);

	int realX = size.width + (pProjectile->getContentSize().width/2);
	float ratio =(float) offY / (float)offX;
	int realY = (realX * ratio) + pProjectile->getPosition().y;

	CCPoint realDest = ccp(realX,realY);

	// Determine the length of how far we're shooting
	int offRealX = realX - pProjectile->getPosition().x;
    int offRealY = realY - pProjectile->getPosition().y;
    float length = sqrtf((offRealX * offRealX) 
                                       + (offRealY*offRealY));
    float velocity = 480/1; // 480pixels/1sec
    float realMoveDuration = length/velocity;
	
	//绑定动作
	pProjectile->runAction(CCSequence::create(
		CCMoveTo::create(realMoveDuration,realDest),
		CCCallFuncN::create(this,
		callfuncN_selector(HelloWorld::spriteMoveFinished)),
	    NULL
		));

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值