【问题标题】:touchesMoved move background imagetouchesMoved 移动背景图片
【发布时间】:2015-02-09 14:15:32
【问题描述】:

现状

视图加载,加载 backgroundImage 将reticle BOOL 显示为假

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    self.anchorPoint = CGPointMake(0.5, 0.5);
    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    //Add background
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"backgroundTemp" ofType:@"png"];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
    SKTexture *texture = [SKTexture textureWithImage:image];
    backgroundImage = [SKSpriteNode spriteNodeWithTexture:texture];
    backgroundImage.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
    backgroundImage.position = CGPointMake(0.5, 0.5);
    backgroundImage.zPosition = 0;
    [self addChild:backgroundImage];
    //END Add Background

    removeArray = [[NSMutableArray alloc] init];
    reticleShow = false;

}

所以在背景图片被加载后,显示出来。唯一的其他选择是按下屏幕以放大backgroundImage 以及加载reticle 图像。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    if (reticleShow == false) {
        //Add Reticle
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"reticle_001" ofType:@"png"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:bundle];
        SKTexture *texture = [SKTexture textureWithImage:image];
        SKSpriteNode *reticle = [SKSpriteNode spriteNodeWithTexture:texture];
        reticle.zPosition = 1;
        reticle.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
        reticle.position = CGPointMake(0.5, 0.5);
        [self addChild:reticle];

        [removeArray addObject:reticle];
        //End Add Reticle

        //Enlarge background
        backgroundImage.size = CGSizeMake(self.frame.size.width*3, self.frame.size.height*3);
        //End Enlarge background

        reticleShow = true;
    } else {
//        //Remove reticle
//        backgroundImage.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
//        [self removeChildrenInArray:removeArray];
//        reticleShow = false;
//        //end remove reticle

        //enable drag

        //end enable drag
    }
}

现在它提供了放大和布尔值防止进一步添加精灵,以及提供标线图像。

现在,加载完毕后。或者已经进行了初始印刷。我现在希望能够拖动背景。即移动背景并保持标线居中。

所以我做了这个,

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKNode *node = [self nodeAtPoint:location];

        if ([node.name isEqualToString:backgroundImage]) { <--- ERROR HERE sksprite node to type nsstring

            CGPoint previousLocation = [touch previousLocationInNode:self];
            float diff = location.y - previousLocation.y;
            CGPoint newPosition = CGPointMake(node.position.x, node.position.y +diff);

            if (newPosition.y > 230) {
                newPosition.y = 230;
            }
            node.position = newPosition;
        }
    }
}

三个问题

  1. 如何在较小的 z-index 上拖动图像?即拖动背景而不是标线?

  2. 背景图片放大后如何移动?

  3. sknode & nsstring错误如何传递? backgroundImage.name = @"backgroundImage";

【问题讨论】:

  • 3 - SKNode 有一个 name 属性。你需要设置它。例如,myNode.name = @"myName";然后使用 if([myNode.name isEqualToString:@"myName"]) 检查它
  • @sangony 太好了,这回答了我的一个问题。谢谢
  • @sangony 现在我可以在 z-index 0 上移动背景,但是,如果覆盖了标线 z-index 1,我将无法移动背景图像。
  • 如果 zPosition 高于背景的节点不需要用户交互,例如触摸事件,请将其 userInteractionEnabled 属性设置为 NO。

标签: ios objective-c sprite-kit


【解决方案1】:

问题一 拖动较小的 Z-Index

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKNode *node = [backgroundImage nodeAtPoint:location];

        if ([node.name isEqualToString:@"backgroundImage"]) {

            CGPoint previousLocation = [touch previousLocationInNode:self];
            float ydiff = location.y - previousLocation.y;
            float xdiff = location.x - previousLocation.x;
            CGPoint newPosition = CGPointMake(node.position.x - xdiff, node.position.y - ydiff);

            if (newPosition.y > 230) {
                newPosition.y = 230;
            }
            node.position = newPosition;
        }
    }
}

插入这个 reticle.userInteractionEnabled = NO;

问题二

不影响放大

问题三

命名 SkSpriteNode 进行检测

backgroundImage.name = @"backgroundImage";

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多