gpt4 book ai didi

ios - 背景图像在 cocos2d 中没有正确滚动

转载 作者:行者123 更新时间:2023-11-29 11:05:27 25 4
gpt4 key购买 nike

我编写了在游戏背景中滚动图像的代码。简单地定义两个 Sprite 并设置它们的位置。

CGSize screenSize=[[CCDirector sharedDirector]winSize];

sky1 = [CCSprite spriteWithFile:@"sky1.png"];

sky1.position=ccp(screenSize.width/2, screenSize.height/2);
[self addChild:sky1];
CCLOG(@"sky1 position width=%f height=%f",sky1.position.x,sky1.position.y);
sky2=[CCSprite spriteWithFile:@"sky2.png"];
sky2.position=ccp(screenSize.width/2, sky1.position.y+sky1.contentSize.height/2+sky2.contentSize.height/2);
[self addChild:sky2];
[self schedule:@selector(scroll:) interval:0.01f];

并在scroll方法中编写滚动的代码:

-(void)scroll:(ccTime)delta{
CGSize screen=[[CCDirector sharedDirector] winSize];
CCLOG(@"come from schedule method pos.y=%f",sky1.position.y);
sky1.position=ccp(sky1.position.x, sky1.position.y-1);
sky2.position=ccp(sky2.position.x, sky2.position.y-1);
if(sky1.position.y <= -([sky1 boundingBox].size.height));
{
sky1.position=ccp(sky1.position.x, sky2.position.y+[sky2 boundingBox].size.height);

CCLOG(@"COMING IN first ifin scroll mwthod position width=%f pos.y=%f",sky1.position.x,sky1.position.y);
}
if(sky2.position.y<= -[sky2 boundingBox].size.height);
{
sky2.position=ccp(sky2.position.x, sky1.position.y+[sky1 boundingBox].size.height);
CCLOG(@"coming in secnond if ");
}
}

当我删除 if 条件时,它会正常工作一次。我不知道我的情况出了什么问题,我的代码到底是怎么回事。谁能解释一下?

最佳答案

我不太确定为什么您的代码无法正常工作。我脑海中闪过的一个念头是你的间隔时间可能太短了。换句话说,0.01f (1/100) 比您的游戏更新速度更短,最有可能是 0.016 (1/60)。所以我首先要尝试的是将间隔调整为 0.02f。您还可以从 schedule 调用中删除 interval 参数,这样它每帧只运行一次。我也会尝试删除 ccTime 参数,因为它没有被使用。

[self schedule:@selector(scroll)];

-(void)scroll {}

但除此之外,这可能最好使用 CCActions 来处理。这看起来像是一对简单的 CCMoveTo 操作与 CCRepeatForever 组合以获得您想要的效果。这就是我要走的路。

编辑:这里有一些关于使用 CCActions 来完成这个的更多细节。有多种方法可以完成同一件事,但您可以尝试以下一种方法:

float moveDuration = 3; // or however fast you want it to scroll
CGPoint initialPosition = ccp(screenSize.width/2, screenSize.height/2);
CGPoint dp = ccp(0, -1*sky1.contentSize.height); // how far you want it to scroll down
CCMoveBy *scrollSky1 = [CCMoveBy actionWithDuration:moveDuration position:dp];
CCMoveTo *resetSky1 = [CCMoveTo actionWithDuration:0 position:initialPosition];
CCSequence *sequence = [CCSequence actionOne:scrollSky1 two:resetSky1];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:sequence];
[sky1 runAction:repeat];

关于ios - 背景图像在 cocos2d 中没有正确滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13835568/

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