gpt4 book ai didi

swift - 在 swift 中对 Sprite 节点的连续 X 和 Y 移动进行编码的最佳方法是什么

转载 作者:行者123 更新时间:2023-11-30 13:35:31 26 4
gpt4 key购买 nike

我正在创建我的第一个 Sprite 套件游戏,但在编写英雄的 Action 时遇到困难。以下是我如何在单一 View 应用程序中使用 IBactions 中的 touch down 和 touch up 在 Objective-C 中编码我的英雄的移动。我试图在 swift 的 Sprite 套件游戏中重复相同的结果。

-(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 工具包中,我必须将 IBactions 替换为 TouchBegan 和 TouchEnded 方法,但是我如何快速重复编码结果。这是我现在正在尝试的方法

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

但是我收到错误,我也尝试过

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

抱歉,我对 swift 和 sprite 套件很陌生

最佳答案

像您尝试的那样使用 SKActions 就可以了。您是否真的在运行该操作,因为从您的代码来看,您似乎没有运行。

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

关于swift - 在 swift 中对 Sprite 节点的连续 X 和 Y 移动进行编码的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36119620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com