我正在学习 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/