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

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

我有一个风景游戏。 现在,屏幕左边是我的玩家左右移动,屏幕右边是敌人移动。 好,现在,如果我在屏幕右侧的任何位置选中,我的播放器应该可以拍摄,但是如果我在屏幕的左侧标签,我的播放器应该移动。 所以下面是我的代码,我得到这样的线程1信号或类似的东西在这种性质的错误。 你能帮忙吗?

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

这是我得到的错误:只要我移动我的玩家angular色周围的应用程序停止,并从main.m文件中得到一个错误,说线程1:信号SIGABRT,并在返回UIApplicationMain(argc,argv, nil,NSStringFromClass([AppDelegate class]));

试试这个代码,但要注意

玩家必须将手指在枪声之间抬起,如果要让他在屏幕上拖动手指时继续拍摄,则必须修改touchesMoved和touches中的条件,以匹配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]]]; } 

希望这有帮助,随意问,如果有什么不清楚。

首先这个条件

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

我希望你知道,这种情况下分配战斗区域接触的位置,那么如果得到的点不等于(0,0),那么这个条件将是真实的,这可能是真实的大部分时间,但是我注意到你在前一行中给了它不同的任务

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

修改并告诉我们你想要testing什么条件。

至于例外,唯一我能想到的是,我的触发器这次崩溃将是这条线

 SKSpriteNode * projectile = sprites; 

精灵不能为零,也没有办法从代码中推断出你是如何处理精灵variables的。


如果这对我的问题没有帮助,那么当您移动angular色时,应用程序会崩溃 – 从屏幕上移除手指或animation结束之后?