开发者

Problem animating a Parallax Layer in Cocos2d-iPhone

I'm trying to implement my own parallax scrolling in Cocos2d since the built in parallax functionality isn't exactly what I'm looking for (it scales the layers, which is not what I want)

So, my own Parallax Layers overwrite the Layers setPosition method like this

- (void) setPosition:(CGPoint)point
{
    [super setPosition:CGPointMake(point.x / pRatio, point.y / pRatio)];
}

pRatio is an int that reduces the amount the layer moves by, thus creating the kind of Parallax effect I'm looking for. And it works perfectly like this.

However, on occasion I need to animate a Layer to a specific position using MoveTo, so I created this method which responds to an NSNotiification

- (void) gotoStartPosition:(NSNotification *)notification
{
    CGPoint startPos = CGPointFromString([notification object]);
    id moveToStart = [MoveTo actionWithDuration:1 positi开发者_StackOverflow中文版on:startPos];
    [self runAction:moveToStart];

}

Which works until I set the pRatio to anything above 1. If the pRatio is higher than 1, then gotoStartPosition makes the layer jump to CGPoint(0,0) and animates into position from there.

I can't figure out at what point the Layers position is being set to 0,0, and I don't understand why it's only doing this when pRatio is higher than 1.

I suspect this is going to be something painfully simple, but I think my brain is fried. Any help, greatly appreciated.


UPDATE: I'm a doofus. What's happening is obvious. The Animation first sets the layers position to it's current position. But I've told the layer to divide any setPosition coordinates by pRatio. So it's not going to CGPoint(0,0) but something like CGPoint(0.032, 0). I've fixed it like this:

- (void) setPosition:(CGPoint)point
{
    if(!animating){
        [super setPosition:CGPointMake(round(point.x / pRatio), round(point.y / pRatio))];
    }else{
        [super setPosition:point];
    }

}


- (void) gotoStartPosition:(NSNotification *)notification
{
    animating = YES;
    CGPoint startPos = CGPointFromString([notification object]);
    CGPoint pStart = CGPointMake(startPos.x / pRatio, startPos.y / pRatio);
    id moveToStart = [MoveTo actionWithDuration:1 position:pStart];
    id endAnim = [CallFunc actionWithTarget:self selector:@selector(endAnimation)];
    [self runAction:[Sequence actions: moveToStart, endAnim, nil]];
}

- (void) endAnimation
{
    animating = NO;
}

Which isn't elegant, but it works. I'm closing this question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜