cocos2d:如何控制滚动贴图

时间:2011-12-04 18:30:38

标签: iphone scroll cocos2d-iphone tiles parallax

我让我的地图自动滚动但是我想知道如何在地图到达结束之前减慢滚动速度,当它发生时我希望地图停止滚动。我想知道如何做到这一点,因为我的游戏是关于玩家试图射击敌人之前到达地图的末端,在那里有躲避他的庇护所。我希望在敌人到达避难所之前,滚动速度减慢,当敌人到达避难所时,我希望地图停止滚动。 这是代码:

@implementation GameplayScrollingLayer

// Scrolling with TileMap Layers inside of a Parallax Node
-(void)addScrollingBackgroundWithTileMapInsideParallax {
CGSize screenSize = [[CCDirector sharedDirector] winSize];
CGSize levelSize = [[GameManager sharedGameManager] 
                    getDimensionsOfCurrentScene];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    tileMapNode = [CCTMXTiledMap 
                   tiledMapWithTMXFile:@"Level2TileMap.tmx"];
} else {
    tileMapNode = [CCTMXTiledMap 
                   tiledMapWithTMXFile:@"Level2TileMapiPhone.tmx"];
}



CCTMXLayer *groundLayer = [tileMapNode layerNamed:@"GroundLayer"];
CCTMXLayer *rockColumnsLayer = [tileMapNode 
                                layerNamed:@"RockColumnsLayer"];
CCTMXLayer *rockBoulderLayer = [tileMapNode 
                                layerNamed:@"RockBoulderLayer"];


parallaxNode = [CCParallaxNode node];
[parallaxNode setPosition:
 ccp(levelSize.width/2,screenSize.height/2)];
float xOffset = 0.0f;


xOffset = (levelSize.width/2);
[groundLayer retain];
[groundLayer removeFromParentAndCleanup:NO];
[groundLayer setAnchorPoint:CGPointMake(0.5f, 0.5f)];
[parallaxNode addChild:groundLayer z:30 parallaxRatio:ccp(1,1) 
        positionOffset:ccp(0,0)];
[groundLayer release];


xOffset = (levelSize.width/2) * 0.8f;
[rockColumnsLayer retain];
[rockColumnsLayer removeFromParentAndCleanup:NO];
[rockColumnsLayer setAnchorPoint:CGPointMake(0.5f, 0.5f)];
[parallaxNode addChild:rockColumnsLayer z:20 
         parallaxRatio:ccp(0.2,1) 
        positionOffset:ccp(xOffset, 0.0f)]; 
[rockColumnsLayer release];


xOffset = (levelSize.width/2) * 0.3f;
[rockBoulderLayer retain];
[rockBoulderLayer removeFromParentAndCleanup:NO];
[rockBoulderLayer setAnchorPoint:CGPointMake(0.5f, 0.5f)];
[parallaxNode addChild:rockBoulderLayer z:30 
         parallaxRatio:ccp(0.7,1) 
        positionOffset:ccp(xOffset, 0.0f)];
[rockBoulderLayer release];


[self addChild:parallaxNode z:1];
}

// Accelerometer
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration        *)acceleration { //new
shipSpeedY = 9.0 + acceleration.x*20;
shipSpeedX = -acceleration.y*20;
}

// Updating ship based on accelerometer
-(void)updateShip { 
float maxY = winSize.height - ship.contentSize.height/2;
float minY = ship.contentSize.height/2;
float newY = ship.position.y + shipSpeedY;
newY = MIN(MAX(newY, minY), maxY);

float maxX = winSize.width - ship.contentSize.width/2;
float minX = ship.contentSize.width/2;
float newX = ship.position.x + shipSpeedX;
newX = MIN(MAX(newX, minX), maxX);

ship.position = ccp(newX, newY);
}

// Making background scroll automatically 
-(void)update:(ccTime)dt { 
[self updateShip];

CGPoint backgroundScrollVel = ccp(-100, 0);
parallaxNode.position = ccpAdd(parallaxNode.position, ccpMult(backgroundScrollVel, dt));


}

// Adding sprite ship
-(id)init {
self = [super init];
if (self != nil) {
   winSize = [CCDirector sharedDirector].winSize;

    ship=[CCSprite spriteWithFile:@"ship.png"];
    ship.position=ccp(60,160);
    [self addChild:ship z:100];

    self.isAccelerometerEnabled = YES; //new
    [self scheduleUpdate]; //new

    [self addScrollingBackgroundWithTileMapInsideParallax];
}
return self;
}

- (void) dealloc
{
[super dealloc];
}


@end

0 个答案:

没有答案