我正在使用SpriteKit并在xcode中使用场景编辑器绘制场景.根据SpriteKit,我们可以使用导航图来绘制路径,我可以使用导航图绘制路径,但我无法在swift后端访问此对象.
如何从场景中访问此导航图形对象.
我在Xcode中创建了一个具有相当长的路径的粒子发射器.当我在粒子生成器内移动它时,它会沿着我的鼠标路径留下一条线索.
关于我的目标的一些背景信息: 在我的spriteKit游戏中,用户在屏幕上拖动手指来拍摄移动的物体.我试图创建一个"子弹时间"效果,其中对象减速并在当前手指位置接触它们时突出显示.当手指停止移动或者弹药耗尽时,会触发touchesEnded方法拍摄所有突出显示的对象.目前我有他们旅行的路径显示为使用SKShapeNode和CGPath绘制到屏幕的线,但我希望使用发射器路径突出显示路径.
在触摸开始的方法中,我创建了一个圆形SpriteNode,它可以在手指移动到的任何地方移动(物理碰撞附加到圆而不是路径).我已经将粒子轨迹发射器连接到这个圆圈,当我在屏幕上移动它时它随着圆圈移动,但是没有留下痕迹.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
pathToDraw = CGPathCreateMutable();
startPoint = touchLocation;
CGPathMoveToPoint(pathToDraw, NULL, touchLocation.x, touchLocation.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.lineWidth = 2;
lineNode.zPosition = 110;
lineNode.strokeColor = [SKColor whiteColor];
[self addChild:lineNode];
circle = [SKSpriteNode spriteNodeWithTexture:[animationsAtlas textureNamed:@"anim_countdown_9"]];
circle.name = @"circle";
circle.scale = 0.3;
circle.alpha = 0.5;
circle.zPosition = 110;
circle.position = touchLocation;
circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
circle.physicsBody.categoryBitMask = weaponCategory;
circle.physicsBody.dynamic = NO; …Run Code Online (Sandbox Code Playgroud) 是否可以在变量中存储类类型,然后使用该变量将对象强制转换为该类类型?
我似乎能够将类类型存储在枚举中的变量中,但是我们无法使用此变量将新对象强制转换为此类型.
我有一个子类型的枚举
enum FruitType: Int {
case orange, apple, lemon, lime
var className: Fruit.Type {
switch self {
case.orange:
return Orange.self
case.apple:
return Apple.self
case.lemon:
return Lemon.self
case.lime:
return Lime.self
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有这些游戏对象的子类(橙色,苹果,柠檬和酸橙),它们都继承自"水果"
后来我试图使用该变量将对象强制转换为该类型,但它报告错误
错误"使用未声明类型'fruitType'
func createFruit(fruitType: FruitType, name: String) {
if let orange = self.childNode(withName: name) as? fruitType.className {
self.orange = orange
}
etc...
}
Run Code Online (Sandbox Code Playgroud)
我也尝试fruitType.className(self.childNode(withName: name))过相同的结果
我已经研究过泛型,但却找不到适合这种情况的东西.
编辑以显示实际的例子
简化了上述问题,使问题更容易说明.我如何处理这个的真正本质是我正在加载一个SKS场景文件作为基于fruitType的SKReferenceNode,我需要相应地转换该SKReferenceNode
func loadFruit(fruitType: FruitType) {
var fruit: Fruit!
if let fruitFile = SKReferenceNode(filename: fruitType.fileName) {
if …Run Code Online (Sandbox Code Playgroud) 我需要运行未知数量(来自字典数组)的 SKAction 序列,这些序列是在循环中动态创建和偏移的,延迟会随着我通过循环而增加。循环的打印输出应按以下顺序出现
show line 5
hiding everything
show line 6
hiding everything
show line 2
hiding everything
Run Code Online (Sandbox Code Playgroud)
要获得此功能,我使用此代码
func display(winningLines: [Int : AnyObject]) {
var delay = 0.0
var actions = [SKAction]()
let hideAction = SKAction.run {
print("hiding everything")
}
for winningLine in winningLines {
let displayAction = SKAction.run {
print("line \(winningLine.key)")
}
let wait = SKAction.wait(forDuration: delay)
var action: SKAction!
//repeatForever = true
if !repeatForever {
action = SKAction.sequence([wait, displayAction, SKAction.wait(forDuration: 1.0), hideAction])
}
else {
let seq …Run Code Online (Sandbox Code Playgroud)