【问题标题】:Move CCSprite based on users touch - Cocos2d基于用户触摸移动CCSprite - Cocos2d
【发布时间】:2014-08-14 14:28:54
【问题描述】:

我有一个对象,我希望在触摸事件发生的方向上移动 50 个像素。它是一个固定的 CCSprite,名为_eyeObject

我知道我需要使用

- (void)touchBegan:(UITouch *)touches withEvent:(UIEvent *)event {
    CGPoint touchLocation = [touches locationInNode:self];
    //Not sure what to do now
}

如何计算 CCSprite 向用户触摸移动 50 像素?然后在触摸结束时回到原来的位置?

一个起点会很棒....我不需要知道如何制作动画或其他任何东西,只需要计算即可。

【问题讨论】:

    标签: ios cocos2d-iphone uitouch


    【解决方案1】:

    每当发生触摸时,您都希望在该方向上移动 50 像素,您可以这样做(基于您希望基于静态定位的眼睛对象精灵来实现):

    - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
    {
        CGPoint touchPos = [touch locationInNode:self];
        CGPoint delta = ccpSub(touchPos, self.eyeObject.position);
    
        float moveAmount = 50.0f;
        CGPoint moveVec;
    
        if (delta.x > 0.0f)
        {
            moveVec.x = moveAmount;
        }
        else
        {
            moveVec.x = -moveAmount;
        }
    
        if (delta.y > 0.0f)
        {
            moveVec.y = moveAmount;
        }
        else
        {
            moveVec.y = -moveAmount;
        }
    
        self.spriteStartPosition = self.sprite.position;
        self.sprite.position = ccpAdd(self.sprite.position, moveVec);
    }
    
    
    - (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
    {
        self.sprite.position = self.spriteStartPosition;
    }
    

    如果您正在移动的对象是那个 eyeObject,那么它将是:

    - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
    {
        CGPoint touchPos = [touch locationInNode:self];
        CGPoint delta = ccpSub(touchPos, self.eyeObject.position);
    
        float moveAmount = 50.0f;
        CGPoint moveVec;
    
        if (delta.x > 0.0f)
        {
            moveVec.x = moveAmount;
        }
        else
        {
            moveVec.x = -moveAmount;
        }
    
        if (delta.y > 0.0f)
        {
            moveVec.y = moveAmount;
        }
        else
        {
            moveVec.y = -moveAmount;
        }
    
        self.eyeObjStartPosition = self.eyeObject.position;
        self.eyeObject.position = ccpAdd(self.eyeObject.position, moveVec);
    }
    
    
    - (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
    {
        self.eyeObject.position = self.eyeObjStartPosition;
    }
    

    如果静态对象代表操纵杆或游戏手柄,一般要移动(在 touch 开始时也会这样做,所以我会将其分离到自己的方法中):

    - (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
    {
        CGPoint touchPos = [touch locationInNode:self];
        CGPoint delta = ccpSub(touchPos, self.dPad.position);
    
        float moveAmount = 50.0f;
    
        if (delta.x > 0.0f)
        {
            self.sprite.position.x += moveAmount;
        }
        else
        {
            self.sprite.position.x -= moveAmount;
        }
    
        if (delta.y > 0.0f)
        {
            self.sprite.position.y += moveAmount;
        }
        else
        {
            self.sprite.position.y -= moveAmount;
        }
    }
    

    【讨论】:

      【解决方案2】:

      这里有一个简单的解决方案——获取两点之间的单位向量,将其乘以 50,然后将其添加到当前位置。又名:

      - (void)touchBegan:(UITouch *)touches withEvent:(UIEvent *)event {
          CGPoint touchLocation = [touches locationInNode:self];
          //Not sure what to do now
      
          CGPoint destinationVector = ccpSub(touchPos, self.eyeObject.position);
          CGPoint movementVector = ccpNormalize(destinationVector);
          movementVector = ccpMult(movementVector, 50);
      
          [_eyeObject runAction:[CCActionMoveTo actionWithDuration:1.0 position:ccpAdd(_eyeObject, movementVector)]];
      }
      

      如果您想让它在 touchEnded 上移回,只需在第一次移动它之前将位置保存在一个变量中,然后在 touchEnded 上将它发送回那个位置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多