【问题标题】:Sprite - How to only touch certain parts of screen?Sprite - 如何只触摸屏幕的某些部分?
【发布时间】:2014-09-02 01:25:27
【问题描述】:

我正在制作游戏,我只想让用户触摸屏幕边缘?有什么办法可以做到吗?

以下代码允许用户触摸屏幕上的任何位置,但我希望他们只触摸屏幕的某些部分。

感谢您的时间和答案

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

    }
}

【问题讨论】:

    标签: ios touch sprite-kit sprite area


    【解决方案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];
        }
    
    }
    

    【讨论】:

    • 即使它不起作用,您解决此问题的方法也是完全正确的。
    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多