代码之家  ›  专栏  ›  技术社区  ›  Sadat

ipad:如何通过滑动来移动精灵,并根据滑动速度继续移动

  •  0
  • Sadat  · 技术社区  · 15 年前

    我有一个精灵(球的形象),我可以移动它使用触摸和移动。我还确定了位置(x轴,y轴)的变化率取决于滑动周期。

    现在我需要继续这个精灵按照它的速度和方向前进。这是我的密码-

    Touch Event
    
    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    
      CGPoint location = [touch locationInView: [touch view]];
      CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
    
      self.touchStartTime = [event timestamp];
      self.touchStartPosition = location;
    
      if (YES == [self isItTouched:self.striker touchedAt:convertedLocation]) {
        self.striker.isInTouch = YES;
      }
    
      return YES;
    }
    
    - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    
      CGPoint location = [touch locationInView: [touch view]];
      CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
    
      self.touchLastMovedTime = [event timestamp];
      self.touchMovedPosition = convertedLocation;
    
    
      if(self.striker.isInTouch == YES){
        self.striker.position = self.touchMovedPosition;
      }
    
    }
    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    
      CGPoint location = [touch locationInView: [touch view]];
      CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
    
      self.touchEndTime = [event timestamp];
      self.touchEndPosition = location;
    
      if( self.striker.isInTouch == YES 
        && ( self.touchEndTime - self.touchLastMovedTime ) <= MAX_TOUCH_HOLD_DURATION )
      {
        float c = sqrt( pow( self.touchStartPosition.x - self.touchEndPosition.x, 2 ) 
            + pow( self.touchStartPosition.y - self.touchEndPosition.y, 2 ) );
    
        self.striker.speedx =  ( c - ( self.touchStartPosition.y - self.touchEndPosition.y ) ) 
                             / ( ( self.touchEndTime - self.touchStartTime ) * 1000 );
    
        self.striker.speedy =  ( c - ( self.touchStartPosition.x - self.touchEndPosition.x ) ) 
                 / ( ( self.touchEndTime -   self.touchStartTime ) * 1000 );
    
        self.striker.speedx *= 4;
        self.striker.speedy *= 4;
    
        self.striker.isInTouch = NO;
        [self schedule:@selector( nextFrame ) interval:0.001];
    
      }
    
    }
    
    Scheduled Method to move Sprite
    
    - (void) nextFrame {
    
      [self setPieceNextPosition:self.striker];
      [self adjustPieceSpeed:self.striker];
    
      if( abs( self.striker.speedx ) <= 1 && abs( self.striker.speedy ) <= 1 ){
        [self unschedule:@selector( nextFrame )];
      }
    }
    
    SET next Position
    
    - (void) setPieceNextPosition:(Piece *) piece{
    
      CGPoint nextPosition;
      float tempMod;
      tempMod = ( piece.position.x + piece.speedx ) / SCREEN_WIDTH;
      tempMod = (tempMod - (int)tempMod)*SCREEN_WIDTH;
      nextPosition.x = tempMod;
    
      tempMod = ( piece.position.y + piece.speedy ) / SCREEN_HEIGHT;
      tempMod = (tempMod - (int)tempMod)*SCREEN_HEIGHT;
      nextPosition.y = tempMod;
    
      piece.position = nextPosition;
    }
    
    Set new Speed
    
    - (void) adjustPieceSpeed:(Piece *) piece{
    
      piece.speedx =(piece.speedx>0)? piece.speedx-0.05:piece.speedx+0.05;
      piece.speedy =(piece.speedy>0)? piece.speedy-0.05:piece.speedy+0.05;
    }
    

    虽然,目前我正在使用静态速度调整技术,但我希望使其动态取决于初始速度(我欣赏任何想法)

    2 回复  |  直到 15 年前
        1
  •  0
  •   cc.    15 年前

    看起来你已经很接近了。你在触摸端安排一个选择器,计算速度,然后根据这个速度移动精灵,直到它衰减,所以所有的机械部件都在那里。

    看起来不确定的是物理学。例如,你在速度上做了一些奇怪的事情来减缓球的速度,这看起来一点都不现实。

     float speed = sqrt( speedx * speedx + speedy * speedy );
     if (speed == 0) {
         // kill motion here, unschedule or do whatever you need to
     }
     // Dampen the speed.
     float newSpeed = speed * .9;
     if (newSpeed < SOME_THRESHOLD) newSpeed = 0;
     speedx = speedx * newSpeed / speed;
     speedy = speedy * newSpeed / speed;
     //  Move sprite according to the new speed
    

    这将使球在释放时保持同一方向,逐渐减速直至停止。更多信息,特别是如果你想做一些反弹或什么,谷歌为向量代数介绍。

        2
  •  0
  •   Sadat    15 年前

    谢谢大家。

    //in adjustSpeed method
      piece.speedx -= piece.speedx * DEACCLERATION_FACTOR;
      piece.speedy -= piece.speedy * DEACCLERATION_FACTOR;
    
    //and simply, free the sprite to move from ccTouchMoved method not ccTouchEnd
      if(self.striker.isInTouch == YES && distance_passed_from_touch_start>=a_threashold){
        //calculate speed and position here as it was in ccTouchEnd method
        self.striker.isInTouch = NO;
        [self schedule:@selector( nextFrame ) interval:0.001];
    
      }
    
    推荐文章