gpt4 book ai didi

swift - Xcode 6 swift : place 2 blocks on either side of the screen?

转载 作者:行者123 更新时间:2023-11-30 10:19:43 25 4
gpt4 key购买 nike

我需要在屏幕两侧的左上角和右上角放置 2 个 block 。这看起来很简单,我已经做了几次尝试,但这些 block 却不见踪影。我尝试使用 CGPointCGPointMake 并仅使用数字坐标,但 block 没有出现在 GameScene 中。

这些 block 稍后将向下移动。我在这里初始化一切:

class GameScene: SKScene {

var savior = SKSpriteNode()

var block1Texture = SKTexture()
var block2Texture = SKTexture()
var blocksMoveRemove = SKAction()
let blockGap = 150
var block1 = SKSpriteNode()
var block2 = SKSpriteNode()

这是我需要设置 block 位置的地方:

  //Create blocks

block1Texture = SKTexture(imageNamed: "block1")
block2Texture = SKTexture(imageNamed: "block2")
block1 = SKSpriteNode(texture: block1Texture)
block2 = SKSpriteNode(texture: block2Texture)

block1.setScale(1)
block2.setScale(1)
block1.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.1)
block2.position = CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.1)

self.addChild(block1)
self.addChild(block2)

最佳答案

您需要使用 addChild: 将 block 添加到您的节点.

更多备注:

如果您打算覆盖变量,则在声明变量时不必初始化它们。相反,请使用强制解包变量。

var block1 : SKSpriteNode!

如果您希望 block 位于角,则您的 y应该是高度乘以0.1 ,不是0.9 .

此外,您可能看不到任何内容,因为找不到图像(检查名称)或不包含可见元素。

另外,请注意 frame属性(property)可能具有意想不到的值(value)。来自文档:

The frame is the smallest rectangle that contains the node’s content, taking into account the node’s xScale, yScale, and zRotation properties. Not all nodes contain content of their own.

关于swift - Xcode 6 swift : place 2 blocks on either side of the screen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751142/

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