gpt4 book ai didi

iPhone 5 与 iPhone 4 屏幕尺寸对比

转载 作者:行者123 更新时间:2023-11-28 22:40:53 24 4
gpt4 key购买 nike

我一直在为 iPhone 开发一个 cocos2d 应用程序。我一直在我的 iPhone 5 上单独测试它。它上面的一切看起来都很棒,但是当我在 3.5 英寸模拟器 iPhone 上测试它时,一些按钮不见了,一些标签放错了地方。

我想知道为什么会这样?我原以为一切都通过 cocos2d 网格定位,然后将其转换为像素,所以无论屏幕尺寸如何,布局看起来都应该相同。

我认为问题的部分原因可能是当我设置菜单项的位置时,(0,0) 将其置于屏幕中央。我不知道这是为什么。

对正在发生的事情有什么建议吗?

左边是 iPhone 5,左边是模拟器(3.5 英寸)。

enter image description here

这是此屏幕的代码:

    CCSprite *title = [CCSprite spriteWithSpriteFrameName:@"highscoresTitle.png"];
[self addChild:title];
title.position = ccp([CCDirector sharedDirector].winSize.width/2, [CCDirector sharedDirector].winSize.height-title.contentSize.height-15);
background.position = ccp([CCDirector sharedDirector].winSize.width/2, [CCDirector sharedDirector].winSize.height/2);
play = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"playNew.png"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"playNew.png"] target:self selector:@selector(playScene:)];
play.position = ccp(0, -200);

CCLabelTTF *backLabel = [CCLabelTTF labelWithString:@"BACK" fontName:@"RBNo2-Light-Alternative" fontSize:25];
CCMenuItemLabel *goBack = [CCMenuItemLabel itemWithLabel:backLabel target:self selector:@selector(back:)];
goBack.position = ccp(0, -260);

CCMenuItemSprite *gameicon = [CCMenuItemSprite itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:@"gameCenter.png"] selectedSprite:[CCSprite spriteWithSpriteFrameName:@"gameCenter.png"] target:self selector:@selector(gameIcon:)];
gameicon.position = ccp(0, -150);

CCMenu *menu = [CCMenu menuWithItems:play,goBack, gameicon, nil];
[self addChild:menu z:0];

for(int i = 0; i<5 && i<length; i++){

NSString *temp = [NSString stringWithFormat:@"%d: %d", i+1,[[highscores objectAtIndex:i]intValue]];
CCLabelTTF *label = [CCLabelTTF labelWithString:temp fontName:@"RBNo2-Light-Alternative" fontSize:20];
label.position = ccp([CCDirector sharedDirector].winSize.width/2, 350-i*30);
label.visible = YES;
[self addChild:label z:100 tag:1];

}

NSString *temp = [NSString stringWithFormat:@"Last Score: %d", newHighscore];
CCLabelTTF *label = [CCLabelTTF labelWithString:temp fontName:@"RBNo2-Light-Alternative" fontSize:20];
label.position = ccp([CCDirector sharedDirector].winSize.width/2, 415);
label.visible = YES;
[self addChild:label z:100 tag:1];

这是另一个带有 slider 的示例:(iPhone 5 左侧) enter image description here

这是 slider 的代码:

    sliderCtl = [[UISlider alloc]initWithFrame:CGRectMake(100, 460, 200, 0)];
//[sliderCtl.layer setAffineTransform:CGAffineTransformMakeRotation(3.141/2)];
[sliderCtl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

sliderCtl.backgroundColor = [UIColor clearColor];
sliderCtl.value = 1.0;

[[[[CCDirector sharedDirector] openGLView] window] addSubview:sliderCtl];

sliderEff = [[UISlider alloc]initWithFrame:CGRectMake(100, 402, 200, 0)];

[sliderEff addTarget:self action:@selector(effectsSlider:) forControlEvents:UIControlEventValueChanged];

sliderEff.backgroundColor = [UIColor clearColor];
sliderEff.value = 1.0;

[[[[CCDirector sharedDirector] openGLView] window] addSubview:sliderEff];

非常感谢。

最佳答案

这个答案很可能会让您失望。 iPhone 5 的长边有许多像素,这与之前的所有机型都不一样。用于在场景中放置对象的坐标系是点,在配备 Retina 显示屏的设备上,1 pt 等于 2 px。

您确实可以使用 CCDirector 提供给您的 winSize。这确实可以帮助您计算相对位置,例如为了像我在代码的某些地方看到的那样沿水平居中,例如用于标签。在 anchorPointwinSize 和相关属性的帮助下,您一个人负责定位对象。

具体例子:你把goBack放在-260pt的相对位置。请注意,默认情况下,您的菜单 位于屏幕中央。在 iPhone 4S 和之前的所有型号上,这意味着绝对位置为 480 pt/2 - 260 pt = -20 pt。按钮不在屏幕上也就不足为奇了。您必须测试 winSize.height 并进行相应调整。

关于iPhone 5 与 iPhone 4 屏幕尺寸对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542574/

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