0

我一直在尝试在 AS3 中制作等距游戏。我正在使用一个影片剪辑来阻止角色在树等对象中移动,但我必须为我使用它的每棵树都赋予影片剪辑一个新的实例名称,然后将其添加到一个名为 BarriersV 的数组中。

我想做的只是将一个影片剪辑添加到数组中并以某种方式重用它,但据我所知,您不能将实例名称用于不止一件事,只有最近的一个才能工作。有没有更好的方法来获得相同的结果?

这是我所有的代码:

var isRight:Boolean=false
var isLeft:Boolean=false
var isUp:Boolean=false
var isDown:Boolean=false
var isSpace:Boolean=false
var xspeed:Number=0
var yspeed:Number=0
var maxspeed:Number=9
var accel:Number=1
var sprites:Array = [];
var barriersV:Array = [];
var barriersH:Array = [];
var animationState:String = "idle";
var lastbtnpress:String = "none";

import flash.events.Event;
import flash.geom.Rectangle;
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(evt:Event){
 root.scrollRect = new Rectangle(mc.x - stage.stageWidth/2, mc.y - stage.stageHeight/1.5, stage.stageWidth, stage.stageHeight);
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
function downKey(event:KeyboardEvent){
 if(event.keyCode==68){
 isRight=true
 isLeft=false
 mc.scaleX = +0.29;
 }
 if(event.keyCode==65){
 isLeft=true
 isRight=false
 mc.scaleX = -0.29;
 }
 if(event.keyCode==87){
 isUp=true}
 if(event.keyCode==83){
 isDown=true}
 if(event.keyCode==32){
 isSpace=true}
}

stage.addEventListener(KeyboardEvent.KEY_UP, upKey);
function upKey(event:KeyboardEvent){
 if(event.keyCode==68){
 isRight=false}
 if(event.keyCode==65){
 isLeft=false}
 if(event.keyCode==87){
 isUp=false}
 if(event.keyCode==83){
 isDown=false}
 if(event.keyCode==32){
 isSpace=false}
}

stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(Event){
// if right is pressed and speed didnt hit the limit, increase speed
if(isRight==true && xspeed<maxspeed){xspeed+=4}
// if left is pressed and speed didnt hit the limit, increase speed (the other way)
if(isLeft==true && xspeed>-maxspeed){xspeed-=4}
// if speed is more than 0, decrease
if(xspeed>0){xspeed-=accel}
// if speed is less than 0, increase
if(xspeed<0){xspeed+=accel}

sortView()

// just like x, but with y
if(isDown==true && yspeed<maxspeed){yspeed+=4}
if(isUp==true && yspeed>-maxspeed){yspeed-=4}
if(yspeed>0){yspeed-=accel}
if(yspeed<0){yspeed+=accel}

// apply speed to movieclip
mc.x+=xspeed
mc.y+=yspeed


for(var i = 0; i < 500; i++)

    if ((mc.x < barriersH[i].x) && barriersH[i].hitTestObject(mc.mcBounds)){
        xspeed += 0;
        mc.x = mc.x-9;
    }
    else if ((mc.x > barriersH[i].x) && barriersH[i].hitTestObject(mc.mcBounds)){
        xspeed -= 0;
        mc.x = mc.x+9;
    }

for(var a = 0; a < 500; a++)
    if ((mc.y < barriersV[a].y) && barriersV[a].hitTestObject(mc.mcBounds)){
        yspeed += 0;
        mc.y = mc.y-9;
    }
    else if ((mc.y > barriersV[a].y) && barriersV[a].hitTestObject(mc.mcBounds)){
        yspeed -= 0;
        mc.y = mc.y+9;
    }

if(( isLeft == true || isRight == true || isUp == true || isDown == true || xspeed > accel || xspeed < accel *-1 || yspeed > accel|| yspeed < accel *-1)){
     animationState = "running";
}
if (isSpace == true){
    animationState = "jumping";
}
if(mc.currentLabel != animationState){
     mc.gotoAndStop(animationState);
}
if(( isLeft == false || isRight == false || isUp == false || isDown == false || xspeed == 0|| yspeed == 0)){
    animationState = "idle";
    }
}
for(var a = 0; a < 500; a++)
{
    var barrierV:Sprite = new Sprite();

    barrierV.x = Math.random() * stage.stageHeight;
    barrierV.y = Math.random() * stage.stageWidth;
    barrierV.graphics.lineStyle(2, 0);
    barrierV.graphics.beginFill(0xFF0000);
    barrierV.graphics.drawRect(0, 0, 50, 50);
    barrierV.graphics.endFill();

    barriersV.push( autumTree2V );
    barriersV.push( autumTree1V );
    barriersV.push( autumTree3V );
    barriersV.push( rockV );
}
for(var i = 0; i < 500; i++)
{
    var barrierH:Sprite = new Sprite();

    barrierH.x = Math.random() * stage.stageHeight;
    barrierH.y = Math.random() * stage.stageWidth;
    barrierH.graphics.lineStyle(2, 0);
    barrierH.graphics.beginFill(0xFF0000);
    barrierH.graphics.drawRect(0, 0, 50, 50);
    barrierH.graphics.endFill();

    barriersH.push( H1 );
}
for(var j = 0; j < 500; j++)
{
    var sprite:Sprite = new Sprite();

    sprite.x = Math.random() * stage.stageHeight;
    sprite.y = Math.random() * stage.stageWidth;
    sprite.graphics.lineStyle(2, 0);
    sprite.graphics.beginFill(0xFF0000);
    sprite.graphics.drawRect(0, 0, 50, 50);
    sprite.graphics.endFill();

    sprites.push( mc );
    sprites.push( grass );
    sprites.push( grass2 );
    sprites.push( grass3 );
    sprites.push( grass4 );
    sprites.push( rock );
    sprites.push( autumTree1 );
    sprites.push( autumTree2 );
    sprites.push( autumTreeRed1 );
}

function sortView():void
{
    // Sort the content of 'sprites'.
    sprites.sortOn("y", Array.NUMERIC);

        for each(var j in sprites)
    {
        // Then add each to the display list in the new order.
        addChild(j);
    }
}
4

0 回答 0