Multiple functions in Touch Function methods Sprite Kit

三世轮回 提交于 2020-01-05 04:59:09

问题


Please do not vote this down. If it's not a good question I will remove it immediately but here is something I am very confused with.

I have a game in landscape view. Now, left of the screen is for my player to move around and right side of the screen is for its enemy to move around. Okay now, If I tab anywhere on right side of the screen, my player should shoot but if I tab left side of the screen, my player should move. so below is my code and I'm getting errors like this Thread 1 Signal or something in that nature. can you please help?

    // TOUCH FUNCTIONS
       - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
              {
                     UITouch *touch = [touches anyObject];
                     CGPoint touchLocation = [touch locationInNode:self.scene];
                     [self movePlayerToward:touchLocation];
              }
       - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
              {
                     UITouch *touch = [touches anyObject];
                     CGPoint touchLocation = [touch locationInNode:self.scene];
                     [self movePlayerToward:touchLocation];
              }
       - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
              {
                     UITouch *touch = [touches anyObject];
                     CGPoint touchLocation = [touch locationInNode:self.scene];
                     [self movePlayerToward:touchLocation];
                     CGPoint Combatzone = CGPointMake(self.size.width/2, self.size.height);
                     if((Combatzone.x = touchLocation.x) && (Combatzone.y = touchLocation.y))
              {
              // 1 - Set up initial location of projectile
              SKSpriteNode * projectile = sprites;
              projectile.position = _Player.position;
              // 2- Determine offset of location to projectile
              CGPoint offset = rwSub(touchLocation, projectile.position);
              [self addChild:projectile];
              // 5 - Get the direction of where to shoot
              CGPoint direction = rwNormalize(offset);
             // 6 - Make it shoot far enough to be guaranteed off screen
             CGPoint shootAmount = rwMult(direction, 1000);
             // 7 - Add the shoot amount to the current position
            CGPoint realDest = rwAdd(shootAmount, projectile.position);
        // 8 - Create the actions
        float velocity = 480.0/1.0;
        float realMoveDuration = self.size.width / velocity;
        SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
        SKAction * actionMoveDone = [SKAction removeFromParent];
        [projectile runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
    }
}

This is the error I get: as soon as I move my player character around the app stops and I get an error from main.m file which says Thread 1: signal SIGABRT and that is on the line that returns UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));


回答1:


Try this code, but something to note

the player has to lift his finger between gunshots, if you want him to keep shooting as he drags his finger on the screen, then you have to modify the conditions in the touchesMoved and touchesBegan to match the one in the TouchesEnded

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self.scene];
        if(touchLocation.x > self.size.width/2 )
            [self movePlayerToward:touchLocation];
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self.scene];
        if(touchLocation.x > self.size.width/2 )
            [self movePlayerToward:touchLocation];
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint touchLocation = [touch locationInNode:self.scene];
        if(touchLocation.x <= self.size.width/2 )//left side will shoot to change it to right side, invert the inequality sign
            [self shoot:touchLocation];
        else
            [self movePlayerToward:touchLocation];
    }

    -(void)shoot:(CGPoint)touchLocation{
        // 1 - Set up initial location of projectile
        SKSpriteNode * projectile = sprites;
        projectile.position = _Player.position;
        // 2- Determine offset of location to projectile
        CGPoint offset = rwSub(touchLocation, projectile.position);
        [self addChild:projectile];
        // 5 - Get the direction of where to shoot
        CGPoint direction = rwNormalize(offset);
        // 6 - Make it shoot far enough to be guaranteed off screen
        CGPoint shootAmount = rwMult(direction, 1000);
        // 7 - Add the shoot amount to the current position
        CGPoint realDest = rwAdd(shootAmount, projectile.position);
        // 8 - Create the actions
        float velocity = 480.0/1.0;
        float realMoveDuration = self.size.width / velocity;
        SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
        SKAction * actionMoveDone = [SKAction removeFromParent];
        [projectile runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
    }

Hope this helps, feel free to ask if something is unclear.




回答2:


First of all this condition

if((Combatzone.x = touchLocation.x) && (Combatzone.y = touchLocation.y))

is poorly constructed I hope you are aware that this condition assigns combat zone to touch location then if the resulting point is not equal to (0, 0) then this condition would be truly which is likely to be true most of the time, however I noticed that you gave it a different assignment in the previous line

 CGPoint Combatzone = CGPointMake(self.size.width/2, self.size.height);

revise and tell us what condition are you trying to test.

as for the exception the only thing I can think of that my trigger this crash would be this line

SKSpriteNode * projectile = sprites;

sprites cannot be nil, and there is no way to infer from the code you shared how you handle the sprites variable.


if this doesn't help then my question to you, does the app crash while you are moving the character - after you have removed your finger from the screen or after the animation has ended?



来源:https://stackoverflow.com/questions/22876813/multiple-functions-in-touch-function-methods-sprite-kit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!