0

所以我正在尝试使用 xcode 和 C4 重新创建一个弹跳球应用程序。我现在要做的是在 C4 中重新创建 Dan Shiffmans Nature of Code book 中的一些示例。

到目前为止,我已经设法创建了我自己的在屏幕上弹跳的“移动器”对象类。这一切都很好。下一步是开始对这些推动者“施加一些力量”并进行尝试。

既然我已经添加了施加力的方法,我似乎遇到了一个导致程序崩溃的异常。我设置了异常断点,C4Control.m 中的这一行是它停止的地方:

if(_animationDuration == 0.0f) super.center = center;

进一步研究这一点,我注意到 super.center 或 center 属性都没有分配给它们的值(均为 NaN)。我不确定是什么原因造成的?一旦我决定将每个 Movers 的初始速度/加速度设置为 0,并且只分配“fores”(风和重力的 C4Vectors)来在这些对象中创建运动,这似乎就开始了。当我注释掉施加每个力的 2 行时,问题就消失了。

这是我的移动类的实现:

//Init Method
- (id)initWithPoint:(CGPoint)point andCanvasController:(C4CanvasController *)canvasController {
self = [super init];
if (self) {
    canvasFrame = CGPointMake(canvasController.canvas.width,
                              canvasController.canvas.height);

    diam = [C4Math randomInt:70];
    mass = diam * 0.5f;
    CGRect ellipseFrame = CGRectMake(0, 0, diam, diam);

    [self ellipse:ellipseFrame];
    self.lineWidth = 0.0f;
    self.fillColor = [UIColor colorWithRed:[C4Math randomInt:60]/60
                                     green:[C4Math randomInt:100]/100.0f
                                      blue:0.9f
                                     alpha:0.3f];

    int foo = arc4random() % (int)([canvasController.canvas height]);
    int bar = arc4random() % (int)([canvasController.canvas width]);


    self.location = [C4Vector vectorWithX:foo
                                        Y:bar
                                        Z:0.0f];

    self.origin = self.location.CGPoint;

    self.accel = [C4Vector vectorWithX:0.00f Y:0.0f Z:0.0f];
    self.vel = [C4Vector vectorWithX:0.00f Y:0.0f Z:0.0f];


}
return self;
}


//Method to calculate updated mover position
-(void)update{
[self.vel add:self.accel];
[self.location add:self.vel];
self.origin = self.location.CGPoint;
[self.accel multiplyScalar:0];
[self checkBounds];

}


//Checking if mover has reached edge of screen
-(void)checkBounds{
if (self.origin.x > canvasFrame.x - diam){
    self.location.x = canvasFrame.x - diam;
    self.vel.x *= -1;

}
else if (self.origin.x < 0){
    self.vel.x *= -1;
    self.location.x = 0;

}


if(self.origin.y > canvasFrame.y - diam){
    self.location.y = canvasFrame.y - diam;
    self.vel.y *= -1;
}
else if(self.origin.y < 0){
    self.vel.y *= -1;
    self.location.y = 0;
}

}


//Start C4Timer to run update every 1/60th of a second
- (void) run{
updateTimer = [C4Timer automaticTimerWithInterval:1/60.0f
                                                 target:self
                                                 method:@"update"
                                                repeats:YES];

}



//Calculate a force to apply to mover
- (void)applyForce:(C4Vector *)force{
[force divideScalar:mass];
[self.accel add:force];
}

在我的 C4WorkSpace.m 文件中:

#import "C4Workspace.h"
#import "Mover.h"

@implementation C4WorkSpace{
Mover *testMover;
NSMutableArray *moverArray;

}

-(void)setup {
C4Vector *gravity = [C4Vector vectorWithX:0.01f Y:0.0 Z:0.0f];
C4Vector *wind = [C4Vector vectorWithX:0 Y:0.4 Z:0];

moverArray = [[NSMutableArray alloc]init];
//Not using foo/bar variables right now, but usually use them to create CGPoint p
//int foo = arc4random() % (int)([self.canvas height]);
//int bar = arc4random() % (int)([self.canvas width]);

for (int i = 0; i < 500; i++) {
    CGPoint p = CGPointMake(0,0);

    Mover *m = [[Mover alloc]initWithPoint:p andCanvasController:self];
    [moverArray addObject:m];

}

for (Mover *m in moverArray){
    [self addShape:m];
    [m run];
    [m applyForce:gravity];
    [m applyForce:wind];
}




}

@end

很抱歉这已经持续了多长时间,非常感谢任何和所有的帮助!

4

0 回答 0