Question

I have an Init method and spawn() method which is called by a CCAction every 2 seconds! What i want is to move pipePair node across the screen every 2 seconds, something like in the flappy bird game! But in my case am not able to add multiple pairs of the node in the screen plus the speed of the MoveBy CCAction fastens periodically.

So i want the nodes should be added periodically and they should move in a constant speed across the screen.

Any help would be appreciated.

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);`
}

This is my spawn method which is called every 2 seconds!

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);

}
Was it helpful?

Solution

Firs of all, you don't need to do this :

if( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ) //RGBA
{
    return false;
}
if ( !CCLayer::init() )
{
    return false;
}

CCLayerColor inherits from CCLayer (reference : docs) and I certainly hope you don;t inherit from both of them! :)

As to the problem at hand, I can't pinpoit a problem, but I can spot some possible pitfalls, which may be contrbuting to your issues.

  1. A lot of nodes. You have your Layer, on which you add the _moving node on which you add the _pipes node on which you add the pipePair node. Unless they are essential for other things in your game, I can;t see a reason why you are not adding the pipePair directly on your layer.

  2. You didn't set the position of _moving and _pipes - though it may seem unimportant, it is good to explicilty position all your nodes to not be suprised later on. A usefull thing here is the `CCPointZero.

  3. I have never had any success whatsoever with reusing CCSequences like you do. It always turned out to be best to just create them when they are to be used and not worry about them later on.

Hope some of these will help!

OTHER TIPS

Try increasing the delay from 2.0f to 3.0f

CCDelayTime *delayForTwoSecs = CCDelayTime::create(3.0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top