射击小游戏一02(玩家和怪物添加)

boolHelloWorld::init()

{

    if ( !CCLayer::init() )

    {

        returnfalse;

    }

    glClearColor(255.0f, 255.0f, 255.0f, 1.0f);//背景颜色

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

 

    CCSprite *player = CCSprite::create("Player.png");

    player->setPosition(ccp(player->getContentSize().width/2.0f,size.height/2.0f));

    this->addChild(player);    

    //定时的为游戏添加敌人

    this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0f);

    returntrue;

}

 

void HelloWorld::gameLogic(CCTime dt)

{

    this->addTarget();

}

void HelloWorld::addTarget()

{

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

    CCSprite *target = CCSprite::create("Target.png", CCRect(0, 0, 27, 40));

    //高度随机

    int minY = target->getContentSize().height/2;

    int maxY = winSize.height - target->getContentSize().height/2;

    int rangeY = maxY - minY;

    int actualY = (arc4random()%rangeY) +minY;

    target->setPosition(ccp(winSize.width + (target->getContentSize().width/2),actualY));

    this->addChild(target);    

    //速度随机

    int minDuration = (int)2.0f;

    int maxDuration = (int)4.0f;

    int rangeDuration = maxDuration - minDuration;

    int actualDuration  = (rand()%rangeDuration)+ minDuration;

    //create action

    CCFiniteTimeAction *actionMove = CCMoveTo::create(actualDuration, ccp(0-target->getContentSize().width/2, actualY));

    CCFiniteTimeAction *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));

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

}

//移出出了屏幕的精灵

void HelloWorld::spriteMoveFinished(CCNode*sender)

{

    CCSprite *sprite = (CCSprite*)sender;

    this->removeChild(sprite, true);

}

 

posted on 2013-04-13 10:04  jack_yan  阅读(216)  评论(0编辑  收藏  举报