マップ内を移動するキャラクター(perso)が配列で生成するようにプログラムしようとしています。そこに、私の「Tuiles」MovieClipを呼び出す変数「t」があり、「map」配列上にそれぞれ「1」の新しいタイルを持つグリッド(グリル)を作成します。最初に1つのタイルの子で衝突関数を修正したので、「マップ」関数と「グリッド」関数を作成しました。ワールドクリエイトとキャラクターの衝突には「t」変数が必要です。関数「creeDecor」に変数を設定すると、Flashはそれを適切に生成しますが、衝突関数に変数「t」が存在しないと言っています。コードの上に「t」変数を一般変数として配置すると、Flashはステージの右下に1つのタイルを生成し、衝突関数が正常に機能します...
どうすればそれらを2回混合できますか?出来ますか ?
コード全体をお見せします。それを機能させたい場合は、MovieClipクラス名「Tuiles」(正方形:32 * 32)と2番目のMC「Perso」を作成する必要があります。
//Creation of perso
var perso:Perso = new Perso();
var grille:MovieClip = new MovieClip();
var t:Tuiles = new Tuiles();
var T:int = 32;
var map:Array = [
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
]
// temporary stockage
var stock:Array = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
]
creeDecor()
initHero();
stage.addEventListener(KeyboardEvent.KEY_DOWN, clavierDown);
stage.addEventListener(KeyboardEvent.KEY_UP, clavierUp);
stage.addEventListener(Event.ENTER_FRAME, animation);
// Stage collision
var minX = 0;
var minY = 0;
var maxX = stage.stageWidth - perso.width;
var maxY = stage.stageHeight - perso.height;
var speedHero = 5;
function clavierDown(e)
{
switch(e.keyCode)
{
case 37:
perso.speedX = -speedHero;
break;
case 39:
perso.speedX = speedHero;
break;
case 38:
perso.speedY = -speedHero;
break;
case 40:
perso.speedY = speedHero;
break;
}
}
function clavierUp(e)
{
switch(e.keyCode)
{
case 37:
perso.speedX = 0;
break;
case 39:
perso.speedX = 0;
break;
case 38:
perso.speedY = 0;
break;
case 40:
perso.speedY = 0;
break;
}
}
function animation(e)
{
animeHero();
}
// Stage border collision function
function animeHero()
{
if(perso.x + perso.speedX >= minX && perso.x + perso.speedX <= maxX)
{
perso.x += perso.speedX;
} else if (perso.speedX < 0)
{
perso.x -= Math.abs(perso.x-minX);
//trace(Math.abs(perso.x-minX));
} else {
perso.x += Math.abs(perso.x-maxX) ;
}
if(perso.y + perso.speedY >= minY && perso.y + perso.speedY <= maxY)
{
perso.y += perso.speedY;
}
collision();
}
function initHero()
{
perso.speedX = 0;
perso.speedY = 0;
perso.x = 200;
perso.y = 300;
addChild(perso);
}
function collision() {
if (perso.hitTestObject(t))
{
if (perso.x >= t.x + t.width - speedHero && perso.x <= t.x +t.width + 2)
{
perso.x = t.x + t.width;
}
if (perso.x + perso.width >= t.x - 2 && perso.x + perso.width <= t.x + speedHero)
{
perso.x = t.x - perso.width;
}
if (perso.y >= t.y + t.height - speedHero && perso.y <= t.y +t.height + 2)
{
perso.y = t.y + t.height;
}
if (perso.y + perso.height >= t.y - 2 && perso.y + perso.height <= t.y + speedHero)
{
perso.y = t.y - perso.height;
}
}
}
// level creation
function creeDecor():void{
for (var i:int=0; i<20; i++){ // boucle sur les 20 colonnes
for (var j:int=0; j<15; j++){ // boucle sur les 15 lignes de chaque colonne
var f:int = map[j][i]
if(f>0) {
t.x= i*T;
t.y= j*T;
t.gotoAndStop(f)
grille.addChild(t)
// Specialisation
if (f==1)
stock[j][i] = t;
}
else
{
stock[j][i] = []
}
}
}
}
addChild(grille)