cocos2d-x创建精灵(2)
来源:未知 责任编辑:责任编辑 发表时间:2013-12-06 08:43 点击:次
p> }
p>}
p>
p>也就是init也是真正需要我们定制的:
p>[cpp]
p>// on "init" you need to initialize your instance
p>bool HelloWorld::init()
p>{
p> if (!CCLayerColor::initWithColor( ccc4(255,255,255,255) ))
p> {
p> return false;
p> }
p>
p> CCSize winSize = CCDirector::sharedDirector()->getWinSize();
p> CCSprite *player = CCSprite::create("Player.png",
p> CCRectMake(0,0,27,40));
p>
p> player->setPosition(ccp(player->getContentSize().width/2, winSize.height/2));
p>
p> addChild(player);
p>
p> schedule(schedule_selector(HelloWorld::gameLogic),1.0);
p>
p> return true;
p>}
p>
p>看见没,上面就调用了CCLayerColor的函数了。然后里面创建了一个精灵。当然里面还有不严谨的地方,之后我会指出来。
p>我们看重要的一句逻辑:
p>[cpp]
p>schedule(schedule_selector(HelloWorld::gameLogic),1.0);
p>这句的作用 API有说明:Callback interval time in seconds. 0 means tick every frame, 就是每隔一段时间回调一次。当然我们使用这个回调来产生敌人的。
p>依次调用的函数有:
p>void HelloWorld::gameLogic( float dt )
p>{
p>addTarget();
p>}
p>[cpp]
p>void HelloWorld::addTarget()
p>{
p> CCSprite* target = CCSprite::create("Target.png", CCRectMake(0,0,27,40));
p> if (NULL == target)
p> {
p> CCLOG("Create target failed");
p> return;
p> }
p>
p> CCSize winSize = CCDirector::sharedDirector()->getWinSize();
p>
p> const int minY = target->getContentSize().height/2;
p> const int maxY = winSize.height-minY;
p> const int rangeY = maxY- minY;
p> const int actualY = (rand()%rangeY)+minY;
p>
p> target->setPosition(ccp(winSize.width+target->getContentSize().width/2, actualY));
p>
p> addChild(target);
p>
p> const int minDuration = 2;
p> const int maxDuration = 4;
p> const int rangeDuration = maxDuration-minDuration;
p> const int actualDuration = (rand()%rangeDuration)+minDuration;
p>
p> CCFiniteTimeAction* action = CCMoveTo::create((float)actualDuration, ccp(-target->getContentSize().width/2, actualY));
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>