Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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
Iphone 线性脉冲-Cocos2d/Objective-C/Box2d_Iphone_Objective C_Cocos2d Iphone_Box2d - Fatal编程技术网

Iphone 线性脉冲-Cocos2d/Objective-C/Box2d

Iphone 线性脉冲-Cocos2d/Objective-C/Box2d,iphone,objective-c,cocos2d-iphone,box2d,Iphone,Objective C,Cocos2d Iphone,Box2d,我有一门大炮,一个球和一个扳机。当你按下扳机时,一个线性脉冲被施加到球上 short int direction = [level.cannon cannon].rotation; short int power = 24.8; b2Vec2 force = b2Vec2(direction, power); [level.ball body]->ApplyLinearImpulse(force, [level.ball body]->GetWorldCenter()); 我的问

我有一门大炮,一个球和一个扳机。当你按下扳机时,一个线性脉冲被施加到球上

short int direction = [level.cannon cannon].rotation;

short int power = 24.8;
b2Vec2 force = b2Vec2(direction, power);
[level.ball body]->ApplyLinearImpulse(force, [level.ball body]->GetWorldCenter());
我的问题是,当按下扳机时,线性脉冲被施加到球上,但球实际上并没有从加农炮精灵的顶部出来

它这样做的原因(我想)是因为我已经将大炮的锚点设置为(0.5,0)

我已经计算出,大炮每旋转5度,需要加上/减去2的倍数

例如

0到55度

0到5从旋转中减去2 5到10从旋转中减去4 10到15从旋转中减去6 等等

对于0到-55度

0到-5将2添加到旋转 -5到-10将4添加到旋转中 -10至-15向旋转方向添加6

目前,我正在使用这段代码来完成这项工作

if (_cannon.rotation == 0)
    {
        direction = _cannon.rotation;
    } else if (_cannon.rotation >= 1 && _cannon.rotation < 6)
    {
        direction = _cannon.rotation - 2;
    } else if (_cannon.rotation >= 6 && _cannon.rotation < 11)
    {
        direction = _cannon.rotation - 4;
    } else if (_cannon.rotation >= 11 && _cannon.rotation < 16)
    {
        direction = _cannon.rotation - 6;
    } else if (_cannon.rotation >= 16 && _cannon.rotation < 21)
    {
        direction = _cannon.rotation - 8;
    } else if (_cannon.rotation >= 21 && _cannon.rotation < 26)
    {
        direction = _cannon.rotation - 10;
    } else if (_cannon.rotation >= 26 && _cannon.rotation < 31)
    {
        direction = _cannon.rotation - 12;
    } else if (_cannon.rotation >= 31 && _cannon.rotation < 36)
    {
        direction = _cannon.rotation - 14;
    } else if (_cannon.rotation >= 36 && _cannon.rotation < 41)
    {
        direction = _cannon.rotation - 16;
    } else if (_cannon.rotation >= 41 && _cannon.rotation < 46)
    {
        direction = _cannon.rotation - 18;
    } else if (_cannon.rotation >= 46 && _cannon.rotation < 55)
    {
        direction = _cannon.rotation - 20;
    } else if (_cannon.rotation <= -1 && _cannon.rotation > -6)
    {
        direction = _cannon.rotation + 2;
    } else if (_cannon.rotation <= -6 && _cannon.rotation > -11)
    {
        direction = _cannon.rotation + 4;
    } else if (_cannon.rotation <= -11 && _cannon.rotation > -16)
    {
        direction = _cannon.rotation + 6;
    } else if (_cannon.rotation <= -16 && _cannon.rotation > -21)
    {
        direction = _cannon.rotation + 8;
    } else if (_cannon.rotation <= -21 && _cannon.rotation > -26)
    {
        direction = _cannon.rotation + 10;
    } else if (_cannon.rotation <= -26 && _cannon.rotation > -31)
    {
        direction = _cannon.rotation + 12;
    } else if (_cannon.rotation <= -31 && _cannon.rotation > -36)
    {
        direction = _cannon.rotation + 14;
    } else if (_cannon.rotation <= -36 && _cannon.rotation > -41)
    {
        direction = _cannon.rotation + 16;
    } else if (_cannon.rotation <= -41 && _cannon.rotation > -46)
    {
        direction = _cannon.rotation + 18;
    } else if (_cannon.rotation <= -46 && _cannon.rotation > -55)
    {
        direction = _cannon.rotation + 20;
    }
if(_.rotation==0)
{
方向=旋转;
}否则如果(_cannon.rotation>=1&&u cannon.rotation<6)
{
方向=_.carnon旋转-2;
}否则如果(_cannon.rotation>=6&&u cannon.rotation<11)
{
方向=_.carnon旋转-4;
}否则如果(_cannon.rotation>=11&&u cannon.rotation<16)
{
方向=_.carnon旋转-6;
}否则如果(_cannon.rotation>=16&&u cannon.rotation<21)
{
方向=_.carnon旋转-8;
}否则如果(_cannon.rotation>=21&&u cannon.rotation<26)
{
方向=_.carnon旋转-10;
}否则如果(_cannon.rotation>=26&&u cannon.rotation<31)
{
方向=_.carnon旋转-12;
}否则如果(_cannon.rotation>=31&&u cannon.rotation<36)
{
方向=_.carnon旋转-14;
}否则如果(_cannon.rotation>=36&&u cannon.rotation<41)
{
方向=_.carnon旋转-16;
}否则如果(_cannon.rotation>=41&&u cannon.rotation<46)
{
方向=_.carnon旋转-18;
}否则如果(_cannon.rotation>=46&&u cannon.rotation<55)
{
方向=_.carnon旋转-20;
}否则如果(_.carnon.rotation-6)
{
方向=_.旋转+2;
}否则如果(_.carnon.rotation-11)
{
方向=_.旋转+4;
}否则如果(_.carnon.旋转-16)
{
方向=_.旋转+6;
}否则如果(_.carnon.rotation-21)
{
方向=_.旋转+8;
}否则,如果(加农炮旋转-26)
{
方向=_.旋转+10;
}否则如果(_.旋转-31)
{
方向=_.旋转+12;
}否则如果(_.carnon.旋转-36)
{
方向=_.旋转+14;
}否则如果(_.carnon.rotation-41)
{
方向=_.旋转+16;
}否则如果(_.旋转-46)
{
方向=_.旋转+18;
}否则如果(_.旋转-55)
{
方向=_.旋转+20;
}
我知道必须有一个更简单的方法来做到这一点,但我仍在学习。有人能帮我吗

 short int val, val2;
 val = _cannon.rotation/5;
 val2 = _cannon.rotation%5; //remainder of the division
                            //it will be zero when rotation is a multiple of 5. 
 if(val2 > 0)
    val += 1;
 direction = _cannon.rotation - 2*val;
我不确定这是否是你想要的,我没有测试它。只是做了一些心理测试,看看它是否适用于你提出的案例


但我希望您可以将此作为使代码更小的起点。

lol。。。哇,伙计。非常感谢你。我不敢相信(大约)40行代码竟然变成了6行。这对于正整数非常有效,但是我如何解释负整数呢?我是个白痴。在我看到你的答案之前,我从未尝试过将旋转除以2。我所要做的就是int r=cannon.rotation/2,这适用于正整数和负整数我认为这适用于负整数,你只需要检查val2的绝对值。like:if(abs(val2)>0)
 short int val, val2;
 val = _cannon.rotation/5;
 val2 = _cannon.rotation%5; //remainder of the division
                            //it will be zero when rotation is a multiple of 5. 
 if(val2 > 0)
    val += 1;
 direction = _cannon.rotation - 2*val;