客观c大随机方法,我这样做对吗?

时间:2012-07-29 19:10:40

标签: iphone ios

我一直在构建一个更简单的游戏,核心图形与随机脚本遭遇,如果这有意义,它开始成为视图控制器中的一个巨大的方法。

-(void)spawnStuff{
CGPoint position;
CGPoint position1;

int chance = random()%10;
switch (chance) {
    case 0:
            [self spawnWall];
        Position.y = 580;
        Position.x = 160;
        wall.center = Position;

        [self spawnWall];
        Position1.y = 480;
        Position1.x = 80;
        wall.center = Position1;

}
-(void)spawnWall{
UIImage* myImage = [UIImage imageNamed:@"Wall.png"];
Wall = [[Sprite alloc] initWithImage:myImage];

CGRect rect = CGRectMake(0, 0, 90, 50);
more initilization stuff }

我可能会重复这行代码20 - 30与这些墙的位置不同,并且只有10种不同的场景,大约是班级中的第3个代码,它开始得到一个lil redonkulous我还是很新编程和巨型方法有点吓到我。这是解决这个问题的正确方法吗?

1 个答案:

答案 0 :(得分:0)

如果你经常这样做,我会建议保留一系列可能的动作,然后在必要时随机选择一个。以下内容应该告诉你如何做到这一点。

注意,您可以随意添加/减去数组,这会更改可用的块...这只是在底部显示,随机选择并执行一次块100次。

您可以在代码中的相应位置添加/删除块,以使其保持本地化。这至少应该让你开始......

// If we want to perform some action randomly, then we build an array
// of possibly actions (blocks), and then randomly pick one and execute it.
// Figure out what type of block you want...

typedef void(^ActionBlock)(void);
NSMutableArray *possibleActions = [NSMutableArray array];
[possibleActions addObject:^{
    NSLog(@"Doing Action Foo");
}];
[possibleActions addObject:^{
    NSLog(@"Doing Action Bar");
}];
[possibleActions addObject:^{
    NSLog(@"Doing Action Baz");
}];
[possibleActions addObject:^{
    NSLog(@"Doing Action FooBar");
}];
[possibleActions addObject:^{
    NSLog(@"Doing Action Blarg");
}];
[possibleActions addObject:^{
    NSLog(@"Doing Action Zip");
}];

// Now, when I want to pick an action to perform...
for (int i = 0; i < 100; ++i) {
    ActionBlock block = [possibleActions objectAtIndex:arc4random_uniform(possibleActions.count)];
    block();
}

修改 这条线

typedef void(^ActionBlock)(void);

是一个typedef,它定义了特定类型块的类型。当然,函数和块的语法有点难以阅读。也许我应该退后一步。我希望它不会冒犯你,如果它以任何方式光顾,我会提前道歉。它类似于

typedef int IntType;
typedef void const * OpaqueType;

有点容易阅读。第一个定义类型为整数类型的IntType,第二个定义类型为“指向const-void的指针”的类型OpaqueType。然后,您可以声明变量,如...

IntType someInteger;
OpaqueType someOpaqueValue;

现在,在基本的C中,我们也可以声明函数指针。假设我们有一些功能......

int functionOne(int arg1, double arg2) {
    // do whatever...
}
int functionTwo(int arg1, double arg2) {
    // do whatever...
}

你可以轻松地调用该功能......

int result = functionOne(42, 3.14159);

好吧,你也可以创建一个指向该函数的变量,你可以像任何其他函数一样调用它。

int(*function)(int,double) = functionOne;
int result = function(42, 3.14159); // Calls functionOne
function = functionTwo;
result = function(42, 3.14159); // calls functionTwo

但是,这种语法输入有点令人厌烦。因此,特别是对于函数和块,我们创建了typedef。要为函数创建typedef,它与创建函数指针变量完全相同,但您只需将该类型放在变量名称的位置即可。所以......

typedef int(*MyFunction)(int, double);

声明一个类型,该类型表示指向返回int的函数的指针,并且接受两个参数,int和double。

现在,块的语法就像函数指针一样,除了用a *交换a ^。所以,输出类型......

typedef void(^ActionBlock)(void);

表示ActionBlock是一种类型,表示返回void且不带参数的块。

因此,您可以声明ActionBlock类型的变量,并将它们分配给具有void返回且没有参数的块。在这个例子中,这正是我所做的。

注意,在代码中,我们创建了一堆像这样的块......

[possibleActions addObject:^{
    NSLog(@"Doing Action Foo");
}];

编译器允许你省略显而易见的东西,所以代码真的说...创建一个不带参数的块,并且返回类型为void,然后把它添加到集合中。

我们多次这样做,然后,当准备调用函数时,我们只是迭代集合。但是,编译器想要知道如何调用函数/块,并且从集合中获取的所有内容都是。因此,我们希望将我们得到的对象转换为一个类型,该类型告诉编译器它是一个返回void的块,并且不带参数。

当我们这样做时,我们可以调用函数/块。那么,让我们再看一下最后一篇文章(有一些评论),以及一点点不那么紧凑

for (int i = 0; i < 100; ++i) {
    // We have a bunch of blocks stored in the array.
    // We want to pick one at random, so we will get a random number
    // between [0, size of our array).
    uint32_t index = arc4random_uniform(possibleActions.count);

    // Now, we have a random index into our array.  Get the object in that position.
    id randomObject = [possibleActions objectAtIndex:index];

    // We have an opaque id type, but we know it's a block.  Cast it to a block type.
    ActionBlock block = randomObject;

    // Now, we have our block, in a type that the compiler will let us invoke.
    // Call it like any other function.
    block();
}

修改

好的,你走了。创建一个iVar,NSTimer * _timer。在didLoad中安排它,并在didUnload和dealloc

中使其无效
- (void)dealloc {
    [_timer invalidate];
}

typedef void(^ActionBlock)(void);

- (void)callRandomFunction:(NSTimer*)timer {
    // Our userInfo is actually our array of blocks
    NSArray *actions = timer.userInfo;
    ActionBlock block = [actions objectAtIndex:arc4random_uniform(actions.count)];
    block();
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *possibleActions = [NSMutableArray array];

    // Create the list of possible functions that can be called...

    // Now setup a timer that performs callRandomFunction: every second
    // Pass the array of blocks as the userInfo of the timer.
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(callRandomFunction:) userInfo:possibleActions repeats:YES];

    // Whatever else you need in here...
}

- (void)viewDidUnload
{
    [_timer invalidate];

    // Any other unload stuff...

    [super viewDidUnload];
}