根据不同显示器的大小调整SpriteKit视图中的节点大小

问题描述:

我想创建一个具有spriteKit视图的通用应用程序。
但iPad的显示屏比iPhone显示屏更大!根据不同显示器的大小调整SpriteKit视图中的节点大小

1)如何根据显示尺寸定位调整大小的节点?我的意思是当我为iPhone添加节点时,我不希望iPad上的这个节点的维度等于iPhone上的维度,我希望每个不同的屏幕具有不同大小的相同节点。

2)还有我怎样才能定位这个节点的不同显示?

感谢很多

首先设定了一个自动的保证金为您的应用程序:

var gameArea: CGRect 

override init(size: CGSize) { 

    let maxAspectRatio: CGFloat = 16.0/9.0 
    let playableWidth = size.height/maxAspectRatio 
    let margin = (size.width - playableWidth)/2 
    gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height) 

    super.init(size: size) 
} 

然后你就可以实现你的SKNode

player.physicsBody = SKPhysicsBody(polygonFrom: path) 
player.physicsBody!.affectedByGravity = false 
player.physicsBody!.categoryBitMask = PhysicsCategories.Player 
player.physicsBody!.collisionBitMask = PhysicsCategories.None 
player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy 
player.constraints = [SKConstraint.positionX(SKRange(lowerLimit: gameArea.minX + player.size.width/2, upperLimit: gameArea.maxX - player.size.width/2))]` 
+0

谢谢你,但我的意思是颜色不精灵节点。 –

+0

噢好吧,很遗憾,我不能帮你那个.. – 2017-10-10 18:07:08

+0

这是几乎一样的.aspectFit但有更多的工作 – Knight0fDragon

我想有型动物相同的节点尺寸为每个不同的 屏幕。

如果我知道你想什么,在这里我的答案:

你有两个选择:

1 - https://www.raywenderlich.com/49695/sprite-kit-tutorial-making-a-universal-app-part-1

2 - 编程调整:

// code in objc 

// defines global 
#define IS_IPAD  (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 

#define SCALE_FACTOR (IS_IPAD ? 2 : 1) // what ever you want 

// usage in the code 
sprite.size = CGSizeMake(width * SCALE_FACTOR, height * SCALE_FACTOR); 

// etc... 

编辑:

位置总是相对于父母。这里的定位点也很重要。这里举一个例子,如何添加一个标签直接到现场。标签位置是顶部中间

func addBestScoreLabel(bestScore: Int) { 
     let bestScoreLabel = SKLabelNode() 

     bestScoreLabel.text = "Best Score: " + String(bestScore) 
     bestScoreLabel.fontSize = 20 // what ever 
     bestScoreLabel.fontColor = .red // what ever 
     bestScoreLabel.zPosition = 10 // what ever 
     bestScoreLabel.position = CGPoint(x: self.frame.width/2, y: self.frame.height - bestScoreLabel.frame.height) 

     // add the label to the scene 
     self.addChild(bestScoreLabel) 
    } 

// call the function 
addBestScoreLabel(bestScore: 1234) 
+0

我必须把代码? –

+0

首先你创建你的精灵,对吧?之后,你给它一个大小。当你这样做时,你可以考虑取决于设备的比例因子。 – Yann

+0

你可以写代码吗?这是我的代码:bestScoreLabel.position = CGPoint(x:self.frame.width/256,y:self.frame.height/256 + self.bestScoreLabel.frame.width/3 - 20) bestScoreLabel.scale(to :CGSize(width:self.frame.width - 30,height:self.frame.width/2))非常感谢 –