简体   繁体   English

如何将in循环中的int解析为xcode中的方法?

[英]how to parse int in a for loop to a method in xcode?

Hi guys what I am trying to do is to create 6 sprites and to space them out evenly, I edited some code i got from a book, but am currently stuck at the part where I need to space the sprites out evenly 嗨,大家好,我要创建6个精灵,并将它们均匀地隔开,我编辑了从书中得到的一些代码,但目前停留在需要将精灵均匀地隔开的部分

    [groundNode setPosition:CGPointMake(45, 20)];

where this will stack all 6 sprites on top of each other? 这会将所有6个Sprite彼此堆叠在一起? How can I make it in such a way it would become something like 我怎样才能使它变成类似

    [groundNode setPosition:CGPointMake(45*x, 20)];

where x is the int taken from the for loop. 其中x是从for循环中获取的int。 My code is listed at the bottom. 我的代码在底部列出。 Thank you so much!! 非常感谢!!

-(id)init{
    self = [super init];
    if(self !=nil){
        for(int x=0;x<6;x++){
            [self createGround];
        }   
    }
    return self;
}

-(void) createGround{
    int randomGround = arc4random()%3+1;
    NSString *groundName = [NSString stringWithFormat:@"ground%d.png", randomGround];
    CCSprite *groundSprite = [CCSprite spriteWithFile:groundName];
    [self addChild:groundSprite];
    [self resetGround:groundSprite];
}

-(void) resetGround:(id)node{
    CGSize screenSize =[CCDirector sharedDirector].winSize;
    CCNode *groundNode = (CCNode*)node;
    [groundNode setPosition:CGPointMake(45, 20)];

}

Step one is to build those methods to take an offsetIndex parameter: 第一步是构建这些方法以采用offsetIndex参数:

-(void) createGroundWithOffsetIndex: (int) offsetIndex {
-(void) resetGround: (CCNode *) node withOffsetIndex: (int) offsetIndex {

Then, in createGround, pass it through: 然后,在createGround中,将其传递给:

[self resetGround:groundSprite withOffsetIndex: offsetIndex];

And pass it in from the loop: 并从循环中传递它:

for(int x=0;x<6;x++){
  [self createGroundWithOffsetIndex:x];
}       

And finally, the bit of code you knew you'd use (inside resetGround:withOffsetIndex:): (note the +1, since offsets (by semantic) start at zero) 最后,您知道将要使用的部分代码(在resetGround:withOffsetIndex:内部):(请注意+1,因为偏移量(按语义)从零开始)

[groundNode setPosition:CGPointMake(45 * offsetIndex+1, 20)];

Some notes: 一些注意事项:

  • Consider carefully how much passing is necessary here, and try to think of an improved architecture: if you are tiling the same image, perhaps createGround should take a CGRect and be responsible for filling that much area? 仔细考虑这里需要进行多少次传递,然后尝试考虑一种改进的体系结构:如果要平铺相同的图像,也许createGround应该采用CGRect并负责填充那么多区域?

  • This is horizontal only; 这只是水平的; I leave it as an exercise to pass CGPoints (or similar {x,y} struct) as the offsetIndex. 我将其作为传递CGPoints(或类似的{x,y}结构)作为offsetIndex的练习。

  • Your casting patterns are borderline concerning. 您的投放模式是边界线。 Why pass it as an id, then cast it into another local var, when the whole time it came in as yet another type? 为什么将其作为id传递,然后将其转换为另一个本地var,而将其作为另一种类型呢? I'd think that one over... 我想那...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM