0

だから私はxcodeとC4を使って弾むボールアプリを再作成しようとしています. 私が今やろうとしているのは、Dan Shiffman の Nature of Code の本にある例のいくつかを C4 で再作成することです。

これまでのところ、画面上を跳ね回る「Mover」オブジェクトの独自のクラスを作成することができました。それはすべてうまくいっています。次のステップは、ムーバーに「何らかの力を適用」し、それをいじることでした。

強制を適用するメソッドを追加したので、プログラムをクラッシュさせる例外が発生しているようです。例外ブレークポイントを設定すると、C4Control.m の次の行で停止します。

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

これをさらに調べてみると、super.center プロパティにも center プロパティにも値が割り当てられていないことがわかります (両方とも NaN)。何が原因なのかわからない?これは、各ムーバーの初期速度/加速度を 0 に設定し、これらのオブジェクトにモーションを作成するために「フォア」(風と重力の 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