在我目前的項目中,我正在使用UITapGestureRecognizer。我已經對它進行了編碼,以便當屏幕的左側被輕敲時,一個對象會移動,並且當輕擊屏幕的右側時,另一個對象會移動。但是,手勢識別器僅在右側輕敲時才起作用。另外,我想這樣做,以便SKActions可以執行,如果雙方同時攻擊。我不明白我做錯了什麼。我的代碼如下。在此先感謝UITapGestureRecognizer將無法正常執行
在viewDidLoad中
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
[tap1 setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tap1];
view.multipleTouchEnabled = YES;
-(void)tap1:(UIGestureRecognizer *)gestureRecognizer {
SKNode *person11 = [self childNodeWithName:@"person1"];
SKNode *person2 = [self childNodeWithName:@"person2"];
CGPoint pt = [gestureRecognizer locationInView:self.view];
if (pt.x > (self.view.bounds.size.width/2))
{
if (person2.position.x == CGRectGetMidX(self.frame) + 400) {
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveLeft2];
}
if (person2.position.x == CGRectGetMidX(self.frame) + 90) {
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveRight2];
}
}
if (pt.x < (self.view.bounds.size.width/2)){
if (person11.position.x == CGRectGetMidX(self.frame) - 90) {
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person11 runAction:moveLeft2];
}
if (person11.position.x == CGRectGetMidX(self.frame) - 400) {
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person11 runAction:moveRight2];
}
}
}
謝謝你的幫助,該方法的作品。無論使用什麼方法,Person1都不會被識別。 SKAction不影響person1。我不知道這是爲什麼,你知道我可能做錯了什麼? – d33
@ d33,你是否嘗試過調試(打印到控制檯以查看代碼是否執行)。 – Dopapp