Touch Function 方法中的多个函数 Sprite Kit

Posted

技术标签:

【中文标题】Touch Function 方法中的多个函数 Sprite Kit【英文标题】:Multiple functions in Touch Function methods Sprite Kit 【发布时间】:2014-04-05 05:04:00 【问题描述】:

请不要对此投反对票。如果这不是一个好问题,我会立即删除它,但这是我很困惑的问题。

我有一个横向视图的游戏。现在,屏幕左侧是供我的玩家四处移动的,而屏幕右侧是供其敌人四处移动的。好的,如果我在屏幕右侧的任何位置点击,我的玩家应该射击,但如果我在屏幕左侧点击,我的玩家应该移动。所以下面是我的代码,我收到了类似 Thread 1 Signal 或类似性质的错误。你能帮忙吗?

    // 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]]];
    

这是我得到的错误: 一旦我在应用程序停止周围移动我的玩家角色并且我从 main.m 文件中收到一个错误,上面写着 Thread 1: signal SIGABRT 并且在返回 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class ]));

【问题讨论】:

嘿,朋友,感谢您的编辑,但您的声誉损失了我两分。你能帮忙吗? :D 哪一行崩溃了?错误到底说了什么?添加异常断点 这是我得到的错误:当我在应用程序周围移动我的玩家角色时,我会从 main.m 文件中得到一个错误,上面写着 Thread 1: signal SIGABRT 并且在线返回 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 【参考方案1】:

试试这段代码,但有一点需要注意

玩家必须在枪声之间抬起手指,如果你想让他在屏幕上拖动手指时继续射击,那么你必须修改 touchesMoved 和 touchesBegan 中的条件以匹配 TouchesEnded 中的条件

p>
- (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]]];
    

希望这会有所帮助,如果有不清楚的地方,请随时询问。

【讨论】:

【参考方案2】:

首先是这个条件

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

构造不佳我希望您知道此条件将战区分配给触摸位置,然后如果结果点不等于 (0, 0) 那么此条件将是真实的,这在大多数情况下可能是正确的,但是我注意到您在上一行中给了它一个不同的分配

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

修改并告诉我们您要测试什么条件。

至于例外,我唯一能想到的就是触发这次崩溃的就是这一行

SKSpriteNode * projectile = sprites;

sprites 不能为 nil,并且无法从您共享的代码中推断出您如何处理 sprites 变量。


如果这对您没有帮助,那么我的问题是,当您移动角色时应用程序是否会崩溃 - 在您将手指从屏幕上移开后或动画结束后?

【讨论】:

好吧,我感谢您的努力,但让我修改这个问题。首先我必须说我想做什么。我的应用程序就像有两个敌人站在一起,他们每个人都有一半的屏幕,他们可以移动以获取弹药和健康。好吧,如果玩家抓起一些弹药,它可以用它射击,对吧?所以现在这里的复杂情况是触摸只会移动我的角色,但如果我抓住弹药然后按下敌人不会向它射击,但它会使我的玩家向它移动。那么我怎样才能编辑触摸功能,使其两者兼得。 顺便说一下这里的代码:CGPoint Combatzone = CGPointMake(self.size.width/2, self.size.height);这是如果我触摸玩家应该只射击而不是移动的区域。 抱歉耽误了我一段时间,我太忙了。查看我刚刚发布的新答案。

以上是关于Touch Function 方法中的多个函数 Sprite Kit的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Sencha Touch 2 视图中定义自己的函数

微信小程序 功能函数 touch触摸计时

没有可行的重载'='用于将std :: function回调赋值为成员函数

SAS 字符串常用函数

var和function定义方法的区别

js通用扩展方法——转自翘首以望