简体   繁体   English

如何使用Swift代码在SpriteKit上放置按钮?

[英]How to put buttons on SpriteKit with Swift code?

I tried to make a game on Xcode 7 Beta, I almost finished it but the problem is I don't know how to put buttons with Swift code, I tried to put them on Main.storyboard but they don't arrange very well. 我试图在Xcode 7 Beta上制作游戏,但我几乎完成了游戏,但是问题是我不知道如何使用Swift代码放置按钮,我试图将它们放置在Main.storyboard上,但是它们的排列不太好。 some of them become bigger and some smaller etc... So I want to know how to put these buttons with code as in picture below. 其中一些变得更大而一些变得更小等等。所以我想知道如何用下面的代码放置这些按钮。 Blue button first from the left is Leaderboard, then Facebook share, twitter share, and another default button (maybe it will be Remove Ads) 左侧的蓝色按钮首先是排行榜,然后是Facebook分享,Twitter分享和另一个默认按钮(可能是“删除广告”)

在此处输入图片说明

Your buttons should be .png files in the images.xcassets (you can add a new image set by clicking the + at the bottom). 您的按钮应该是images.xcassets中的.png文件(您可以通过单击底部的+添加新的图像集)。 It will automatically choose the right size depending on the device/simulator. 它将根据设备/模拟器自动选择合适的尺寸。 Then you create a variable to store your node like this: 然后创建一个变量来存储节点,如下所示:

let myNode = SKSpriteNode(imageNamed: "yourImage")

Then you can position the node multiple ways but i'll show you what I think will work best for you. 然后,您可以以多种方式定位节点,但我将向您展示我认为最适合您的内容。 Use myNode.position = CGPointMake(CGRectGetMaxX(self.frame) *0.40, CGRectGetMaxY(self.frame) * 0.25) This will get the max x and y values of the screen, multiply them by .4 and .25 respectively and then you can add the node to the scene will this line: 使用myNode.position = CGPointMake(CGRectGetMaxX(self.frame) *0.40, CGRectGetMaxY(self.frame) * 0.25)这将获得屏幕的最大x和y值,分别乘以.4和.25,然后得出可以将节点添加到场景中这一行:

self.addChild(myNode)

Oh, and you can multiply the values by whatever decimal you want to get the nodes in the exact spot. 哦,您可以将值乘以想要获得准确位置上的节点的任何十进制数。 IF you want them to be side by side you can even do something like this: myNode.position = CGPointMake(otherNode.position.x + myNode.frame.size.width/2, CGRectGetMaxY(self.frame) * 0.25) 如果您希望它们并排,甚至可以执行以下操作: myNode.position = CGPointMake(otherNode.position.x + myNode.frame.size.width/2, CGRectGetMaxY(self.frame) * 0.25)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM