【问题标题】:Cocos2d: Animation based on accelerometerCocos2d:基于加速度计的动画
【发布时间】:2011-12-08 11:14:33
【问题描述】:

任何人都知道那里有任何很好的最新教程,这些教程展示了如何根据加速度计运动为精灵制作动画。我想让一只鸟摇摆到设备指向的位置。例如,如果玩家决定通过加速度计将小鸟向左移动,我希望我的小鸟播放向左摇摆的动画。

// Accelerometer
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration   *)acceleration { 
birdSpeedY = 9.0 + acceleration.x*15;
birdSpeedX = -acceleration.y*20;
}

// Updating bird based on accelerometer
-(void)updateBird { 
float maxY = winSize.height - bird.contentSize.height/2;
float minY = bird.contentSize.height/2;
float newY = bird.position.y + birdSpeedY;
newY = MIN(MAX(newY, minY), maxY);

float maxX = winSize.width - bird.contentSize.width/2;
float minX = bird.contentSize.width/2;
float newX = bird.position.x + birdSpeedX;
newX = MIN(MAX(newX, minX), maxX);

bird.position = ccp(newX, newY);
}

// Making background scroll automatically 
-(void)update:(ccTime)dt { 
[self updateBird];

CGPoint backgroundScrollVel = ccp(-100, 0);
parallaxNode.position = ccpAdd(parallaxNode.position, ccpMult(backgroundScrollVel, dt));


}


-(id)init {
self = [super init];
if (self != nil) {
   winSize = [CCDirector sharedDirector].winSize;

    CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache addSpriteFramesWithFile:@"birdAtlas.plist"];

    NSMutableArray *framesArray=[NSMutableArray array];
    for (int i=1; i<10; i++) {
        NSString *frameName=[NSString stringWithFormat:@"bird%d.png", i];
        id frameObject=[cache spriteFrameByName:frameName];
        [framesArray addObject:frameObject];
    }
    // animation object
    id animObject=[CCAnimation animationWithFrames:framesArray delay:0.1];

    // animation action
    id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
    animAction=[CCRepeatForever actionWithAction:animAction];


    bird=[CCSprite spriteWithSpriteFrameName:@"bird1.png"];
    bird.position=ccp(60,160);



    CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"birdAtlas.png"];
    [self addChild:batchNode z:100];
    [batchNode addChild:bird];

    [bird runAction:animAction];

    self.isAccelerometerEnabled = YES; 
    [self scheduleUpdate]; 

    [self addScrollingBackgroundWithTileMapInsideParallax];
  }
  return self;
}

- (void) dealloc
{
[super dealloc];
}


@end

【问题讨论】:

    标签: animation cocos2d-iphone accelerometer


    【解决方案1】:

    您可以尝试使用加速度计方法并使用 ccp() 更改 Sprite 的位置。您还需要知道模式中的横向或纵向项目。 你可以试试下面的东西

    - (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
    {   
        [lbl setString:[NSString stringWithFormat:@"X=>%.2lf Y=>%.2lf",(double)acceleration.x,(double)acceleration.y]];
        double x1= -acceleration.y *10;
        double y1=  acceleration.x *15;
        if(acceleration.x >0.05)
        {
             y1*=spped_incr; // Make Movement Here
    
        }
        [Sprite_Name runAction:[CCMoveTo actionWithDuration:0.1f position:ccpAdd(ccp(x1,y1), Sprite_Name.position)]];
    }
    

    以上内容适用于横向模式....如果您需要纵向模式,则需要更改轴并使用 TRY & Error 方法。

    【讨论】:

    • 好的,谢谢,我会试试看,而且我的游戏将是横向的,所以我不需要改变任何东西。
    • 我真的很难过,请您帮我看看如何修改您的代码以适合我的代码。我在我提出问题的页面上方发布了一些代码,所以请检查一下。抱歉,如果这给您带来了困扰,我通常是编程新手,我很难理解某些事情是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多