为什么当我“滚动”时这个 iPhone 应用程序出现滞后?

发布于 2024-09-09 01:27:59 字数 2131 浏览 7 评论 0原文

我正在开发一款类似“涂鸦跳跃”的游戏,屏幕上同时显示 5 个平台。有两个背景 UIImageView。我有“scrollUp”函数,每当玩家位于屏幕顶部 3/4 时就会调用该函数。当背景在视野中时,游戏速度变慢并且滞后,当背景在视野之外(已经消失)时,游戏运行流畅且精细。我尝试禁用使它们移动的实际代码,但它仍然滞后,背景图像都是我在 Photoshop 中创建的 png,并且每个都在单独的 UIImageView 中。

这是“scrollUp”函数代码:(记住,这是使用 Xcode for iphone 的 Objective-C):

-(void)scrollUp {
    if(fireball.center.y < scrollLine) {
    //sets up variables for background movement to add to.
    float oldXB1 = background1.center.x;
    float oldYB1 = background1.center.y;
    float oldXB2 = background2.center.x;
    float oldYB2 = background2.center.y;

         //makes "players" velocity go in the downward direction (disregard for question)
    velocity.y += .25;

    //sets up a for loop which tests each platform individually
         //[platformHolder] holds all 5 platforms
    for (int i = 0; i < [platformHolder count]; i++) {
    UIImageView *temp = (UIImageView *)[platformHolder objectAtIndex:i];
    if(fireball.center.y > 0) {
        temp.center = CGPointMake(temp.center.x, temp.center.y + 4);
    }
    if(fireball.center.y < 0) {
        temp.center = CGPointMake(temp.center.x, temp.center.y + 8);
        velocity.y += .03;
    }
    if(temp.center.y >  480) {
        scoreNum += 10;
        score.text =[NSString stringWithFormat:@"Score: %i", scoreNum];
        temp.center = CGPointMake(arc4random() % randomNumMax, 0);
    }
    }

    if(fireball.center.y > 0) {
        background1.center = CGPointMake(oldXB1, oldYB1 + velocityB1.y);
        background2.center = CGPointMake(oldXB2, oldYB2 + velocityB2.y);
    }

    if(fireball.center.y < 0) {
        background1.center = CGPointMake(oldXB1, oldYB1 + velocityB1.y);
        background2.center = CGPointMake(oldXB2, oldYB2 + velocityB2.y);
    }

    if(background1.center.y > (480 + background1.center.y/2)) {
        background1.center = CGPointMake(background1.center.x, -1*background1.center.y );
    }
    if(background2.center.y > (480 + background2.center.y/2)) {
         background2.center = CGPointMake(background2.center.x, -1*background2.center.y );
    }       
    }
}

I am developing a "doodle jump" like game and I have 5 platforms on the screen on at a time. There are two background UIImageViews. I have the "scrollUp" function, which is called whenever the player is in the top 3/4's of the screen. When the backgrounds are in view, the game slows down and lags, when the backgrounds are out of view (have scolled away), the game runs smooth and fine. I tried disabling the actual code that made them move and it still lagged, the background images are both png's that I created in photoshop and are each in an individual UIImageView.

Here is the "scrollUp" function code: (remember, this is objective-c using Xcode for iphone) :

-(void)scrollUp {
    if(fireball.center.y < scrollLine) {
    //sets up variables for background movement to add to.
    float oldXB1 = background1.center.x;
    float oldYB1 = background1.center.y;
    float oldXB2 = background2.center.x;
    float oldYB2 = background2.center.y;

         //makes "players" velocity go in the downward direction (disregard for question)
    velocity.y += .25;

    //sets up a for loop which tests each platform individually
         //[platformHolder] holds all 5 platforms
    for (int i = 0; i < [platformHolder count]; i++) {
    UIImageView *temp = (UIImageView *)[platformHolder objectAtIndex:i];
    if(fireball.center.y > 0) {
        temp.center = CGPointMake(temp.center.x, temp.center.y + 4);
    }
    if(fireball.center.y < 0) {
        temp.center = CGPointMake(temp.center.x, temp.center.y + 8);
        velocity.y += .03;
    }
    if(temp.center.y >  480) {
        scoreNum += 10;
        score.text =[NSString stringWithFormat:@"Score: %i", scoreNum];
        temp.center = CGPointMake(arc4random() % randomNumMax, 0);
    }
    }

    if(fireball.center.y > 0) {
        background1.center = CGPointMake(oldXB1, oldYB1 + velocityB1.y);
        background2.center = CGPointMake(oldXB2, oldYB2 + velocityB2.y);
    }

    if(fireball.center.y < 0) {
        background1.center = CGPointMake(oldXB1, oldYB1 + velocityB1.y);
        background2.center = CGPointMake(oldXB2, oldYB2 + velocityB2.y);
    }

    if(background1.center.y > (480 + background1.center.y/2)) {
        background1.center = CGPointMake(background1.center.x, -1*background1.center.y );
    }
    if(background2.center.y > (480 + background2.center.y/2)) {
         background2.center = CGPointMake(background2.center.x, -1*background2.center.y );
    }       
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文