AS3:基于图块的运动系统
AS3: Tile based movement system
对 as3 非常陌生,使用找到的教程 here 来尝试基于图块的移动。但是,我似乎无法使代码正常工作。我不断收到错误代码:
"1046: Type was not found or was not a compile-time constant:hero."
参考的行是:
var character: hero = new hero();
我的完整代码是:
package {
import flash.display.MovieClip;
import flash.events.*;
public class main2 extends MovieClip {
var hero;
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, 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, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 1, 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 = 4;
var heroSpawnY = 2;
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] == 0) {
character.gfx.rotation = 0;
character.y += tileSide;
heroY++;
}
if (event.keyCode == 38 && myMap[heroY - 1][heroX] == 0) {
character.gfx.rotation = 180;
character.y -= tileSide;
heroY--;
}
if (event.keyCode == 39 && myMap[heroY][heroX + 1] == 0) {
character.gfx.rotation = 270;
character.x += tileSide;
heroX++;
}
if (event.keyCode == 37 && myMap[heroY][heroX - 1] == 0) {
character.gfx.rotation = 90;
character.x -= tileSide;
heroX--;
}
}
}
}
}
任何关于这个问题的帮助都会很棒,现在已经解决了一个小时。
此外,如果您对 as3 资源有任何建议,请告诉我...特别是基于平铺的系统。
感谢已进。
该错误表示未找到您的 class Hero
。您应该将 hero.as
文件放在 main2.as
文件所在的位置(与 main2.as
相同的位置)。然后导入它:
import Hero;
以及我对 as3 资源的推荐:
1.http://republicofcode.com:I 认为最适合开始的站点 as3.I 从它开始。
2.http://kirupa.com: 一个不错的网站,文章很多
3.http://flashandmath.com:专业人士
别忘了 Adobe ActionScript-3 API 参考资料:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/
I H☺ P E 这有帮助!
对 as3 非常陌生,使用找到的教程 here 来尝试基于图块的移动。但是,我似乎无法使代码正常工作。我不断收到错误代码:
"1046: Type was not found or was not a compile-time constant:hero."
参考的行是:
var character: hero = new hero();
我的完整代码是:
package {
import flash.display.MovieClip;
import flash.events.*;
public class main2 extends MovieClip {
var hero;
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, 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, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 1, 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 = 4;
var heroSpawnY = 2;
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] == 0) {
character.gfx.rotation = 0;
character.y += tileSide;
heroY++;
}
if (event.keyCode == 38 && myMap[heroY - 1][heroX] == 0) {
character.gfx.rotation = 180;
character.y -= tileSide;
heroY--;
}
if (event.keyCode == 39 && myMap[heroY][heroX + 1] == 0) {
character.gfx.rotation = 270;
character.x += tileSide;
heroX++;
}
if (event.keyCode == 37 && myMap[heroY][heroX - 1] == 0) {
character.gfx.rotation = 90;
character.x -= tileSide;
heroX--;
}
}
}
}
}
任何关于这个问题的帮助都会很棒,现在已经解决了一个小时。
此外,如果您对 as3 资源有任何建议,请告诉我...特别是基于平铺的系统。
感谢已进。
该错误表示未找到您的 class Hero
。您应该将 hero.as
文件放在 main2.as
文件所在的位置(与 main2.as
相同的位置)。然后导入它:
import Hero;
以及我对 as3 资源的推荐:
1.http://republicofcode.com:I 认为最适合开始的站点 as3.I 从它开始。
2.http://kirupa.com: 一个不错的网站,文章很多
3.http://flashandmath.com:专业人士
别忘了 Adobe ActionScript-3 API 参考资料:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/
I H☺ P E 这有帮助!