如何以编程方式删除JSTileMap中的块

时间:2015-05-19 13:04:40

标签: swift jstilemap

我目前正在使用JSTileMap和Swift。我希望我的玩家能够击中障碍并让它突破。有谁知道如何做到这一点?另外,我如何设置此块的破坏动画?这是我的代码: `

var map = JSTileMap()
var player = Player()
var previousUpdateTime = CFTimeInterval()
var walls = TMXLayer()
var hazards = TMXLayer()
var gameIsOver = Bool()
var backgroundMusicPlayer = AVAudioPlayer()

override func didMoveToView(view: SKView) {

    self.backgroundColor = SKColor(red: 0.4, green: 0.4, blue: 0.95, alpha: 1.0)
    self.userInteractionEnabled = true

    playBackgroundMusic()
    map = JSTileMap(named: "level1.tmx")
    map.xScale = 1.3
    map.yScale = 1.3
    addChild(map)

    walls = map.layerNamed("walls")
    hazards = map.layerNamed("hazards")

    player = Player(imageNamed: "koalio_stand")
    player.position = CGPointMake(100, 50)
    player.zPosition = 15
    map.addChild(player)

}

func playBackgroundMusic() -> () {
    var error: NSError?
    let backgroundMusicURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("level1", ofType: "mp3")!)
    backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: backgroundMusicURL, error: &error)
    backgroundMusicPlayer.numberOfLoops = -1
    backgroundMusicPlayer.prepareToPlay()
    backgroundMusicPlayer.play()
}

func tileRectFromTileCoords(tileCoords: CGPoint) -> CGRect {
    let levelHeightInPixels = map.mapSize.height * map.tileSize.height
    let origin = CGPointMake(tileCoords.x * map.tileSize.width, levelHeightInPixels - ((tileCoords.y + 1) * map.tileSize.height))
    return CGRectMake(origin.x, origin.y, map.tileSize.width, map.tileSize.height)
}

func tileGIDAtTileCoord(coord: CGPoint, layer:TMXLayer) -> NSInteger {
    let layerInfo = layer.layerInfo
    return layerInfo.tileGidAtCoord(coord)
}

func checkForAndResolveCollisionForPlayer(player: Player, layer:TMXLayer){

    let indices = [7, 1, 3, 5, 0, 2, 6, 8]
    player.onGround = false

    for var i = 0; i < 8; i++ {

        let tileIndex = Int(indices[i])
        let playerRect = player.collisionBoundingBox()
        let playerCoord = layer.coordForPoint(player.desiredPosition)

        if playerCoord.y >= self.map.mapSize.height - 1 {
            gameOver(true)
            return
        }

        let tileColumn = tileIndex % 3
        let tileRow = tileIndex / 3
        let tileCoord = CGPointMake(playerCoord.x + CGFloat(tileColumn - 1), playerCoord.y + CGFloat(tileRow - 1))
        let gid = self.tileGIDAtTileCoord(tileCoord, layer: layer)

        if gid != 0 {
            var tileRect = self.tileRectFromTileCoords(tileCoord)
            if CGRectIntersectsRect(playerRect, tileRect) {
                var intersection = CGRectIntersection(playerRect, tileRect)
                if tileIndex == 7 {
                    player.desiredPosition = CGPointMake(player.desiredPosition.x, player.desiredPosition.y + intersection.size.height)
                    player.velocity = CGPointMake(player.velocity.x, 0.0)
                    player.onGround = true
                } else if tileIndex == 1 {
                    player.desiredPosition = CGPointMake(player.desiredPosition.x, player.desiredPosition.y - intersection.size.height)




                } else if tileIndex == 3 {
                    player.desiredPosition = CGPointMake(player.desiredPosition.x + intersection.size.width, player.desiredPosition.y)
                } else if tileIndex == 5 {
                    player.desiredPosition = CGPointMake(player.desiredPosition.x - intersection.size.width, player.desiredPosition.y)
                } else {
                    if intersection.size.width > intersection.size.height {
                        player.velocity = CGPointMake(player.velocity.x, 0.0)
                        var intersectionHeight = Float()
                        if tileIndex > 4 {
                            intersectionHeight = Float(intersection.size.height)
                            player.onGround = true
                        } else {
                            intersectionHeight =
                                Float(-intersection.size.height)
                        }
                        player.desiredPosition = CGPointMake(player.desiredPosition.x, player.desiredPosition.y + intersection.size.height)
                    } else {
                        var intersectionWidth = Float()
                        if tileIndex == 6 || tileIndex == 0 {
                            intersectionWidth = Float(intersection.size.width)
                        } else {
                            intersectionWidth = Float(-intersection.size.width)
                        }
                        player.desiredPosition = CGPointMake(player.desiredPosition.x + intersection.size.width , player.desiredPosition.y)

                            //(player.desiredPosition.x + intersectionWidth, player.desiredPosition.y)
                    }
                }
            }
        }
    }
    player.position = player.desiredPosition
}

func handleHazardCollisions(player: Player){

    if gameIsOver {
        return
    }

    let indices = [7, 1, 3, 5, 0, 2, 6, 8]
    for var i = 0; i < 8; i++ {
        let tileIndex = Int(indices[i])

        let playerRect = player.collisionBoundingBox()
        let playerCoord = hazards.coordForPoint(player.desiredPosition)

        let tileColumn = tileIndex % 3
        let tileRow = tileIndex / 3
        let tileCoord = CGPointMake(playerCoord.x + CGFloat(tileColumn - 1), playerCoord.y + CGFloat(tileRow - 1))

        let gid = self.tileGIDAtTileCoord(tileCoord, layer: hazards)

        if gid != 0 {
            let tileRect = self.tileRectFromTileCoords(tileCoord)
            if CGRectIntersectsRect(playerRect, tileRect) {
                gameOver(true)
            }
        }
    }
}

func breakblock(){


}





func checkForWin() {
    // 10 tiles before the map ends
    if player.position.x > ((map.mapSize.width * map.tileSize.width) - (map.tileSize.width * 10)) {
        self.gameOver(true)
    }
}

func gameOver(won: Bool){

    gameIsOver = true
    self.runAction(SKAction.playSoundFileNamed("hurt.wav", waitForCompletion: false))

    var gameText = ""

     if player.position.x > ((map.mapSize.width * map.tileSize.width) - (map.tileSize.width * 10)) {
        gameText = "You Won!"
    } else {
        gameText = "You died!!"
    }

    let endGameLabel = SKLabelNode(fontNamed: "Marker Felt")
    endGameLabel.text = gameText
    endGameLabel.fontSize = 40.0
    endGameLabel.position = CGPointMake(self.size.width / 2, self.size.height / 1.7)
    self.addChild(endGameLabel)

   let replay = UIButton()
    replay.tag = 321
    let replayImage = UIImage(named: "replay.png") as UIImage!
    replay.setImage(replayImage, forState: UIControlState.Normal)
    replay.addTarget(self, action: "replayGame", forControlEvents: UIControlEvents.TouchUpInside)
    replay.frame = CGRectMake(self.size.width / 2 - replayImage!.size.width / 2, self.size.height / 2 - replayImage!.size.height / 2, 80, 30)
    self.view!.addSubview(replay)



}

func replayGame() {
    self.view!.viewWithTag(321)!.removeFromSuperview()
   self.view!.presentScene(GameScene(size: view!.bounds.size ))
    setViewpointCenter(player.position)
    map.scene!.size.height = 5000

}

func setViewpointCenter(position: CGPoint) {
    var x = returnMax(position.x, self.size.width / 2)
    var y = returnMax(position.y, self.size.height / 2)
    x = returnMin(x, (map.mapSize.width * map.tileSize.width / 2) )
    y = returnMin(y, (map.mapSize.height * map.tileSize.height / 2) )
    let actualPosition = CGPointMake(x, y)
    let centerOfView = CGPointMake(self.size.width / 2, self.size.height / 2)
    let viewPoint = CGPointSubtract(centerOfView, actualPosition)
    map.position = viewPoint
}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        var touchLocation = touch.locationInNode(self)
        if touchLocation.x > self.size.width/2 {
            player.mightAsWellJump = true
        } else {
            player.forwardMarch = true
        }
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let halfWidth = self.size.width / 2.0
        var touchLocation = touch.locationInNode(self)
        var previousTouchLocation = touch.previousLocationInNode(self)
        if touchLocation.x > halfWidth && previousTouchLocation.x <= halfWidth {
            player.forwardMarch = false
            player.mightAsWellJump = true
        } else if previousTouchLocation.x > halfWidth && touchLocation.x <= halfWidth {
            player.forwardMarch = true
            player.mightAsWellJump = false
        }
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        var touchLocation = touch.locationInNode(self)
        if touchLocation.x < self.size.width / 2 {
            player.forwardMarch = false
        } else {
            player.mightAsWellJump = false
        }
    }
}

override func update(currentTime: CFTimeInterval) {

    if gameIsOver {
        return
    }

    var delta = currentTime - previousUpdateTime

    if delta > 0.02 {
        delta = 0.02
    }

    previousUpdateTime = currentTime
    player.updatePlayer(delta)

    checkForAndResolveCollisionForPlayer(player, layer: walls)
    handleHazardCollisions(player)
    checkForWin()
    setViewpointCenter(player.position)

}

} `

目前,当我运行我的应用程序时,我的播放器可以向前奔跑并跳转。他会击中积木但没有任何反应。我尝试了一切。也许块id可以被另一个替换?任何见解都将非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

首先检索您的块,您可以使用JSTileMap中的此函数。 Map是您已加载的地图的名称。

由于您使用的是JSTileMap,我假设您使用的是Tiled,因为它是使用JSTileMap的最佳免费工具。您可以在那里找到您的图层名称,并能够在该图层中选择或删除块。

let layer : TMXLayer = map.layerNamed("YourLayerName") 

Node是SKSpriteNode的名称,在您的情况下是您要删除的块。

let node = layer.tileAt(star.position)

要动态破坏这个区块,请访问Apple网站上的SKAction Swift Doc。