Search code examples
ioscocos2d-iphoneuitouch

Move CCSprite based on users touch - Cocos2d


I have an object that I wish to move 50 pixels in the direction the the touch event occurs. It is a fixed CCSprite called _eyeObject.

I know I need to use

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

How would I calculate the CCSprite to move 50 pixels towards the users touch? Then move back to the original position when the touch ends?

A starting point would be great....I don't need to know how to animate or anything, just the calculation.


Solution

  • Whenever a touch happens you want to move 50 pixels in that direction you can do (based on your desire to do it based on a statically positioned eye object sprite):

    - (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;
    }
    

    If the object you are moving IS that eyeObject then it would be:

    - (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;
    }
    

    To move in general if the static object was representing a joystick or gamepad (would do the same in touch began so I'd separate this into its own method):

    - (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;
        }
    }