0

我有一个 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);

}
4

2 回答 2

0

首先,您不需要这样做:

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

CCLayerColor从 CCLayer 继承(参考:文档),我当然希望你不要继承他们两个!:)

至于手头的问题,我无法确定问题,但我可以发现一些可能的陷阱,这可能会导致您的问题。

  1. 很多节点。你有你的层,你在上面添加_moving节点,在上面添加_pipes节点,在上面添加pipePair节点。除非它们对您游戏中的其他事物必不可少,否则我看不出您不pipePair直接在图层上添加的原因。

  2. 您没有设置_movingand的位置_pipes- 尽管它可能看起来并不重要,但最好明确定位所有节点,以免以后感到惊讶。这里有用的是`CCPointZero.

  3. CCSequences我从来没有像你一样重用过任何成功。事实证明,最好只在要使用它们时创建它们,以后不用担心它们。

希望其中一些会有所帮助!

于 2014-03-20T12:42:34.937 回答
-1

尝试将延迟从 2.0f 增加到 3.0f

CCDelayTime *delayForTwoSecs = CCDelayTime::create(3.0);
于 2014-03-24T09:27:48.347 回答