When sliding sprite, if sprite disappears off the

2019-04-17 13:56发布

问题:

When sliding sprite, if sprite disappears off the side, i want to make it wrap around to the opposite side but I do not know how to do this while the sprite is simultaneously being pushed off one side i want the other bit that you can't see to appear on the opposite side like a loop some sort of wormhole thing.

here is my code so far but it crashes and it only transports the sprite once the whole of the sprite disappears of the side. Loop also needs to run as an infinite loop until someone quits the app.

        for (int i =0; i<16; ++i) {
        MyNode *currentSprite = [c1array objectAtIndex:i];
        if (currentSprite.contentSize.height>=320 || currentSprite.position.y-currentSprite.contentSize.height/2<=0 ){
            MyNode *Bsprite = currentSprite;
            MyNode *Tsprite = currentSprite;
            Bsprite.scale = 1.0;
            Tsprite.scale = 1.0;

            if(currentSprite.position.y >=253){
            Bsprite.position = ccp(currentSprite.position.x,-35);
                [self addChild:Bsprite];
                Bsprite.visible = TRUE;
            }
            if (currentSprite.position.y <=0) {
                Tsprite.position = ccp(currentSprite.position.x,324);
                [self addChild:Tsprite];
                Tsprite.visible = TRUE;
            }
            MyNode *isChanging;
            if ((Tsprite.visible == TRUE && currentSprite.visible == TRUE) || (Bsprite.visible == TRUE && currentSprite.visible == TRUE)) {
                isChanging = TRUE;
            }
            if (isChanging == FALSE) {
                [self removeChild:Tsprite cleanup:YES];
                [self removeChild:Bsprite cleanup:YES];
            }
        }
    }

回答1:

It is not possible to do with one sprite. But you can have two sprites. In common situation when your sprite is sliding along the screen only one sprite will be visible. But when it reaches the border the second one will be visible too. When the second one will completely enter the screen - remove (or hide) the first one.

The best way to implement this is to create a CCNode subclass that will contain first and second sprite and will swap them if required. In this way all your logic will be very simple. You will just work with one CCNode (subclass) and will not think about swaping sprites - it will be done automatically by your class

EDIT

@interface MyNode : CCNode
{
    CCSprite *sprite1;
    CCSprite *sprite2;
    CCSprite *currentSprite;
    bool isChanging; //indicates that now two sprites are visible
}
@end