ios - SKAction moveTo 在应该下降时上升

标签 ios coordinates sprite-kit

我正在学习 SpriteKit 教程 here创建一个简单的 sprite 套件射击游戏,您可以在其中制作一艘向小行星发射激光的 spaceship 。

我想让激光(每个激光都是一个 SKSpriteNode)移动到我点击的位置。我在 touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法中正确获得触摸。但是,当我在 SKSpriteNode 上设置 SKAction 时,它会在我点击的位置的 y 方向上移动。即图像窗口的宽度 (x) 500 和高度 (y) 400。当我在坐标 (300, 100) 触摸屏幕时,激光似乎移动到坐标 (300, 300)。

我已验证 touchLocation 中的坐标是正确的。

仅供引用,我为此只使用了 iPhone 模拟器 - 但这应该无关紧要,不是吗?

相关代码片段:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    SKSpriteNode *shipLaser = [_shipLasers objectAtIndex:_nextShipLaser];

    shipLaser.position = CGPointMake(_ship.position.x+shipLaser.size.width,_ship.position.y+0);
    shipLaser.hidden = NO;
    [shipLaser removeAllActions];

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    SKAction *laserMoveAction = [SKAction moveTo:touchLocation duration:0.5];
    SKAction *laserDoneAction = [SKAction runBlock:(dispatch_block_t)^() {
        shipLaser.hidden = YES;
    }];

    SKAction *moveLaserActionWithDone = [SKAction sequence:@[laserMoveAction,laserDoneAction]];
    [shipLaser runAction:moveLaserActionWithDone withKey:@"laserFired"];
}

编辑:我想知道这是否与 SprikeKit 的坐标系起源于左下角,而 UIKit 起源于左上角这一事实有关??

最佳答案

你想要 SKScene 中的 touchLocation,而不是 UIView。

改变:

CGPoint touchLocation = [touch locationInView:self.view];

到:

CGPoint touchLocation = [touch locationInNode:self];

关于ios - SKAction moveTo 在应该下降时上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948027/

相关文章:

swift - 按下按钮时如何在 NSDefaults 中保存 bool 值?

ios - 使用位置管理器

javascript - jQuery window.one ("click") 无法在移动设备上运行

python : Mouse click coordinates as simply as possible

algorithm - 给定直线方程确定 "edge"点

ios - AdMob Interstitial 每五场比赛一次

ios - 使用团队供应配置文件的临时分发

ios - 如何在 UITableviewcells 中显示 HTML 颜色代码

javascript - 半正矢公式 - 结果过高

ios - iOS-以编程方式取消所有声音/音乐?