Cocos2D-在建筑物之间添加孔和不同的高度

问题描述

|| 我正在尝试构建类似于Canabalt的东西,在建筑物(模块)之间添加孔和不同的高度。您可以在以下位置查看有关游戏外观的屏幕截图:http://twitpic.com/4kb5jd 这是我当前正在使用的代码
-(id) init
{
    // always call \"super\" init
    // Apple recommends to re-assign \"self\" with the \"super\" return value
    if( (self=[super init] )) {

        moduleSize = 160;

        screenSize = [[CCDirector sharedDirector] winSize];

        CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
        [frameCache addSpriteFramesWithFile:@\"ModulesScene1.plist\"];

        CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@\"ModulesScene1.png\"];

        // Create the background spritebatch
        spriteBatch = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
        [self addChild:spriteBatch];

        numStripes = 1;

        /* BEGIN MODULES */

        Nsstring* frameName = [Nsstring stringWithFormat:@\"Module0-hd.png\"];
        CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
        sprite.anchorPoint = CGPointMake(0,0.5f);
        sprite.position = CGPointMake(0,screenSize.height / 2);
        [spriteBatch addChild:sprite z:0 tag:0];

        frameName = [Nsstring stringWithFormat:@\"Module0-hd.png\"];
        sprite = [CCSprite spriteWithSpriteFrameName:frameName];
        sprite.anchorPoint = CGPointMake(0,0.5f);
        sprite.position = CGPointMake((moduleSize - 1.1f),screenSize.height / 2);
        [spriteBatch addChild:sprite z:1 tag:1];

        frameName = [Nsstring stringWithFormat:@\"Module1-hd.png\"];
        sprite = [CCSprite spriteWithSpriteFrameName:frameName];
        sprite.anchorPoint = CGPointMake(0,0.5f);
        sprite.position = CGPointMake((moduleSize * 2) - 1.1f,screenSize.height / 2);
        [spriteBatch addChild:sprite z:2 tag:2];

        frameName = [Nsstring stringWithFormat:@\"Module2-hd.png\"];
        sprite = [CCSprite spriteWithSpriteFrameName:frameName];
        sprite.anchorPoint = CGPointMake(0,0.5f);
        sprite.position = CGPointMake(((moduleSize * 3) - 1.1f),screenSize.height / 2);
        [spriteBatch addChild:sprite z:3 tag:3];

        frameName = [Nsstring stringWithFormat:@\"Module0-hd.png\"];
        sprite = [CCSprite spriteWithSpriteFrameName:frameName];
        sprite.anchorPoint = CGPointMake(0,0.5f);
        sprite.position = CGPointMake(((moduleSize * 4) - 1.1f),screenSize.height / 2);
        [spriteBatch addChild:sprite z:4 tag:4];

        frameName = [Nsstring stringWithFormat:@\"Module1-hd.png\"];
        sprite = [CCSprite spriteWithSpriteFrameName:frameName];
        sprite.anchorPoint = CGPointMake(0,0.5f);
        sprite.position = CGPointMake(((moduleSize * 5) - 1.1f),screenSize.height / 2);
        [spriteBatch addChild:sprite z:5 tag:5];

        /* END MODULES */

        // Get current scrollSpped
        scrollSpeed = [[GameManager sharedGameManager] scrollSpeed];

        speedFactors = [[CCArray alloc] initWithCapacity:numStripes];
        [speedFactors addobject:[NSNumber numberWithFloat:2.5f]];
        NSAssert([speedFactors count] == numStripes,@\"speedFactors count does not match numStripes!\");

        [self scheduleUpdate];

    }

    return self;

}

-(void) update:(ccTime)delta
{

    CCSprite* sprite;
    scrollSpeed = [[GameManager sharedGameManager] scrollSpeed];

    CCARRAY_FOREACH([spriteBatch children],sprite)
    {

        NSNumber* factor = [speedFactors objectAtIndex:0];

        CGPoint pos = sprite.position;
        pos.x -= scrollSpeed * [factor floatValue];

        if (pos.x < -screenSize.width)
        {

            pos.x += ((screenSize.width * 2) - 2);

            int x = (arc4random() % 3);

            int xHole = (arc4random() % 10);

            Nsstring *randomName = nil;

            CCSpriteFrame *randomFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:randomName];
            [sprite setdisplayFrame:randomFrame];

        }

        sprite.position = pos;

    }

}
    

解决方法

要在建筑物之间添加孔,您可以添加一个简单的rand()函数,例如init方法中的函数: ...
sprite.position = CGPointMake(((moduleSize * 3) - 1.1f + (rand()%40)),screenSize.height / 2);
... 这将增加一个随机差距(最多40分)     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...