在swift中编码精灵节点的连续X和Y运动的最佳方法是什么

时间:2016-03-20 21:06:52

标签: swift sprite-kit nodes nstimer

我正在创建我的第一个精灵套装游戏,我很难编码我的英雄运动。以下是我在单个视图应用程序中使用触摸向下和在IBactions内部修饰的目标c编码我的英雄运动的方式。我想在swift的sprite kit游戏中重复相同的结果。

-(IBAction)LeftArrowTapped:(id)sender; {

[self heroMovementTimerMethodLeft];

}
-(IBAction)RightArrowTapped:(id)sender; {

[self heroMovementTimerMethodRight];

}
-(IBAction)TouchEndedLeft:(id)sender; {

[heroMovementTimerLeft invalidate];

} 
-(IBAction)TouchEndedRight:(id)sender; {

[heroMovementTimerRight invalidate];

}
-(void)heroMovementTimerMethodLeft {

heroMovementTimerLeft = [NSTimer 
scheduledTimerWithTimeInterval:speedOfhero target:self 
selector:@selector(heroMovementLeft) userInfo:nil repeats:YES];
}
-(void)heroMovementTimerMethodRight {

heroMovementTimerRight = 
[NSTimerscheduledTimerWithTimeInterval:speedOfhero target:self 
selector:@selector(heroMovementRight) userInfo:nil repeats:YES];
}

-(void)heroMovementLeft{

hero.center = CGPointMake(hero.center.x -.5, hero.center.y );
}

-(void)heroMovementRight{

hero.center = CGPointMake(hero.center.x +.5, hero.center.y );
}

-(void)enemyMovementTimerMethod {

enemyMovementTimer =  
[NSTimerscheduledTimerWithTimeInterval:speedOfEnemy target:self 
selector:@selector(enemyMovement) userInfo:nil repeats:YES];

}

据我所知,在sprite kit中我将不得不用touchesBegan和touchesEnded方法替换我的IBactions,但我如何在swift中重复编码结果。这是我现在的尝试

let heroMovementLeft = SKAction.moveToX(self.size.width - 5,  
duration: 3)

但是我得到了一个错误,我也试过了

let heroMovementLeft = CGPointMake(hero.position.x -5 ,
hero.position.y)

我道歉我对swift和精灵工具包非常陌生

1 个答案:

答案 0 :(得分:0)

像你一样使用SKActions很好。 你实际上是在运行这个动作,因为你的代码看起来好像不是。

 let heroMovementLeft = SKAction.moveToX(self.size.width - 5,duration: 3)
 hero.runAction(heroMovementLeft) // This is missing in your code example