我正在制作游戏,我只希望用户触摸屏幕的边缘?我有什么方法可以做到这一点吗?
以下代码可让用户触摸屏幕上的任何位置,但我希望他们只触摸屏幕的某些部分。
感谢您的时间和答案
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"wall"];
sprite.position = location;
sprite.scale = 0.5;
sprite.name = @"wall"; // Needed For Release Thing
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.physicsBody.categoryBitMask = wallCategory;
SKAction *wait = [SKAction waitForDuration:0.4];
SKAction *delete = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[wait, delete, ]];
[sprite runAction: sequence];
[self addChild:sprite];
}
}
答案 0 :(得分:1)
我没有测试这个,所以我不确定它是否会起作用
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if (((location.x < leftTouchArea) || (location.x > rightTouchArea)) && ((location.y < upperTouchArea) || (location.y > lowerTouchArea)))
{
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"wall"];
sprite.position = location;
sprite.scale = 0.5;
sprite.name = @"wall"; // Needed For Release Thing
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.physicsBody.categoryBitMask = wallCategory;
SKAction *wait = [SKAction waitForDuration:0.4];
SKAction *delete = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[wait, delete, ]];
[sprite runAction: sequence];
[self addChild:sprite];
}
}