1

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

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

}
4

2 回答 2

2

I did this before, by (what you have so far) creating an array Mine was with multiple sprites on screen, so I also had 1 definition "Touched sprite"

ontouchstart empty array and add first point (start point) + set TouchedSprite to the Sprite that was closest to the start point) ontouchmove add point to array ontouchend (run the array on the sprite by actions (get back to that later)

A few problems is what I got, (1) I could only control 1 sprite at a time and (2) the drawn line had way too many points.

Solution to number 1: Create a subclass Object of a sprite and create your sprites like that. Within that object create an array (make it accessible with @property and @synthesize) which will allow you to place the drawn path points in. Also create a public method called "runPathAction". So onTouchStart you clean the Array and set the selected spriteObject. OnTouchMove add the items, OntouchEnd set the array of the selected spriteObject to the local array and then run the "runPathAction" method. (You could have passed it to the method, but I like doing it this way, just in case I want to access the array)

Solution to number 2: I found that drawing the line creates WAY to many points. So, i created a boolean operator called "CanDraw" and a schedule with a time interval of 0.1 (you can play around with it) to a method which sets canDraw to YES. and then in the onTouchMove you check for "canDraw == YES", add the point and set canDraw=NO;

This way you will have an interval of 0.1 seconds to add the points. Don't forget to remove the schedule onTouchEnd!!!


Now how to run the actions. You will want a steady speed, so you will need to set a speed variable. Walk through the array of points and calculate the distance between each point to create a total distance. (PS Don't forget that the first pint is from CurrentLocation to Point0 in the array) When you have the total distance you can figure out how much delay time you should set in your actions per step. (if you don't do this, you don't know the delay time and if you fix that you get weird movement).

Create a method, check the "count" of the array. If count = 0 return; (finished!!! do cleanup or whatever) otherwise run an actionsequence, which at the end calls itself. (the check for count will handle the "break". grab 1 item of the array CGPoint p = (objectAtIndex:0); and remove that item from the array (removeAtIndex:0). Run the action with the delay time and there you go!

于 2012-08-23T05:22:16.113 回答
1

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

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

于 2012-05-18T19:53:16.097 回答