我找到一些代码可以帮助我创建一个按钮,按下它时可以动画。但它是客观的。一般来说,我可以理解这段代码是什么,但我无法在swift上正确翻译它。请帮我翻译一下。非常感谢。
1
- (SKSpriteNode *)fireButtonNode
{
SKSpriteNode *fireNode = [SKSpriteNode spriteNodeWithImageNamed:@"fireButton.png"];
fireNode.position = CGPointMake(fireButtonX,fireButtonY);
fireNode.name = @"fireButtonNode";
fireNode.zPosition = 1.0;
return fireNode;
}
2
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"fireButtonNode"]) {
}
}
答案 0 :(得分:0)
在斯威夫特:
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace PhotoManagement.Converters
{
public class StartPointConverter : IValueConverter
{
[Obsolete]
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double && ((double)value > 0.0))
{
return new Point((double)value / 2, 0);
}
return new Point();
}
[Obsolete]
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
}
请注意,变量func getFireButtonNode()->SKSpriteNode{
let fireButtonX = CGRectGetMidX(frame), fireButtonY = CGRectGetMidY(frame)
let fireNode = SKSpriteNode(imageNamed: "fireButton")
fireNode.position = CGPoint(x: fireButtonX, y: fireButtonY)
fireNode.name = "fireButtonNode"
fireNode.zPosition = 1.0
return fireNode
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location )
if node.name == "fireButtonNode" {
//do your stuff here
}
}
}
和fireButtonX
永远不会在您的代码示例中初始化。您可以像{I}一样在fireButtonY
中定义它们,也可以在调用getFireButtonNode
方法时将它们作为参数传递,如下所示:
getFireButtonNode
并称之为:
func getFireButtonNode(xPos:CGFloat, _ yPos:CGFloat)->SKSpriteNode{}
如果你想这样称呼它:
getFireButtonNode(100,100)
您必须明确定义外部和本地参数名称,如下所示:
getFireButtonNode(xPos:100, yPos:100)
其中xPos(以及yPos)是外部参数名称,当您调用该函数时,它将用作参数名称。 x和y是局部参数,您可以在函数体内使用它们。
答案 1 :(得分:0)
你看过苹果演示项目“DemoBots”吗?他们有按钮帮助。
另一个更简单的例子
http://nathandemick.com/programming/tutorial/2014/09/23/buttons-sprite-kit-using-swift.html
最后是一个gitHub项目。
https://github.com/nguyenpham/sgbutton
在我的个人游戏中,我使用apple demo bot示例,并进行了一些与我相关的调整。这个课程也有助于tvO聚焦。