为了学习,我必须在iOS上创建游戏(例如太空入侵者)。实际上,我对游戏领域感到困惑:无论形状因素如何,我都需要将子弹从x位置的最大值拉出。 确实,我的代码可以在旧的外形尺寸(如iPhone 8)上完美运行。但是,在新的外形尺寸(iPhone X,XS ..)上,行星可以在所有屏幕上消失。
(我是Swift的初学者,所以我知道我的代码中有很多错误)。但是我认为问题出在我除以一个整数这一事实。
第一张照片:我想要的(在iPhone 8上可用) 第二个:我的问题。
感谢您的帮助(对不起,我英语不好,法国人啊哈哈)。
翻译:
zone_de_jeu =游戏区
largeur_jouable = width_playable
marge_largeur = width_margin
hauteur_jouable = height_playable
marge_hauteur = height_margin
在此处输入代码
var zone_de_jeu: CGRect
override init(size: CGSize) {
let max_ratio_aspect: CGFloat = 16.0 / 9.0
let largeur_jouable = size.height / max_ratio_aspect
let marge_largeur = (size.width - largeur_jouable) / 2
let hauteur_jouable = size.width / max_ratio_aspect
let marge_hauteur = (size.height - hauteur_jouable) / 2
zone_de_jeu = CGRect(x: marge_largeur, y: marge_hauteur, width: largeur_jouable, height: hauteur_jouable)
super.init(size: size)
let initialisation_mouvement_planete = touch.location(in: self)
let mouvement_planete = touch.previousLocation(in: self)
let arrive_final_planete_x = initialisation_mouvement_planete.x - mouvement_planete.x
planete.position.x += arrive_final_planete_x
if planete.position.x > zone_de_jeu.maxX - planete.size.width / 6.5 {
planete.position.x = zone_de_jeu.maxX - planete.size.width / 6.5
}
if planete.position.x < zone_de_jeu.minX + planete.size.width / 6.5 {
planete.position.x = zone_de_jeu.minX + planete.size.width / 6.5
}
我应该用更一般的方式修改除数吗?