-1

在我的游戏中,我有两个背景一个接一个地创建一个向左移动的连续循环。但是,现在一个去,另一个不跟随。

我无情地尝试了不同的数字,但似乎没有任何帮助。

谢谢你能给我的任何帮助。

if((self = [super init]))
    {

        self.isTouchEnabled = YES;

        background=[CCSprite spriteWithFile:@"testbackground88.png"];
        [self addChild:background z:1];
        background.position=ccp(500,240);
        id repeat1 =[CCRepeatForever actionWithAction:[CCSequence actions:
                                                             [CCMoveTo actionWithDuration:7 position:ccp(-300,240)],
                                                             [CCPlace actionWithPosition:ccp(800,240)],nil]];
       [background runAction:repeat1];


        background2=[CCSprite spriteWithFile:@"testbackground92.png"];
        [self addChild:background2 z:1];
        background2.position=ccp(500,240);
        id repeat2 =[CCRepeatForever actionWithAction:[CCSequence actions:
                                                             [CCMoveTo actionWithDuration:7 position:ccp(-300,240)],
                                                             [CCPlace actionWithPosition:ccp(800,240)],nil]];
       [background2 runAction:repeat2];
}
4

2 回答 2

0

按照你这样做的方式,你应该总是看到 bg2 超过 bg,因为它们每次都移动到相同的确切位置。例如,如果您希望将 background2 放在背景 1 的右侧,您可以执行类似的操作

background2=[CCSprite spriteWithFile:@"testbackground92.png"];
[self addChild:background2 z:1];
int bgWidth = background2.contentSize.width;
background2.position=ccp(500 + bgWidth ,240);
id repeat2 =[CCRepeatForever actionWithAction:[CCSequence actions:
    [CCMoveTo actionWithDuration:7 position:ccp( (-300 + bgWidth) ,240)],
    [CCPlace actionWithPosition:ccp((800 + bgWidth ),240)],nil]];

最好将两个 sprite 添加到一个容器 Sprite 中,一个相邻的,并在容器上执行移动操作

于 2012-08-23T07:39:29.837 回答
0
if((self = [super init]))
{
    self.isTouchEnabled = YES;

    //add an empty container sprite in 0,0 pos
    CCSprite container = [CCSprite node];
    [self addChild:container];

    // get the screen size and use it for positioning
    CGSize scrSize = [[CCDirector sharedDirector] winSize];

    // add the sprites to the container
    background  =[CCSprite spriteWithFile:@"testbackground88.png"];
    background2 =[CCSprite spriteWithFile:@"testbackground92.png"];

    [container addChild:background z:1];
    [container addChild:background2 z:1];

    // use the background's widths to define it's in between distance. 
    int bgDist = ( background2.contentSize.width + background.contentSize.width) / 2;
    // the container's total travel;
    int containerTravel =  ( background.contentSize.width + background2.contentSize.width)/2 ;

    background.position     =ccp(scrSize.width/2, scrSize.height/2);
    background2.position    =ccp( (scrSize.width/2 + bgDist), scrSize.height/2);

    // move the container
    CCRepeatForever loopBgTravel =[CCRepeatForever actionWithAction:[CCSequence actions:
        [CCMoveTo actionWithDuration:0 position:ccp(0,0)],
        [CCMoveTo actionWithDuration:7 position:ccp(-containerTravel,0)]];

   [container runAction:loopBgTravel];
}

如果这不起作用,请随时再次询问

于 2012-08-24T05:17:30.407 回答