1

我正在用 Cocos2d 开发游戏。在其中,场景的背景随着 x 轴不断移动。我想了解我该怎么做,因为我是 cocos2d 的新手。

提前致谢

4

1 回答 1

1

看看我的代码 -这里是源代码。在 中找到“移动”技术HelloWorldLayer.m。我拍摄了一张 360 度全景照片,并将其从右到左连续移动,全屏显示。

在 YourClass.h 文件中:

#import "cocos2d.h"
@interface YourClass : CCLayer

+(CCScene *) scene;

@end

在 YourClass.m 文件中:

#import "YourClass.h"

@implementation YourClass

CCSprite *panorama;
CCSprite *appendix;
//_____________________________________________________________________________________
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    YourClass *layer = [YourClass node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}
//_____________________________________________________________________________________
// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {

        panorama = [CCSprite spriteWithFile: @"panorama.png"];
        panorama.position = ccp( 1709/2 , 320/2 );
        [self addChild:panorama];

        appendix = [CCSprite spriteWithFile: @"appendix.png"];
        appendix.position = ccp( 1709+480/2-1, 320/2 );
        [self addChild:appendix];

        // schedule a repeating callback on every frame
        [self schedule:@selector(nextFrame:)];

    }
    return self;
}
//_____________________________________________________________________________________
- (void) nextFrame:(ccTime)dt {

    panorama.position = ccp(panorama.position.x - 100 * dt, panorama.position.y);
    appendix.position = ccp(appendix.position.x - 100 * dt, appendix.position.y);
    if (panorama.position.x < -1709/2) {
        panorama.position = ccp( 1709/2 , panorama.position.y );
        appendix.position = ccp( 1709+480/2-1, appendix.position.y );
    }

}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}
//_____________________________________________________________________________________
@end

玩一点这段代码,你会得到你需要的。

于 2012-07-26T13:50:45.667 回答