Cocos2D Sprite相对运动 [英] Cocos2D Sprite Relative Movement

查看:186
本文介绍了Cocos2D Sprite相对运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我似乎无法解决。
我想根据手指移动在屏幕上移动精灵,但不是使精灵移动朝向触摸位置,我想让精灵在同一

I have a problem that I can't seem to solve. I would like to move a sprite on the screen based on finger movements, but instead of making the sprite move towards the touch position I would like the sprite to move in the same way the finger moves on the screen.

示例:
用户将他/她的手指放在屏幕上(任意位置),然后拖动手指在屏幕上向左移动200像素,sprite也应向左移动200像素。

Example: User puts his/her finger on the screen (any position) and then drags the finger on the screen by 200 pixel to the left, the sprite should also move of 200 pixels to the left.

我的做法是将玩家的位置存储在开始,然后计算手指的开始位置和当前位置之间的差值,以添加到子画面本身的起始位置。

My approach was to store the position of the player at the start and then calculating the difference between the start position and the current position of the finger to add to the starting position of the sprite itself.

这是我的代码

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if([touches count] == 1){
    UITouch *touch = [touches anyObject];
    CGPoint startPos = [touch locationInView:[touch view]];
    startPosition = startPos;
}

}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if([touches count] == 1){
        UITouch *touch = [touches anyObject];
        CGPoint touchPos = [touch locationInView:[touch view]];
        CGPoint playerPos = player.position;

        float yDifference = startPosition.y - touchPos.y;
        playerPos.y = playerPos.y - yDifference;

        // just to change to GL coordinates instead of the ones used by Cocos2D
        // still doesn't work even if I remove this...
        CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:playerPos];
        [player setPosition:convertedPoint];
    }

}

我希望我的问题是清楚的,但如果不是不要犹豫提问或进一步解释。我已经坚持了这一段时间:'(

I hope the problem I have is clear but if it is not don't hesitate do ask questions or further explaining. I've been stuck on this for quite a while :'(

推荐答案

另外,如果你想要移动

Also, if you want it to move "relative" to where it started as opposed to exactly where the touch is:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if([touches count] == 1){
      UITouch *touch = [touches anyObject];
      CGPoint startPos = [touch locationInView:[touch view]];
      startPosition = startPos;
      playerStartPos = playerPos;
    }
 }

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if([touches count] == 1){
      UITouch *touch = [touches anyObject];
      CGPoint touchPos = [touch locationInView:[touch view]];

      CGPoint newPlayerPos;
      newPlayerPos.x = playerStartPos.x + (touchPos.x - startPosition.x);
      newPlayerPos.y = playerStartPos.y + (touchPos.y - startPosition.y);

      // just to change to GL coordinates instead of the ones used by Cocos2D
      // still doesn't work even if I remove this...
      CGPoint convertedPoint = [[CCDirector sharedDirector] convertToGL:newPlayerPos];
      [player setPosition:convertedPoint];
}

您的代码看起来像是反馈与playerPos.y的区别

Your code is looking like it's "feeding back" onto itself by continually adding the difference to the playerPos.y

这篇关于Cocos2D Sprite相对运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆