touchesMoved移动背景图片

时间:2015-02-09 14:15:33

标签: ios objective-c sprite-kit

现状

查看加载,加载backgroundImage 将reticle BOOL显示为false

-(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&amp; nsstring错误? backgroundImage.name = @“backgroundImage”;

1 个答案:

答案 0 :(得分:0)

问题一 拖动较小的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";

相关问题