繁体   English   中英

移动Sprite移动触摸

[英]move Sprite with touches moved

我正在使用touches move方法移动精灵。 当前精灵跳转到触摸屏幕的点但是我希望精灵只在直接触摸时移动。

我的代码:

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

    for (UITouch *touch in touches) {

        CGPoint location = [touch locationInNode:self];
        CGPoint newPosition = CGPointMake(location.x, self.size.height/2);

        self.sprite.position = newPosition;
     }
 }

检查触摸位置是否在精灵内部,如下所示:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    if(CGRectContainsPoint(self.sprite.boundingBox,positionInScene)) {

        CGPoint newPosition = CGPointMake(positionInScene.x, self.size.height/2);

        self.sprite.position = newPosition;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM