as3瓷砖游戏 - 如果角色跨越瓷砖,做一些事情

时间:2015-09-09 14:12:48

标签: actionscript-3 tile event-listener

所以我正在为学校制作平铺游戏而不是非常熟悉编程,所以如果答案很明显就道歉。

使用数组我创建了一个带有不同图块的地图 - 我想要的是当角色走过某个阵列然后发生一些事情 - 例如:重启游戏

我的代码全部在一个.as文件中:

package {

    import flash.display.MovieClip;
    import flash.events.*;

    public class main2 extends MovieClip {
        public function main2() {
            // Create map

            var mapWidth = 10;
            var mapHeight = 10;
            var tileSide = 32;
            var totalTiles = mapWidth * mapHeight;
            var myMap: Array = [
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 0, 0, 0, 2, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 3, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
            ];

            for (var i: int = 0; i < mapHeight; i++) {
                for (var u: int = 0; u < mapWidth; u++) {
                    var cell: MovieClip = new tile();
                    cell.gotoAndStop(myMap[i][u] + 1);
                    cell.x = tileSide * u
                    cell.y = tileSide * i
                    addChild(cell);
                }
            }
			
			// Hero management

            var heroSpawnX = 1
            var heroSpawnY = 1

            var character: hero = new hero();
            addChild(character)
            character.x = heroSpawnX * tileSide
            character.y = heroSpawnY * tileSide

            var heroX = heroSpawnX
            var heroY = heroSpawnY 			
			

            // Basic movement

            stage.addEventListener(KeyboardEvent.KEY_DOWN, movement);

            function movement(event: KeyboardEvent): void {
                if (event.keyCode == 40 && myMap[heroY + 1][heroX] != 1) {
                    character.gfx.rotation = 0
                    character.y += tileSide
                    heroY++ //Down
                }
                if (event.keyCode == 38 && myMap[heroY - 1][heroX] != 1) {
                    character.gfx.rotation = 180
                    character.y -= tileSide
                    heroY-- //Up
                }
                if (event.keyCode == 39 && myMap[heroY][heroX + 1] != 1) {
                    character.gfx.rotation = 270
                    character.x += tileSide
                    heroX++ //Right
                }
                if (event.keyCode == 37 && myMap[heroY][heroX - 1] != 1) {
                    character.gfx.rotation = 90
                    character.x -= tileSide
                    heroX-- //Left
                }
            } //movement function
			
        } // behind this line
    }

}

在我的数组中,第二行中有一个'2'。我希望我的角色穿过那个瓷砖并“死” - 但更多只是从他的产卵位置重新开始([1] [1])。我假设需要一个事件监听器,但在google一些代码之后仍然无法使其工作。

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:0)

将这些内容添加到您的代码中:

此函数返回指定图块的编号:

function getNumberAt(tileX:uint,tileY:uint){
    var value:uint=myMap[tileX][tyleY];
    return value
}

请注意,此函数的索引应从零开始(如果您希望第一个水平线和第二个垂直线,则应getNumberAt(0,1)

现在替换你的功能:

  function movement(event: KeyboardEvent): void {
            if (event.keyCode == 40 && myMap[heroY + 1][heroX] != 1) {
                character.gfx.rotation = 0
                character.y += tileSide
                heroY++ //Down
            }
            if (event.keyCode == 38 && myMap[heroY - 1][heroX] != 1) {
                character.gfx.rotation = 180
                character.y -= tileSide
                heroY-- //Up
            }
            if (event.keyCode == 39 && myMap[heroY][heroX + 1] != 1) {
                character.gfx.rotation = 270
                character.x += tileSide
                heroX++ //Right
            }
            if (event.keyCode == 37 && myMap[heroY][heroX - 1] != 1) {
                character.gfx.rotation = 90
                character.x -= tileSide
                heroX-- //Left
            }
            trace(getNumberAt(heroX,heroY))//test getNumberAt function


            if(getNumberAt(heroX,heroY)==2){
                //type your code for death here
            }

        } 

我知道这有帮助!