在CocoS2d中移动图像

时间:2012-07-26 13:31:23

标签: iphone

我正在用Cocos2d开发游戏。在它的场景背景不断移动与x轴。 我想知道我怎么能这样做,因为我是cocos2d的新手。

提前致谢

1 个答案:

答案 0 :(得分:1)

查看我的代码 - here are the sources。在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

使用此代码播放一些内容,您将获得所需内容。