【问题标题】:Problems with offsets in portrait mode Cocos2dCocos2d纵向模式下的偏移问题
【发布时间】:2013-04-04 21:14:11
【问题描述】:

我正在根据 Ray Wenderlich 的 Cocos 2d 中旋转炮塔教程进行工作(请参阅此处:http://www.raywenderlich.com/25791/rotating-turrets-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-2)。我需要我的游戏处于纵向模式,这样我才能正确地获得炮塔的位置:

炮塔设法向右射击,但没有向左射击。这是我的代码:

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (_nextProjectile != nil) return;

// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace:touch];

// Set up initial location of projectile
CGSize winSize = [[CCDirector sharedDirector] winSize];
_nextProjectile = [[CCSprite spriteWithFile:@"projectile2.png"] retain];
_nextProjectile.position = ccp(160, 20);

// Determine offset of location to projectile
CGPoint offset = ccpSub(location, _nextProjectile.position);

// Bail out if you are shooting down or backwards
if (offset.x <= 0) return;

// Determine where you wish to shoot the projectile to
int realX = winSize.width + (_nextProjectile.contentSize.width/2);
float ratio = (float) offset.y / (float) offset.x;
int realY = (realX * ratio) + _nextProjectile.position.y;
CGPoint realDest = ccp(realX, realY);

// Determine the length of how far you're shooting
int offRealX = realX - _nextProjectile.position.x;
int offRealY = realY - _nextProjectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;

// Determine angle to face
float angleRadians = atanf((float)offRealY / (float)offRealX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * angleDegrees;
float rotateDegreesPerSecond = 180 / 0.5; // Would take 0.5 seconds to rotate 180 degrees, or half a circle
float degreesDiff = _player.rotation - cocosAngle;
float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond);
[_player runAction:
 [CCSequence actions:
  [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
  [CCCallBlock actionWithBlock:^{
     // OK to add now - rotation is finished!
     [self addChild:_nextProjectile];
     [_projectiles addObject:_nextProjectile];

     // Release
     [_nextProjectile release];
     _nextProjectile = nil;
 }],
  nil]];

// Move projectile to actual endpoint
[_nextProjectile runAction:
 [CCSequence actions:
  [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
  [CCCallBlockN actionWithBlock:^(CCNode *node) {
     [_projectiles removeObject:node];
     [node removeFromParentAndCleanup:YES];
 }],
  nil]];

_nextProjectile.tag = 2;

}

感谢您的帮助!

【问题讨论】:

    标签: ios objective-c cocos2d-iphone


    【解决方案1】:

    您正在检查 x 轴而不是 Y

    // Bail out if you are shooting down or backwards
    if (offset.x <= 0) return
    

    ;

    【讨论】:

    • 如果我改变这个,炮塔朝下然后射击而不是在我点击的地方射击。
    • 尝试反转 y 坐标 - 不像其他一些布局 cocos2d 在底部有 y0
    【解决方案2】:

    您是否真的将应用程序设置为以纵向模式运行,或者您只是旋转了模拟器并重新定位了炮塔?

    如果您没有明确将应用程序设置为纵向运行,您的 x 和 y 坐标将被交换(x 将从 ios 按钮运行到手机顶部,而不是像您预期的那样交叉)。

    如果转换正确I have answered this question before :)

    【讨论】:

    • 您好。我从您的链接答案中使用了您的答案,效果很好,除非您直接向上射击,弹丸的速度比其他弹丸的速度快得多。 +50 的任何想法?
    • 你需要看到 realMoveDuration 直线上升时的值。这是弹丸的速度,它是通过与弹丸终点的距离来计算的。 NSLog out 长度变量和 realMoveDuration 你应该发现问题
    【解决方案3】:

    这里的问题是您复制粘贴了数学,而不是根据您的目的正确编辑它。 Ray 的代码中有一些假设,依赖于你总是在炮塔的右侧而不是向上、向下或向左射击。

    这是您应该查看的数学代码:

    // Determine offset of location to projectile
    CGPoint offset = ccpSub(location, _nextProjectile.position);
    
    // Bail out if you are shooting down or backwards
    if (offset.x <= 0) return;
    

    请注意,如果点击位置在炮塔左侧,则 offset.x 将小于 0,因此这是您从 Ray 那里得到的假设,但没有修改。正如 gheesse 所说,出于您的目的,这应该设置为 offset.y,因为您不希望它们在射弹的原始位置以南射击。但这只是问题的一部分。

    // Determine where you wish to shoot the projectile to
    int realX = winSize.width + (_nextProjectile.contentSize.width/2);
    

    这是您的另一个大问题。你没有修改 Ray 的数学来确定射弹应该去哪里。在 Ray 的代码中,他的射弹总是会落在屏幕右侧之外的位置,因此他使用屏幕的宽度和射弹的大小来确定他想要射弹的真实位置。这导致了你的问题,因为你没有假设你的弹丸总是朝右 - 你的弹丸总是会向上(提示,类似的代码应该用于你的真实)

    float ratio = (float) offset.y / (float) offset.x;
    int realY = (realX * ratio) + _nextProjectile.position.y;
    

    再一次,Ray 在他的游戏数学中做出了假设,而你实际上并没有更正它。您的代码使炮塔转动的方式会影响 realX 坐标而不是 realY,这是 Ray 总是射击右炮塔需要影响的坐标。

    你需要坐下来重新计算一下。

    【讨论】:

    • 很抱歉您发现我的代码完全是垃圾。但我试图扭转坐标,但我发现什么都没有。我认为如果你从头开始会很好,这样人们就可以指出要在哪些领域工作。我所需要的只是有人为我反转坐标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多