改变火灾方向

时间:2013-12-01 04:27:27

标签: ios objective-c sprite-kit

我做了一个游戏,现在我无法弄清楚如何在你点击而不是直接前进的地方进行拍摄。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
//1
SKSpriteNode *shipLaser = [_shipLasers objectAtIndex:_nextShipLaser];  
_nextShipLaser++;
if (_nextShipLaser >= _shipLasers.count) {
    _nextShipLaser = 0;
}

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

//3
CGPoint location = CGPointMake(self.frame.size.width, _ship.position.y);
SKAction *laserMoveAction = [SKAction moveTo:location duration:0.5];  
//4
SKAction *laserDoneAction = [SKAction runBlock:(dispatch_block_t)^() {
    //NSLog(@"Animation Completed");
    shipLaser.hidden = YES;
}];

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

2 个答案:

答案 0 :(得分:1)

您想要做的是在视图中找到触摸的位置。所以你需要将它添加到你的touchesBegan方法

 for(UITouch *t in touches) {
    CGPoint point = [t locationInView:<view>];

    // for use in Sprite Kit
    CGPoint point = [t locationInNode:<node>];
    // make the x position of the object being shot
    // equal to the point location
 }

希望有所帮助

答案 1 :(得分:0)

您只需要替换此行

CGPoint location = CGPointMake(self.frame.size.width, _ship.position.y);

由此

CGPoint location = [[touches anyObject] locationInNode:self];