0

I have a subclass of cpCCSprite, which is just for using chipmunk spaceManager and my class looks like this:

@implementation Helmet

+(id) helmetWithGame:(Game*)game {

    return [[[self alloc] helmetInit:game] autorelease];

}

- (id) helmetInit:(Game*)game {

    cpShape *helmet_1;
    cpShape *helmet_2;
    cpShape *helmet_3;
    cpShape *reference;

    reference = [game.spaceManager addCircleAt:cpvzero mass:STATIC_MASS radius:2];

    helmet_1 = [game.spaceManager addCircleToBody:reference->body radius:20 offset:cpv(-5, 2)];

    helmet_2 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(16, -14)];

    helmet_3 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(8, -14)];

    reference->group    =1;
    helmet_1->group     =1;
    helmet_2->group     =1;
    helmet_3->group     =1;

    [self initWithFile:@"Helmet.png"];
    [self setBody:reference->body];

    self.spaceManager = game.spaceManager;
    self.autoFreeShapeAndBody = YES;

    gameScreenSize = game.contentSize;

    return self;
}

- (void) generateAndShowOn:(Game *)game {

    float startX = (float)((arc4random_uniform(101)) + 100);//returns number from 100 to 200 and casts it as a float
    float startY;

    int endXRange = (game.contentSize.width * .8) - (game.contentSize.width * .5);
    float endX = (float)((arc4random_uniform(endXRange)) + (game.contentSize.width * .5));
    float endY;

    BOOL shouldStartTop;

    if ((arc4random_uniform(101)) < 50) {//returns number from 0 to 100 and checks to see if it is less than 50. If it is, than the helmut starts at the top
        shouldStartTop = YES;
        startY = game.contentSize.height + (self.contentSize.height * .5);
        endY = -self.contentSize.height;
    }

    else {

        shouldStartTop = NO;
        startY = -(self.contentSize.height * .5);
        endY = game.contentSize.height + self.contentSize.height;
    }

    self.position = ccp(startX, startY);
    [game addChild:self];

    ccBezierConfig bezier;
    bezier.controlPoint_1 = ccp (startX, startY);
    bezier.controlPoint_2 = ccp (endX, endY);
    bezier.endPosition    = ccp (endX, endY);

    id rotate =[CCRotateBy actionWithDuration:1.5f angle:360];
    id curve = [CCBezierTo actionWithDuration:1.5f bezier:bezier];
    id spawn = [CCSpawn actions:rotate, curve, nil];

    [self runAction:spawn];

    [self schedule:@selector(doneAnimating) interval:1.6];

}

- (void) doneAnimating{

    if ([[self delegate]respondsToSelector:@selector(helmetPastBounds:)]) {
        [[self delegate]helmetPastBounds:self];

    }
}

- (void) dealloc {

    CCLOG(@"%s", __PRETTY_FUNCTION__);

    [super dealloc];
}

@end

So a new instance of this class is called every second, so that there can be more than one helmet on the screen at a time. Then I have another method that is called just slightly after the acctions have finished to remove the current helmet and deallocate it. But the problem I am having is every so often the actions are jumpy/jerky and that is with the FPS at 60. Do I need to do something different with the actions or what could be causing this?

Stephen
  • 499
  • 9
  • 28
  • actions and physics working against each other? – dqhendricks Mar 07 '13 at 21:54
  • 1
    FPS is an average. There could be a 'one frame' dip at 30 FPS, when a large texture is loaded for example. Does this happen in a device or the simulator (big difference) ? If you suspect texture loading, you could try pre-loading the texture cache with your textures before starting this game sequence - ymmv , make certain you do this when a dip in fps is hardly noticeable. – YvesLeBorg Mar 07 '13 at 21:56
  • It happens on both the device and the simulator. – Stephen Mar 07 '13 at 21:59
  • @dqhendricks can you elaborate on actions and physics working against each other? – Stephen Mar 07 '13 at 21:59
  • Well, chipmunk is a physics engine right? So I imagine it puts forces on your objects to move them or whatever. Your action however is just trying to move your objects linearly. I'm sure at times the two things operating on the same object would cause problems. Typically with physics engines, you apply a force to something in order to move it, not an action. I don't know much about chipmunk however, so I could be terribly wrong. Here is something I found with more info: http://www.cocos2d-iphone.org/forum/topic/7278 – dqhendricks Mar 07 '13 at 22:07
  • 1
    Spacemanager will rotate the sprites, you'll rotate the sprites with CCRotateBy. My guess: one wins most of the time, but not always. My second guess is what Yves said. Check ccConfig and lower the average time for fps to 0.1 or even 0.01, that way you'll notice even single "missed frames" although only as a momentary flicker in the fps display. – CodeSmile Mar 07 '13 at 22:28
  • @LearnCocos2D what do you mean that spaceManager will rotate the sprites, don't I have to tell the sprites to rotate some how? – Stephen Mar 07 '13 at 22:45
  • @Stephen what exactly are you using a physics engine for if you are just going to use actions to move your sprites around? do you know what chipmunk is used for, or how it works? – dqhendricks Mar 08 '13 at 00:22
  • I am using chipmunk for collisions. I have been using chipmunk spaceManager for about a year. – Stephen Mar 08 '13 at 16:28
  • @LearnCocos2D I checked what you were saying about spacemanager and cocos2d calling set position, and you were right. How do I go about fixing this or what is your suggestion for moving the sprite that is attached to the shapes and body? – Stephen Mar 11 '13 at 18:08
  • let physics sprites be moved & rotated by physics. The only thing you could do, if chipmunk supports it, is to disable rotation of bodies and then rotate the sprite as you please. – CodeSmile Mar 11 '13 at 20:17

0 Answers0