【问题标题】:Sprite kit - Moving the node along the x using tochesMovedSprite kit - 使用 tochesMoved 沿 x 移动节点
【发布时间】:2014-05-02 23:59:00
【问题描述】:

我有一个 Box 节点,我只想通过向左或向右滑动来左右移动它。 下面代码的问题在于,一旦发生触摸,Box 就会明显跳到触摸的 x 位置,然后它会随着您的手指移动。

不错,但这不是我想要的效果,如果触摸发生(无论屏幕上的哪个位置),框都会保持在原位,并且只会相应地左右移动(- 或 x 的值触摸的位置)并且最初不要跳到手指的位置。所以 BoxNewPostion.postion.x 不应该真的等于 touchLocation.x

只是补充一点,如果我尝试,Box 会快速闪烁

CGPoint BoxNewPosition = CGPointMake(touchLocation.x - Box.postion.x, Box.position.y); 

看起来好像延迟计算和减去 x 位置。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

        UITouch *touch = [[event allTouches] anyObject];

        CGPoint touchLocation = [touch locationInView:self.view];


        SKSpriteNode* Box = (SKSpriteNode*)[self childNodeWithName:@"BoxNode"];

        CGPoint BoxNewPosition = CGPointMake(touchLocation.x, Box.position.y);

        Box.position = BoxNewPosition;

}

【问题讨论】:

    标签: objective-c ios7 sprite-kit


    【解决方案1】:

    您可以将以下代码更改为仅在 x 轴上移动。

    @implementation MyScene
    {
        SKSpriteNode *object1;
        CGPoint destinationLocation;
    }
    
    -(id)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {
            [self createObject];
            destinationLocation = CGPointMake(300, 150);
        }
        return self;
    }
    
    -(void)createObject
    {
        object1 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
        object1.position = CGPointMake(300, 150);
        [self addChild:object1];
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        destinationLocation = [touch locationInNode:self.scene];
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        destinationLocation = [touch locationInNode:self.scene];
    }
    
    -(void)update:(CFTimeInterval)currentTime
    {
        float x = fabs(object1.position.x - destinationLocation.x);
        float y = fabs(object1.position.y - destinationLocation.y);
    
        float divider = 0;
        if(x > y)
        {
            divider = x;
        } else
        {
            divider = y;
        }
    
        float xMove = (x/divider)*8; // change number to increase or decrease speed
        float yMove = (y/divider)*8; // change number to increase or decrease speed
    
        if(object1.position.x > destinationLocation.x)
            object1.position = CGPointMake(object1.position.x-xMove, object1.position.y);
    
        if(object1.position.x < destinationLocation.x)
            object1.position = CGPointMake(object1.position.x+xMove, object1.position.y);
    
        if(object1.position.y > destinationLocation.y)
            object1.position = CGPointMake(object1.position.x, object1.position.y-yMove);
    
        if(object1.position.y < destinationLocation.y)
            object1.position = CGPointMake(object1.position.x, object1.position.y+yMove);
    }
    
    @end
    

    【讨论】:

    • 感谢 sangony,但您在代码中的节点设置为跟随触摸的位置,这也可以通过运行 SKAction moveByX:different.x y:0 duration 来实现。我需要的是触摸屏幕上的任何地方,然后向左走。 +10x 盒子相对于其原始位置实时执行相同操作。
    • @adimona - 也许我不明白你的问题。我发布的代码将精灵从头到尾平滑地移动到触摸位置,如果触摸位置发生变化,它会在中途更改其路径。您不能使用 SKAction move 来执行此操作,因为除非您取消操作并开始新的操作,否则操作将继续到其最初设置的目的地。如果我正确理解您的第一条评论,您说您的对象“跳”到第一个触摸位置,然后顺利跟踪。我的代码从头到尾平稳地跟踪。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多