Search code examples
cocos2d-iphone

Leaves falling animation and dragging along with finger


I am new to Cocos2D, could any one please tell me how we can do leaves falling from top and settled down at bottom of the iPad, when ever user tilt the iPad or touches the leaves, the leaves should respond accordingly.It should be similar to alice wonderland lite application page number 13 animation. I am posting my code but However it doesn't behave exactly how I want. Whenever I touch near a sprite all the sprites move with incredible force.... they literally move out of the way of where I'm touching. Surely I'm not the only one who's implemented this before? Can anyone lead me in the right direction?

static float hw = 639.0/2.0, hh = 479.0/2.0;
static cpVect mouseToSpace(cpVect pos, CGSize size)
{
    cpVect v = cpvsub(cpvmult(cpv(pos.y/size.height, pos.x/size.width), 2.0f), cpv(1.0f, 1.0f));
    v = cpv(v.x*hw, v.y*hh);
    //  printf("%s\n", cpvstr(v));
    return v;
}

#define GRABABLE_MASK_BIT (1<<31)
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    cpVect point = mouseToSpace([myTouch locationInView:[myTouch view]], [myTouch view].bounds.size);
    mousePoint = point;

    cpShape *shape = cpSpacePointQueryFirst(space, point, GRABABLE_MASK_BIT, 0);

    if(shape) {
        cpBody *body = shape->body;
        mouseJoint = cpPivotJointNew2(mouseBody, body, cpvzero, cpBodyWorld2Local(body, point));
        mouseJoint->maxForce = 50000.0f;
        mouseJoint->biasCoef = 0.15f;
        cpSpaceAddConstraint(space, mouseJoint);
    }
}

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];  

    mousePoint = mouseToSpace([myTouch locationInView:[myTouch view]], [myTouch view].bounds.size);
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesCancelled:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    cpSpaceRemoveConstraint(space, mouseJoint);
    cpConstraintFree(mouseJoint);
    mouseJoint = nil;
}

Any one please guide me or post the code, i need it urgently.

Waiting for your valuable suggestions and any code.

Thanks in advance.

Shiva.


Solution

  • Cocos2d comes with projects to get you started. One of these is "Cocos2d with Box2D". This project starts you off with a scene that spawns a load of boxes at the top which fall to the bottom of the screen using the Box2D physics. If I remember correctly, it also shifts them around when you tilt.

    Look at that project and adapt it to suit your needs.

    This blog was my invaluable go-to resource when starting off with iPhone games development. I suggest you read it.