Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocos2d iphone 如何使用操纵杆位置运行动画?_Cocos2d Iphone_Joystick - Fatal编程技术网

Cocos2d iphone 如何使用操纵杆位置运行动画?

Cocos2d iphone 如何使用操纵杆位置运行动画?,cocos2d-iphone,joystick,Cocos2d Iphone,Joystick,这是我的代码…如何使用操纵杆位置运行动画。如果操纵杆完全拉伸,则动画显示速度很快。否则很慢 [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"walkcycle.plist"] ; spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"walkcycle.png"]; [heroWorldLayer addChild:spriteSheet]; f

这是我的代码…如何使用操纵杆位置运行动画。如果操纵杆完全拉伸,则动画显示速度很快。否则很慢

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"walkcycle.plist"] ;

spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"walkcycle.png"];

[heroWorldLayer addChild:spriteSheet];

float frameInt = 33.0f;

CCArray *jumpFrames = [CCArray arrayWithCapacity:33];
for(int i = 1; i <= 33; ++i) {
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Jump_Anim_V0200%02d.png", i]];
    [jumpFrames addObject:frame];
}
CCAnimation *jumpAnim = [CCAnimation animationWithFrames:[jumpFrames getNSArray] delay:1/frameInt];
CCFiniteTimeAction *jumpAnimate = [CCAnimate actionWithAnimation:jumpAnim restoreOriginalFrame:YES];
_jumpAction = [CCRepeat actionWithAction:jumpAnimate times:1];


NSMutableArray *runFrames = [NSMutableArray array];
for(int i = 1; i <= 11; ++i) {
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Run_Anim00%02d.png", i]];
    [runFrames addObject:frame];
}

CCAnimation *runAnim = [CCAnimation animationWithFrames:runFrames delay:1.0f/22.0f];
CCAnimate *runAnimate = [CCAnimate actionWithAnimation:runAnim restoreOriginalFrame:NO];
_runAction = [CCRepeatForever actionWithAction:runAnimate];


NSMutableArray *walkFrames = [NSMutableArray array];
for(int i = 1; i <= 8; ++i) {
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"walkcycle00%02d.png", i]];
    [walkFrames addObject:frame];
}

CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkFrames delay:(1-(controls.joyStickPos.x))/16.0f];
CCAnimate *walkAnimate = [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO];
_walkAction = [CCRepeatForever actionWithAction:walkAnimate];


if (controls.joyStickPos.x < 0) {
    heroBodySprite.flipX = YES;

} else {
    heroBodySprite.flipX = NO;
}

if(controls.joyStickPos.x ==0){ // joystick pos
        if (_actionState != kActionStateWalk || _actionState == kActionStateIdle) {
            [heroBodySprite stopAllActions];
            [heroBodySprite runAction:_runAction];
            _actionState  = kActionStateWalk;
        } else {
            _actionState = kActionStateIdle;
        }
} 

查看具有操纵杆/D-Pad功能的偷偷输入: 这为您完成了大部分工作

这不是确切的代码,但可能是这样的

if (joystick.position.x <= joystick.position.x - 20) {
//Play slow animation
}
else {
//Play fast animation
} 

你只需要做几个测试,并确定操纵杆的位置,请参见我的答案
if (joystick.position.x <= joystick.position.x - 20) {
//Play slow animation
}
else {
//Play fast animation
} 
int current_state = 0; // at class implementation time

if ((controls.joyStickPos.x >= 0.7 || controls.joyStickPos.x <= -0.7) && current_state != 1){
    current_state = 1;
    [self runAnimation];
} else {

}

if (((controls.joyStickPos.x > 0 && controls.joyStickPos.x < 0.7) || (controls.joyStickPos.x < 0 && controls.joyStickPos.x > -0.7)) && current_state != 2){
    current_state = 2;
    [self walkAnimation];
} else {

}

if (controls.joyStickPos.x == 0 && current_state != 0) {
    [heroBodySprite stopAllActions];
    [wheelSprite stopAllActions];
    current_state = 0;
} else {

}