【问题标题】:"BackgroundMoving" Code is not Working in iPhone4S“背景移动”代码在 iPhone 4S 中不起作用
【发布时间】:2012-03-10 11:39:50
【问题描述】:

我正在制作一款游戏,当我在设备(iPhone4)上测试我的应用程序时,它的背景无法正常工作。就像背景图像结束时黑色背景颜色来了。我的背景图片尺寸为 460X2880,图片名称为“mars_sample.jpg”(适用于 iPhone4),背景移动的代码为:

-(void)backgroundmoving:(ccTime)dt 
{     
    static float bk_f=0.0f;

    bk_f -=0.9;
    if (bk_f <= -960*3) {bk_f=0;}
    background.position = ccp( 0,bk_f);    
}  

当我将“460X2880”大小的图像加载为背景图像时,它不会在后台加载,如果它的大小类似于“460X1192”,它会在背景中加载图像,但在屏幕顶部显示黑色背景颜色。

下面一个代码用于设置背景图片:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    background = [CCSprite spriteWithFile:@"staripad.png"];
    background.anchorPoint= ccp(0,0);
    [self addChild:background z:0];
} else {
    background = [CCSprite spriteWithFile:@"mars_sample.jpg"];
    background.anchorPoint= ccp(0,0);
    [self addChild:background z:0];
    [self schedule:@selector(backgroundmoving:) interval:1/30];
}

请给我一些建议,它将如何工作。 谢谢大家

问候,

陶瑟夫汗

【问题讨论】:

标签: objective-c cocos2d-iphone


【解决方案1】:

backgroundmoving 中检查background.position = ccp( 0, bk_f ),您将坐标的Y 值设置为固定值bk_f。我假设您想要更改 X 值以滚动背景图像background.position = ccp( bk_f, 0 )

我假设的参数dtbackgroundmoving 是用来提供移动的背景图像,没有使用。

确保使用 autosize 属性将背景视图设置为展开以适合其超级视图。

检查背景视图的 frame 属性中的原点 - 检查它是否位于屏幕顶部。

我会在你的背景 UIImageView 后面添加一个普通的 UIView 并将它的颜色设置为绿色或其他一些刺眼的颜色。这样,您可以直观地确认视图所涵盖的房地产。

【讨论】:

  • 不,我没有使用 XIB 来布局我的背景。请参阅我在提出问题的顶部提供的代码。当背景开始移动时,我在几秒钟后面临移动背景和问题。
  • 实际上我在肖像模式下移动我的背景,因为我这样编码。请参阅我提供的屏幕短链接。 (mediafire.com/?s9dh63n3pv3znil 和 mediafire.com/?lfdxxrclo365ozj)。这里的背景从上到下移动。为此,我给出了这样的条件: background.position = ccp( 0, bk_f ) 如果您需要对此进行更多解释,请告诉我。感谢并等待您的回复
【解决方案2】:
//in .h file int bk_f=240;

-(void)backgroundmoving:(ccTime)dt 
{     

    bk_f=bk_f-5;    
    if(bk_f<=0)
    {
        background.position = ccp( 240,160); 
        bk_f=240;
    }
    else
    {
        background.position = ccp( bk_f,160); 
    }


} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 2012-10-15
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    相关资源
    最近更新 更多