• 射击小游戏一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);
    
    }
  • 相关阅读:
    python 3.5下用户登录验证,三次锁定的编码
    Python之面向对象
    Python基础之模块
    Python基础之yield,匿名函数,包与re模块
    Python基础之函数
    Python基础之字符编码,文件操作流与函数
    Python基础之字符串,布尔值,整数,列表,元组,字典,集合
    Python基础之(判断,循环,列表,字典)
    mysql学习
    linux 下的 正则表达式(awk,sed,awk)学习
  • 原文地址:https://www.cnblogs.com/jiackyan/p/3017773.html
Copyright © 2020-2023  润新知