【问题标题】:how to create controls for a character in a game如何为游戏中的角色创建控件
【发布时间】:2015-02-01 10:32:48
【问题描述】:

我是一名初级程序员。我不明白如何在 swift 中为游戏中的角色创建控件。
游戏将包含按钮:左、右、跳跃和动作。
touchesBegan 玩累了,但是左键和右键在恒压下不工作。

【问题讨论】:

  • 考虑使用 CoreMotion 来实现角色的左右移动,并使用按钮来实现跳跃、射击等动作......
  • 需要在印刷机上创建控件,没有加速度计
  • 那么你唯一的选择就是按照 Jad 的建议去做,并使用 touches 方法。一个动作从 touchesBegan 开始并在 touchesEnded 停止。没有其他选择。
  • 好的。谢谢!我会试试看)

标签: ios swift sprite-kit


【解决方案1】:

您还必须实现touchesEnded。 执行此操作的简单方法是在 touchesBegan 中将标志设置为 true,在 touchesEnded 中设置为 false。这样,只要他的标志设置为 true (touchesBegan),您的循环就会运行,并在设置为 false (touchesEnded) 时停止。 希望这会有所帮助,如果您需要更多帮助,请告诉我。

【讨论】:

  • 感谢您的回复。但也许有更好的方法来控制角色?
  • 我目前不会用swift写代码,但是你可以在网上找到很多例子
  • var fingerIsOnLButton = false var actionL = SKAction() let distanceToMove = CGFloat(Character.position.x + 50.0) let actionLeft = SKAction.moveByX(-distanceToMove, y: 0, duration: 5) actionL = SKAction.sequence([actionLeft]) //Left let LButton = SKSpriteNode(imageNamed: "Left") LButton.name = LeftButton LButton.physicsBody = SKPhysicsBody(rectangleOfSize: LButton.frame.size) LButton.physicsBody?.dynamic = false LButton.position = CGPointMake(300, 600) LButton.size = CGSizeMake(50, 50) addChild(LButton)
  • 覆盖 func touchesBegan(touches: NSSet, withEvent event: UIEvent) { let touch = touches.anyObject() as UITouch let touchLocation = touch.locationInNode(self) let body:SKPhysicsBody? = self.physicsWorld.bodyAtPoint(touchLocation) if body?.node?.name == LeftButton { Character.runAction(actionL) fingerIsOnLButton = true } } 覆盖 func touchesEnded(touches: NSSet, withEvent event: UIEvent) { fingerIsOnLButton = false } }
  • 你能编辑你的问题并把你的代码贴在那里,这样我就可以清楚地看到了吗??
猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多