as3 瓷砖游戏 - 如果角色跨过瓷砖,请做某事
as3 Tile game - If character steps over tile, do something
所以我正在为学校开发一款拼图游戏,对编程不是很熟悉,如果答案很明显,我深表歉意。
我使用一个数组创建了一个具有不同图块的地图-我想要的是当角色走过某个数组时会发生一些事情-例如:重新启动游戏
我的代码都在一个 .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”。我希望我的角色穿过那块瓷砖,然后 'die'- 但更多的只是从他的重生位置 ([1][1]) 重新开始。我假设需要一个事件侦听器,但在谷歌搜索了一些代码后 - 仍然无法让它工作。
在此先感谢您的帮助
将这些片段添加到您的代码中:
此函数returns指定的图块数:
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
}
}
我希望这有帮助!
所以我正在为学校开发一款拼图游戏,对编程不是很熟悉,如果答案很明显,我深表歉意。
我使用一个数组创建了一个具有不同图块的地图-我想要的是当角色走过某个数组时会发生一些事情-例如:重新启动游戏
我的代码都在一个 .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”。我希望我的角色穿过那块瓷砖,然后 'die'- 但更多的只是从他的重生位置 ([1][1]) 重新开始。我假设需要一个事件侦听器,但在谷歌搜索了一些代码后 - 仍然无法让它工作。
在此先感谢您的帮助
将这些片段添加到您的代码中:
此函数returns指定的图块数:
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
}
}
我希望这有帮助!