在Cocos2d中滚动多个对象

时间:2013-10-30 14:09:14

标签: ios objective-c cocos2d-iphone

我遇到了在Cocos2d中进行侧滚动的问题。情况是,我有一个精灵,其中包含多个其他精灵,称为动作。用户可以水平来回滑动以滚动多个动作。现在发生的事情是,它非常生涩,似乎滞后而不是一个平滑的卷轴,但只是非常不稳定。不知道问题是什么,我试图改变动画的时间,但这似乎不起作用。

- (void)translateInventoryForSwipe:(int)xTranslationValue {

  NSArray* tempArray = [NSArray arrayWithArray:self.slotsCenterCoordinates];
  [self.slotsCenterCoordinates removeAllObjects];

  for (NSNumber* i in tempArray) {

    NSNumber* newXCoordinate = [NSNumber numberWithInt:[i intValue] + xTranslationValue];
    [self.slotsCenterCoordinates addObject:newXCoordinate];
  }

  [self updatePositionOfActionsInInventory];
}

此方法从父视图中获取两个触摸的delta x。 (当前触摸减去上一次触摸)这将设置滚动视图中所有操作的中心坐标。

- (void)updatePositionOfActionsInInventory {

  for (int inventoryCounter = 0; inventoryCounter < self.inventorySize; inventoryCounter++) {

    FFAction* action = [self.actions objectAtIndex:inventoryCounter];
    if (action != self.actionBeingDragged)
      [self placeAction:action atIndex:inventoryCounter];
  }
  self.tempAction = nil;
}

- (void)placeAction:(FFAction*)action atIndex:(int)index {

  const float yCenterCoordinate = self.boundingBox.size.height/2;

  NSNumber* xCenterCoordinate = [self.slotsCenterCoordinates objectAtIndex:index];
  CGPoint centerPointForActionAtIndex = ccp([xCenterCoordinate floatValue], yCenterCoordinate);
  CCAction* updatePositionAction = [CCMoveTo actionWithDuration:0.03f position:centerPointForActionAtIndex];
  if ([action.view numberOfRunningActions] == 0 || self.tempAction == action) {

    [action.view runAction:updatePositionAction];
    [action.view released];
  }
}

这部分来自处理触摸的父精灵:

  CGPoint currentTouch = [self convertTouchToNodeSpace:touch];
  CGPoint previousTouch = [touch previousLocationInView:[touch view]];
  int translationPoint = currentTouch.x - previousTouch.x;
  [self.inventory translateInventoryForSwipe:translationPoint withPoint:currentTouch];

然后设置模仿滚动效果的动作坐标。我不确定它导致生涩动作的地方,但是如果有人对这种情况有任何帮助那就太棒了!

1 个答案:

答案 0 :(得分:0)

假设代码中的所有复杂性都不是必需的,这里有几个方面需要考虑,我将逐一介绍它们。

首先,内存分配很昂贵,而且很多都是在translateInventoryForSwipe:的每次调用中完成的。创建一个全新的NSArray并重新填充self.slotsCenterCoordinates。相反,您应该迭代动作精灵并逐个重新定位它们。

这将我们带到第二个方面,即使用CCAction移动精灵。为每个精灵创建一个新的CCAction,由于内存分配再次导致延迟。即使不使用CCAction,也会创建CCAction。此外,操作的使用可能是滞后的主要原因,因为在前一个操作完成之前不会接受新操作。更好的方法是直接按delta重新定位精灵,而不是为重新定位分配动作。由于调用translateInventoryForSwipe:的频率很高,因此无需采取相应的动作。

您还应该考虑使用float将delta值发送到方法而不是int。触摸坐标是浮动的,特别是在视网膜设备上,这对于两个像素的距离为0.5f很重要。

基于这些方面,这里是固定方法的模板。这未经过测试,因此可能存在错误。另外,我假设action.view是实际的精灵,因为动作是在那里分配的。

- (void)translateInventoryForSwipe:(float)xTranslationValue {
    for (FFAction *action in self.actions) {
        if (action == self.actionBeingDragged)
            continue;
        // Position the items manually
        float xCoordinate = action.view.position.x + xTranslationValue;
        float yCoordinate = self.boundingBox.size.height/2;
        action.view.position = ccp(xCoordinate, yCoordinate);
    }
}