Xcode4 如何移动&;box2d中的跳跃动画精灵

Xcode4 如何移动&;box2d中的跳跃动画精灵,xcode4,box2d-iphone,Xcode4,Box2d Iphone,在这段代码中,使用精灵表创建了一个动画精灵。精灵创造了身体。那个男孩可以用按钮在世界上移动 //spritesheet plist [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"boyRunning.plist"]; // Create a sprite sheet with the boyRunning images spriteSheet = [CC

在这段代码中,使用精灵表创建了一个动画精灵。精灵创造了身体。那个男孩可以用按钮在世界上移动

   //spritesheet plist
   [[CCSpriteFrameCache sharedSpriteFrameCache]
     addSpriteFramesWithFile:@"boyRunning.plist"];  


    // Create a sprite sheet with the boyRunning  images
    spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"boyRunning.png"];
    [self addChild:spriteSheet];

    // Load up the frames of our animation
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 2; i <=7; ++i) {
        [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"boyRunning%d.png", i]]];
    }
    walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];

    // Create a sprite for our boy
    CGSize winSize = [CCDirector sharedDirector].winSize;
    self.boy = [CCSprite spriteWithSpriteFrameName:@"boyRunning1.png"];        
    _boy.position = ccp(100, 90);
    self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    [_boy runAction:_walkAction];
    [spriteSheet addChild:_boy];
    // boy body creation
    b2BodyDef boyBodyDef;
    boyBodyDef.type = b2_dynamicBody;
    boyBodyDef.linearDamping = 1;
    boyBodyDef.angularDamping = 1;
    boyBodyDef.position.Set(230.0f/PTM_RATIO,(FLOOR_HEIGHT+91.0f)/PTM_RATIO);
    boyBodyDef.userData = _boy;
    boyBody = world->CreateBody(&boyBodyDef);

    b2Body* dynamicBody = world->CreateBody(&boyBodyDef);
    // Define another box shape for our dynamic body.
    b2PolygonShape boxshape;
    boxshape.SetAsBox(11.0f/PTM_RATIO, 30.0f/PTM_RATIO);//These are mid points for our 1m box
    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &boxshape;   
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    armFixture = boyBody->CreateFixture(&fixtureDef);
    dynamicBody->SetTransform( b2Vec2( 10, 20 ), 1 );
//spritesheet plist
[[CCSpriteFrameCache sharedSpriteFrameCache]
addSpriteFramesWithFile:@“boyRunning.plist”];
//创建一个包含男孩跑步图像的精灵表
spriteSheet=[CCSpriteBatchNode batchNodeWithFile:@“boyRunning.png”];
[自添加子项:精灵表];
//加载动画的帧
NSMutableArray*walkAnimFrames=[NSMutableArray];
对于(int i=2;i CreateBody(&boyBodyDef);
b2Body*dynamicBody=world->CreateBody(&boyboddef);
//为我们的动态身体定义另一个长方体形状。
B2polygonshapeboxshape;
SetAsBox(11.0f/PTM_比率,30.0f/PTM_比率);//这些是我们1m盒子的中点
//定义动态实体夹具。
b2FixtureDef fixtureDef;
fixtureDef.shape=&boxshape;
固定件密度=1.0f;
固定件摩擦力=0.3f;
armFixture=boyBody->CreateFixture(&fixtureDef);
dynamicBody->SetTransform(b2Vec2(10,20,1);

当按下按钮时

用于移动使用

body->ApplyForce( b2Vec2(0,50), bodiy]->GetWorldCenter() );
用于跳跃

body[0]->ApplyLinearImpulse( b2Vec2(50,50), body->GetWorldCenter() );
其中身体是你的身体连接到雪碧和b2ve2(x,y)是方向