Konrāds Bušs
Konrāds Bušs

Reputation: 134

Enlarge tapping area for node. ios swift

I have made simple game, but to make it easier to play I want to enlarge node area where to tap. Now I have made that flies are flying random path, and while they are flying I want to tap on it. But now it is very hard to tap on it because the node radius is to small.

Here I am creating sprite, and all the time keeping the count of five.

func updateFlyCount() {
    if  nodeCount < 5 {
        self.addFly()
    }
}


func addFly() {

    // Create sprite
    let fly = SKSpriteNode(imageNamed: "fly")
    fly.name = "fly"

    // Add the fly to the scene
    addChild(fly)

    // Determine speed of the fly
    let actualDuration = random(min: CGFloat(1.5), max: CGFloat(5.0))

    var pointWhereToStart: CGPoint = CGPointMake(0, screenSize.height)

    let randomSide = round(random(min: 1, max: 3))
    print("randomside made\(randomSide)")
    if randomSide == 1 {
        let randomStartingPoint = random(min: screenSize.height * 0.5, max:  screenSize.height)
        pointWhereToStart = CGPointMake (0, randomStartingPoint)
    } else if randomSide == 2 {
        let randomStartingPoint = random(min: 0, max:  screenSize.width)
        pointWhereToStart = CGPointMake (randomStartingPoint, screenSize.height)
    } else if randomSide == 3 {
        let randomStartingPoint = random(min: screenSize.height * 0.5, max:  screenSize.height)
        pointWhereToStart = CGPointMake (screenSize.width, randomStartingPoint)
    }

    let randomPoint1x = random(min: 0, max: screenSize.width)
    let randomPoint2y = random(min: 0, max: screenSize.height)
    let randomPoint3x = random(min: 0, max: screenSize.width)
    let randomPoint4y = random(min: 0, max: screenSize.height)

    let cgpath: CGMutablePathRef = CGPathCreateMutable()
    let startingPoint: CGPoint = pointWhereToStart
    let controlPoint1: CGPoint = CGPointMake(randomPoint1x, randomPoint2y)
    let controlPoint2: CGPoint = CGPointMake(randomPoint3x, randomPoint4y)
    let endingPoint: CGPoint = CGPointMake(screenSize.width * 0.5, screenSize.height * 0.5)
    CGPathMoveToPoint(cgpath, nil, startingPoint.x, startingPoint.y)
    CGPathAddCurveToPoint(cgpath, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endingPoint.x, endingPoint.y)
    let enemyCurve: SKAction = SKAction.followPath(cgpath, asOffset: false, orientToPath: false, duration: NSTimeInterval(actualDuration))
    fly.runAction(SKAction.sequence([SKAction.waitForDuration(0.5), enemyCurve]))

    updateFlyCount()
}

And here is how I remove the node

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches {
        print("tapped")
        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)

        if (touchedNode.name == "fly") {
            touchedNode.removeFromParent()
            addFly()
        }
    }
}

Upvotes: 0

Views: 86

Answers (1)

ThrowingSpoon
ThrowingSpoon

Reputation: 871

Create an SKNode with the size you'd like and then add the SKSpriteNode to it as a child.

Then in your touchesBegan check whether the SKNode was hit rather than the sprite.

You'll have to run the action against the SKNode rather than the fly as well.

Upvotes: 1

Related Questions