我有一个 Init 方法和 spawn() 方法,每 2 秒由 CCAction 调用一次!我想要的是pipePair
每 2 秒在屏幕上移动一次节点,就像在飞扬的小鸟游戏中一样!但在我的情况下,我无法在屏幕中添加多对节点,而且 MoveBy 的速度会CCAction
定期加快。
所以我希望节点应该定期添加,并且它们应该在屏幕上以恒定的速度移动。
任何帮助,将不胜感激。
bool HelloWorld::init()
{
//creating the world
b2Vec2 gravity;
gravity.Set(0.0f, -20.0f);
world = new b2World(gravity);
// Do we want to let bodies sleep?
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
setAccelerometerEnabled( true );
scheduleUpdate();
setTouchEnabled(true);
//////////////////////////////
// 1. super init first
if( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ) //RGBA
{
return false;
}
if ( !CCLayer::init() )
{
return false;
}
screenSize = CCDirector::sharedDirector()->getWinSize();
//Initializing CCNodes
_moving = CCNode::create();
addChild(_moving);
_pipes = CCNode::create();
_moving->addChild(_pipes);
CCSprite *test = CCSprite::create("goalssss.png");
CCRect rectOfPipe = test->boundingBox();
float64 distanceToMove = screenSize.width + 2.5*rectOfPipe.size.width;
CCMoveBy* movePipes = CCMoveBy::create(0.01*distanceToMove, CCPointMake(-screenSize.width, 0));
CCRemoveSelf *removePipes = CCRemoveSelf::create();
sequenceActionOfPipes = CCSequence::create(movePipes,removePipes);
sequenceActionOfPipes->retain();
CCCallFunc *spawn = CCCallFunc::create(this, callfunc_selector(HelloWorld::spawnPipes));
CCDelayTime *delayForTwoSecs = CCDelayTime::create(2.0);
CCSequence *sequenceForSpawnAndDelay = CCSequence::create(spawn,delayForTwoSecs);
CCRepeatForever *repeatActionForeverOfSpawnAndDelay = CCRepeatForever::create(sequenceForSpawnAndDelay);
this->runAction(repeatActionForeverOfSpawnAndDelay);`
}
这是我的 spawn 方法,每 2 秒调用一次!
void HelloWorld::spawnPipes(){
CCNode *pipePair = CCNode::create();
pipePair->setPosition(CCPoint(screenSize.width, 0));
//pipePair->setZOrder(-10);
float64 randomVal = arc4random()%(int32)(screenSize.height/3);
CCSprite *_pipeUpReplica = CCSprite::create("goalssss.png");
_pipeUpReplica->setPosition(CCPoint(20, randomVal));
CCSprite *_pipeBelowReplica = CCSprite::create("goalssss.png");
_pipeBelowReplica->setPosition(CCPoint(20, randomVal+100+_pipeUpReplica->boundingBox().size.height));
pipePair->addChild(_pipeUpReplica);
pipePair->addChild(_pipeBelowReplica);
//run actions
pipePair->runAction(sequenceActionOfPipes);
_pipes->addChild(pipePair);
}