2013-07-15 60 views
0

我嘗試機會TE精靈當我移動我的精靈,IK就剩上下工作,但不正確的。這裏是我的代碼:如何機會精靈偷偷摸摸的操縱桿(使用Dpad)

if (dpad.degrees > 0 && dpad.degrees < 59) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerrigt.png"]]; 
    NSLog(@"going rigt"); 
} 
else if (dpad.degrees > 331 && dpad.degrees < 360) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerrigt.png"]]; 
    NSLog(@"going rigt"); 
} 
else if (dpad.degrees > 151 && dpad.degrees < 240) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerleft.png"]]; 
    NSLog(@"going left"); 
} 
if (dpad.degrees > 60 && dpad.degrees < 150) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerup.png"]]; 
    NSLog(@"going up"); 
} 
else if (dpad.degrees > 241 && dpad.degrees < 330) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerdown.png"]]; 
    NSLog(@"going down"); 
} 

是的,我知道我speld錯了XD。

thx for ur time。

+0

不知道,但你必須「失蹤」的範圍在那裏..(即如果dpad.degrees == 0或> = 59 && <= 60等)。也許這是你的問題?如果「正確」是零度,那麼它目前沒有被任何案例捕獲。 – Mark

+0

一些如何,如果我將它設置== 0它運行的代碼嗲我不碰DPAD。我嘗試過所有的範圍 – user2584968

回答

0

如果通過做出不同的想象來擺脫它。 首先我增加了一個新CGPoint

CGPoint oldPosition = ccp(player.position.x, player.position.y); 

然後我用在新舊位置差

if (oldPosition.x < newPosition.x) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerrigt.png"]]; 
    NSLog(@"going rigt"); 
} 
else if (oldPosition.x > newPosition.x) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerleft.png"]]; 
    NSLog(@"going left"); 
} 
if (oldPosition.y < newPosition.y) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerup.png"]]; 
    NSLog(@"going up"); 
} 
else if (oldPosition.y > newPosition.y) { 
    [player setTexture:[[CCTextureCache sharedTextureCache] addImage:@"playerdown.png"]]; 
    NSLog(@"going down"); 
} 

希望它可以幫助誰的人有同樣的問題

0

首先,操縱桿設爲DPad類型和4個方向

joystick.isDPad=YES; 
    joystick.numberOfDirections=4; 

更新中:

CGPoint velocity = ccpMult(joystick.velocity, 100); 
    if(velocity.x!=0||velocity.y!=0) 
    if(abs(velocity.x)>50&&abs(velocity.y)>50) 
    { 
     return; 
    } 
    else if(velocity.x < -50) 
    { 
     //code for go left here 
     return; 
    } 
    else if(velocity.x > 50) 
{ 
    //code for go right here 
    return; 
} 

    else if(velocity.y > 50) 
    { 
     //code for go up here 
     return; 
    }else if(velocity.y < -50) 
    { 
     //code for go down here 
     return; 
    }