flash AS3重用实例

时间:2013-07-03 10:52:31

标签: actionscript-3 instance flash-cs6 isometric

我一直在尝试在AS3中制作等距游戏。我正在使用一个movieclip来阻止角色移动通过树之类的对象,但我必须继续为movieclip提供一个新的实例名称,用于我使用它的每个树,然后将其添加到名为barriersV的数组中。

我想做的只是将一个movieclip添加到数组并以某种方式重用它,但据我所知,你不能使用一个实例名称不止一件事,只有最新的一个可以工作。是否有更好的方法来获得相同的结果?

继承我的所有代码:

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);
    }
}

0 个答案:

没有答案