如何机会精灵偷偷摸摸的操纵杆(Dpad)

时间:2013-07-15 20:31:58

标签: ios cocos2d-iphone

当我移动我的精灵时,我试图机会te精灵,我上下左右工作,但不正确。这是我的代码:

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");
}

是的,我知道我说错了XD。

这是你的时间。

2 个答案:

答案 0 :(得分: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");
}

希望它可以帮助那些有同样问题的人

答案 1 :(得分: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;
    }