我正在關注SpriteKit教程here,以創建一個簡單的精靈套件射擊遊戲,您可以製作一艘太空飛船,在小行星上發射激光。SKAction moveTo應該降低
我想讓激光(每個激光是一個SKSpriteNode)移動到點擊的位置。我在方法touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
內正確地獲取了觸摸。然而,當我在SKSpriteNode上設置一個SKAction
時,它會在y方向的OPPOSITE上移動,我點擊它。即圖像窗口的寬度(x)500和高度(y)400.當我觸摸屏幕的座標(300,100)時,lazer似乎移動到座標(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中從左上角的起源?
爲什麼'_ship.position.y + 0'?另外,shipLaser的父母是什麼? – prototypical
'+ 0'並不重要,它是從教程中複製粘貼代碼的結果。 'shipLaser'的父級是視圖控制器 - 在init_方法內,我在'_shipLasers'數組中的每個對象上調用'[self addChild:shipLaser]' –
您是不是將激光添加到SKScene? – prototypical