【问题标题】:How to make a sprite follow a trail?如何让精灵跟随轨迹?
【发布时间】:2012-05-26 09:24:53
【问题描述】:

我正在制作一个游戏,其中玩家画一条线,然后一个精灵应该跟随并在其上运行。我有一个可变数组和一个效果很好的绘制方法。但我无法找到移动精灵的方法。我尝试了不同的方法,但无法让迭代器工作。

它应该通过遍历数组来工作。其中填充了先前存储的 CGPoint 位置。我尝试在 ccTouchedEnded 中移动精灵,但它突出显示 [toucharray objectAtIndex:0] 并说“将 'id' 传递给不兼容类型 'CGPoint (aka 'struct CGPoint') 的参数”

   -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  { 
    //remove objects each time the player makes a new path
    [toucharray removeAllObjects];
}

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {    
    UITouch *touch = [ touches anyObject];
    CGPoint new_location = [touch locationInView: [touch view]];
    new_location = [[CCDirector sharedDirector] convertToGL:new_location];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

    // add touches to the touch array 
   [toucharray addObject:NSStringFromCGPoint(new_location)];
    [toucharray addObject:NSStringFromCGPoint(oldTouchLocation)];

}

-(void)draw
{
    glEnable(GL_LINE_SMOOTH);

    for(int i = 0; i < [toucharray count]; i+=2)
    {
        CGPoint start = CGPointFromString([toucharray objectAtIndex:i]);
        CGPoint end = CGPointFromString([toucharray objectAtIndex:i+1]);
        ccDrawLine(start, end);
    }
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

  // here is the line I can't get to work to move the sprite

 _sprite.position = ccpAdd(ccpMult([toucharray objectAtIndex:0], progress),         ccpMult([toucharray objectAtIndex:0+1], 1-progress));

}

【问题讨论】:

标签: iphone ios cocos2d-iphone touches trail


【解决方案1】:

我以前做过这个,通过(到目前为止)创建一个数组 我的屏幕上有多个精灵,所以我也有 1 个定义“触摸精灵”

ontouchstart 空数组并添加第一个点(起点)+ 将 TouchedSprite 设置为离起点最近的 Sprite) ontouchmove 将点添加到数组 ontouchend(通过动作在精灵上运行数组(稍后再谈)

我遇到了一些问题,(1) 我一次只能控制 1 个精灵,(2) 绘制的线点太多。

第 1 点的解决方案: 创建一个精灵的子类对象并像这样创建你的精灵。在该对象中创建一个数组(使其可以通过@property 和@synthesize 访问),它允许您将绘制的路径点放入其中。同时创建一个名为“runPathAction”的公共方法。 所以 onTouchStart 你清理了 Array 并设置了选中的 spriteObject。 OnTouchMove 添加项目,OntouchEnd 将选中的 spriteObject 的数组设置为本地数组,然后运行“runPathAction”方法。 (你可以将它传递给方法,但我喜欢这样做,以防我想访问数组)

解决方案 2: 我发现画线会导致很多点。所以,我创建了一个名为“CanDraw”的布尔运算符和一个时间间隔为 0.1 的时间表(你可以玩弄它)到一个将 canDraw 设置为 YES 的方法。然后在 onTouchMove 中检查“canDraw == YES”,添加点并设置 canDraw=NO;

这样,您将有 0.1 秒的时间间隔来添加积分。不要忘记删除日程 onTouchEnd!!!


现在如何运行这些动作。 你会想要一个稳定的速度,所以你需要设置一个速度变量。 遍历点数组并计算每个点之间的距离以创建总距离。 (PS不要忘记,第一品脱是从CurrentLocation到数组中的Point0) 当你有总距离时,你可以计算出你应该在每一步的动作中设置多少延迟时间。 (如果你不这样做,你就不会知道延迟时间,如果你修复它,你会得到奇怪的运动)。

创建一个方法, 检查数组的“计数”。如果计数 = 0 返回; (完成!!!做清理什么的) 否则运行一个actionsequence,最后调用它自己。 (计数检查将处理“中断”。 抓取数组 CGPoint p = (objectAtIndex:0) 的 1 项;并从数组中删除该项目 (removeAtIndex:0)。 使用延迟时间运行动作,然后就可以了!

【讨论】:

    【解决方案2】:

    在一个数组或一个列表中记录路径的位置,并遍历它以沿着路径移动你的精灵。我在制作的游戏中这样做了,以在玩家身后创建粒子轨迹。我使用了一个大小为 20 的数组,并在一个区间内对其进行迭代,用我的角色位置更新迭代器位置的数组,然后将粒子效果移动到存储在数组中迭代器位置加 1 的位置。

    您需要使用起始位置为数组播种,这样您就没有空值,并且当您位于数组末尾时,您需要一个特殊情况,因为您不想从越界位置读取,而是从 0 位置读取代码。

    【讨论】:

    • 您应该有一个 CGPoints 数组,您正在使用迭代器访问并传递给您的 move 函数。如果您需要更多帮助,可以在这里发布一些代码供我查看吗?
    • 我更新了上面的代码看看它移动功能有问题。我可以存储 CGPoints,但我无法让移动函数从数组中读取
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    相关资源
    最近更新 更多