2012-07-26 98 views
1

我正在Cocos2d中開發遊戲。在它背景中不斷移動着x軸的場景。 我想知道我該怎麼做,因爲我是新的cocos2d。在CocoS2d中移動圖像

在此先感謝

回答

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 

用一下這個代碼,你會得到你所需要的。