Actionscript 如何在两个阵列之间进行测试,同时在这些阵列内的movieclips之间进行切换?

Actionscript 如何在两个阵列之间进行测试,同时在这些阵列内的movieclips之间进行切换?,actionscript,Actionscript,我正在制作一个平台游戏,其中我必须在两个数组(白块和黑块)和另一个数组(红块)之间切换角色碰撞,该数组在按钮碰撞时激活并计时 然而,我在使用hitTest代码时遇到了问题,我用它来确保我可以将redBlocks用作平台,同时也可以使用whiteBlocks或blackBlocks。我在想,一旦定时器被触发,是否有人能帮助我让红方块一直处于活动状态,并且在黑方块或白方块之间切换是无效的 如果你想让我更清楚地解释我的问题,我会做的。请注意,我对actionscript和编码一般来说都是新手,因此,对

我正在制作一个平台游戏,其中我必须在两个数组(白块和黑块)和另一个数组(红块)之间切换角色碰撞,该数组在按钮碰撞时激活并计时

然而,我在使用hitTest代码时遇到了问题,我用它来确保我可以将redBlocks用作平台,同时也可以使用whiteBlocks或blackBlocks。我在想,一旦定时器被触发,是否有人能帮助我让红方块一直处于活动状态,并且在黑方块或白方块之间切换是无效的

如果你想让我更清楚地解释我的问题,我会做的。请注意,我对actionscript和编码一般来说都是新手,因此,对于我可能犯的错误,我深表歉意。我感谢您能提供的任何帮助,因为我已经解决了这一问题一段时间了

import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.events.Event;
import flash.display.MovieClip
import flash.display.DisplayObject;


//PLAYER MOVEMENT
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyLIST);
stage.addEventListener(KeyboardEvent.KEY_UP, keyLIST);

manMC.positionPNT = new Point (manMC.x, manMC.y);

//track using loop and keypresses, not just keyEvent
var leftKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;

function keyLIST (ke:KeyboardEvent) {

    //trace(ke.toString() );

    //1. work out where we're going from where we are now

    //characterPNT = new Point(manMC.x, manMC.y);

        //2. offset our destination point
        switch (ke.keyCode) {

        //LEFT
        case 37:

            trace(ke.type);
            leftKeyDown = (ke.type == "keyDown");

        break;

        //RIGHT
        case 39:

            //moveCharacter(manMC, "right");
            rightKeyDown = (ke.type == "keyDown");

        break;

        //87UP, 83DOWN

        //UP
        case 38:

            trace("true");

            manMC.jump();

        break;

        //DOWN
        case 83:



        break;

        default:

            //trace ("this key does nothing");

        break;
    }// close switch

}// end function keyLIST

function movementCallback (ev:Event):void {

    if (leftKeyDown == true) {

        moveCharacter(manMC, "left");

    }

    if (rightKeyDown == true) {

        moveCharacter(manMC, "right");
    }
}

addEventListener (Event.ENTER_FRAME, movementCallback);

//a new movement function
function moveCharacter(char:character, dir:String ):void {//datatype as void as it returns nothing

    //will need this
    //var characterPNT:Point; //moved to character class
    //maMC.positionPNT = new Point( manMC.x, manMC.y);

    //copy current poistion before offsetting with movement, speed and direction
    manMC.destinationPNT = manMC.positionPNT.clone();
    //characterPNT = new Point(manMC.x, manMC.y);

    //var colliding:Boolean;

    //do multiple collision detection here

    var offsetPNT:Point;//detecting the destination of the body points

    switch (dir) {

        case "left":

            //move PNT to left
            manMC.destinationPNT.x -= manMC.speedNUM;
            offsetPNT = manMC.leftarmPNT;

        break;

        case "right":

            //move PNT to right
            manMC.destinationPNT.x += manMC.speedNUM;
            offsetPNT = manMC.rightarmPNT;



        break;
    }

    //set to true of false by the function that returns a Boolean value
    var colliding:Boolean = multipleHitTest (manMC.positionPNT, manMC.destinationPNT, offsetPNT, activeArray);

    //trace(colliding);
    //trace ("moveMode: " + manMC.moveModeSTR);

    if (!colliding /*&& manMC.moveModeSTR != "falling"*/) { //may be more complex to control player movement when falling&jumping

        manMC.moveToPoint( manMC.destinationPNT );

        //manMC.x = manMC.destinationPNT.x;
        //manMC.y = manMC.destinationPNT.y;

        //always update position PNT when character is moved
        //manMC.positionPNT = manMC.destinationPNT.clone();//copies destination point 
    }
}


//Apply gravity at all times using a loop/callback
addEventListener(Event.ENTER_FRAME, gravityFUNC);

var gravityNUM:Number = new Number(10);

function gravityFUNC (ev:Event) {

    var falling:Boolean;

    var gravityPNT:Point = manMC.positionPNT.clone();
    //trace(manMC.positionPNT.y);

    gravityPNT.y += gravityNUM;
    //trace(gravityPNT.y);

    //EXPERIMENTAL
    gravityPNT.y -= manMC.verticalVelocityNUM;

    //decaying gravity, caping it at 0 to avoid negatives
    manMC.verticalVelocityNUM = Math.max ( 0, manMC.verticalVelocityNUM - gravityNUM);


    falling = !multipleHitTest (manMC.positionPNT, gravityPNT, manMC.feetPNT, activeArray);
    //trace("falling " + falling);

    if (falling == true) {
        //manMC.y = gravityPNT.y;
        manMC.moveToPoint(gravityPNT);

        //either jumping or falling
        if ( manMC.verticalVelocityNUM == 0) {

            manMC.moveModeSTR = "falling";

        }else {

            manMC.moveModeSTR = "jumping";
        }

    } else {

        manMC.moveModeSTR = "walking";

    }
}

//======================================================================



//======================================================================


//add when declared
//varobstacles:Array = new Array (block0MC, block1MC, block2MC);

//declared and then new instance
//var obstacles:Array;
//obstacles = new Array (block6MC);

//declares and create empty array
/*var obstacles:Array = new Array();
//push adds an item to the front of an array
obstacles.push (block0MC); // add instance names of display objects (or other data)
obstacles.push (block1MC);
obstacles.push (block2MC);
obstacles.push (block3MC);*/


//trace("length of list" + obstacles.length);
//trace(obstacles[0]); //access first element of array

//trace( block0MC["x"] );// acessing x property using different method

function multipleHitTest( position:Point, destination:Point, offset:Point, targets:Array):Boolean { //these are ARGUMENTS

    //track hittest true or false
    var returnBOOL:Boolean = new Boolean (false);

    // cap length of loop - ie.e how many iterations?
    var limit:int = new int ( targets.length );// obstacles.length is 3 items long


    //the "counter", increases or decreases each time
    var i:int;

    //chunks =
    //start counter at 0;
    // loop while counter is less than limit;
    // increment counter by 1 each looop;

    for( i=0; i<limit; i++) {


        //will access each item in array, as "i" is an integer
        //obstacles[1];

        //because it's targeted as a movieclip we can ask it's name

        //this is 'reference variable'
        //we are creating an 'alias' of the item in the list
        var testAgainstObject:DisplayObject = targets[i];



        //track direction
        var moveDirection:String;

        //only hit test things we're moving towards...
        if (position.x < destination.x) { //if we're moving right 

            moveDirection = new String( "right" );

        } else if (position.x > destination.x) {//else if we're moving left

            moveDirection = new String( "left" );

        }

        //
        if(

           (moveDirection == "right" && targets[i].x >= position.x && destination.x >= targets[i].x)
           ||//or
           (moveDirection == "left" && targets[i].x <= position.x && destination.x <= (targets[i].x + targets[i].width) )

           ) {//obstacle is to the right

            //  obstacle moving right
            // moving right

        }

        //create a copy of 'destination' 
        var offsetDestination:Point = destination.clone();

        //apply our offset provided by our character limbs
        offsetDestination.offset(offset.x, offset.y);

        //if point is colliding with list item
        //if( testAgainstObject.hitTestPoint (destination.x, destination.y) ) { //REMOVED FOR TESTING
        if( testAgainstObject.hitTestPoint (offsetDestination.x, offsetDestination.y) ) {

            //trace("collisiondetected " + targets[i].name);
            returnBOOL = true;

        } else {
            //trace("no collision");

            //do nothing if flase, as it would contradict a 'true' value set earlier in the loop

        }

    }

    return (returnBOOL); //tesing only
}


//declares and create empty array
var blackBlocks:Array = new Array();
//push adds an item to the front of an array
blackBlocks.push (block0MC); // add instance names of display objects (or other data)
blackBlocks.push (block1MC);
blackBlocks.push (blackbarrier);
blackBlocks.push (blackbarrier2);
blackBlocks.push (blackbarrier3);
blackBlocks.push (blackbarrier4);
blackBlocks.push (blackbarrier5);



var whiteBlocks:Array = new Array();
//push adds an item to the front of an array
whiteBlocks.push (block2MC);
whiteBlocks.push (block3MC);
whiteBlocks.push (whitebarrier);
whiteBlocks.push (whitebarrier2);
whiteBlocks.push (whitebarrier3);
whiteBlocks.push (whitebarrier4);
whiteBlocks.push (whitebarrier5);

var redBlocks:Array = new Array();
redBlocks.push (redblock1MC);



//var activeArray:Array = new Array (blackBlocks);

var activeArray:Array = blackBlocks;
//active.push (redblock1MC);



//Adds an event listener to the button component with the mouse click event.
//hide_btn.addEventListener(MouseEvent.CLICK, toggleBlocks);
//show_btn.addEventListener(MouseEvent.CLICK, showObject);
stage.addEventListener(KeyboardEvent.KEY_DOWN, toggleBlocks);



// start toggle blocks
function toggleBlocks (event:KeyboardEvent):void {
    var i:int = 0;
    var lim:int = activeArray.length;

    if(event.keyCode == Keyboard.SPACE){
    trace("Toggle Blocks");

    blocksVisibility( activeArray , false );

    if( activeArray == blackBlocks) { 
        activeArray = whiteBlocks;

    }else{ 

    activeArray = blackBlocks;



    }

    blocksVisibility( activeArray , true );

} // end IF



} // end toggle blocks

function blocksVisibility( arrARG:Array , visBOOL:Boolean ){

    var i:int = 0;
    var lim:int = arrARG.length;

    for( i=0; i<lim; i++) {
        arrARG[i].visible = visBOOL;
    }

}

blocksVisibility( this.whiteBlocks , false );
blocksVisibility( this.redBlocks , false );

//blocksVisibility( this.redBlocks , false );


//======== red block button ========


// on collision trigger button and make red platform appear

/*
var myTimer:Timer = new Timer(5000,1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);

function timerListener(e:TimerEvent):void {
    //logo_mc.x+=40;
    blocksVisibility( this.redBlocks , true );
    //redBlock1MC:Array = true;

    //activeArray:Array = redBlocks;
}

myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
function onComplete(e:TimerEvent):void {
    //logo_mc.alpha=10;
    //logo_mc.x=20;
    blocksVisibility( this.redBlocks , false );
    //redBlock1MC:Array = false;

    //activeArray:Array = blackBlocks;
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, onStart);

function onStart(e:KeyboardEvent):void {
    if (e.keyCode == 88){

    blocksVisibility( this.redBlocks , true );
    //redBlock1MC:Array = true;

    myTimer.start();
    //logo_mc.alpha=.1;
    //logo_mc.x=20;
}
} */





var myTimer:Timer = new Timer(10000, 1); // 1 second

myTimer.addEventListener(TimerEvent.TIMER, timedPlatform);
//myTimer.start();

function timedPlatform(event:TimerEvent):void {


trace("timedPlatform() called @ " + getTimer() + " ms");


blocksVisibility( this.redBlocks , false );

/*if (manMC.hitTestObject(redButton)){


trace("Start Timer");
myTimer.start();


blocksVisibility( this.redBlocks , true );

        activeArray = redBlocks;


} */
}



redButton.addEventListener(Event.ENTER_FRAME, startTimer);
function startTimer(event:Event):void{
//if (e.keyCode == 88){

//if (manMC, hitTest(redButton)) {

if (manMC.hitTestObject(redButton)) {


trace("Start Timer");
myTimer.start();


blocksVisibility( this.redBlocks , true );

activeArray = blackBlocks;
//activeArray = blackBlocks;



/*if( activeArray == blackBlocks) { 
        activeArray = redBlocks;
                activeArray = blackBlocks;


    }else{ 

    activeArray = blackBlocks;



    }*/





} 
}
导入flash.events.KeyboardEvent;
导入flash.geom.Point;
导入flash.events.Event;
导入flash.display.MovieClip
导入flash.display.DisplayObject;
//球员运动
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyLIST);
stage.addEventListener(KeyboardEvent.KEY\u UP,keyLIST);
manMC.positionPNT=新点(manMC.x,manMC.y);
//使用循环和按键进行跟踪,而不仅仅是keyEvent
var leftKeyDown:Boolean=false;
var rightKeyDown:Boolean=false;
功能键列表(ke:KeyboardEvent){
//trace(比如toString());
//1.从现在的位置找出我们要去的地方
//characterPNT=新点(manMC.x,manMC.y);
//2.抵消我们的目的地
开关(ke.keyCode){
//左
案例37:
痕迹(ke型);
leftKeyDown=(ke.type==“keyDown”);
打破
//对
案例39:
//移动字符(manMC,“右”);
rightKeyDown=(ke.type=“keyDown”);
打破
//87上,83下
//向上
案例38:
痕迹(“真实”);
manMC.jump();
打破
//向下
案例83:
打破
违约:
//跟踪(“此键不起任何作用”);
打破
}//闭合开关
}//结束函数键列表
函数movementCallback(ev:Event):void{
if(leftKeyDown==true){
移动字符(manMC,“左”);
}
如果(rightKeyDown==true){
移动字符(manMC,“右”);
}
}
addEventListener(Event.ENTER_FRAME,movementCallback);
//一种新的运动函数
函数moveCharacter(char:character,dir:String):void{//datatype为void,因为它不返回任何内容
//我需要这个
//var characterPNT:Point;//移动到字符类
//maMC.positionPNT=新点(manMC.x,manMC.y);
//在移动、速度和方向偏移之前复制当前位置
manMC.destinationPNT=manMC.positionPNT.clone();
//characterPNT=新点(manMC.x,manMC.y);
//var冲突:布尔;
//在这里进行多重碰撞检测
var offsetPNT:Point;//检测主体点的目标
交换机(dir){
案例“左”:
//向左移动PNT
manMC.destinationPNT.x-=manMC.speedNUM;
offsetPNT=manMC.leftarmPNT;
打破
案例“权利”:
//向右移动PNT
manMC.destinationPNT.x+=manMC.speedNUM;
offsetPNT=manMC.rightarmPNT;
打破
}
//由返回布尔值的函数设置为true或false
var冲突:布尔值=多比特测试(manMC.positionPNT、manMC.destinationPNT、offsetPNT、activeArray);
//痕迹(碰撞);
//跟踪(“移动模式:+manMC.MOVER”);
如果(!collisding/*&&manMC.movemodester!=“falling”*/){//在控制玩家下落和跳跃时的移动可能更复杂
manMC.moveToPoint(manMC.destinationPNT);
//manMC.x=manMC.destinationPNT.x;
//manMC.y=manMC.destinationPNT.y;
//移动角色时始终更新位置PNT
//manMC.positionPNT=manMC.destinationPNT.clone();//复制目标点
}
}
//始终使用循环/回调应用重力
addEventListener(Event.ENTER_FRAME,gravityFUNC);
var gravityNUM:编号=新编号(10);
函数gravityFUNC(ev:事件){
var:布尔型;
var gravityPNT:Point=manMC.positionPNT.clone();
//跟踪(手动位置PNT.y);
重力Nt.y+=重力线;
//微量元素(重力nt.y);
//实验性
重力Nt.y-=垂直垂直垂直的manMC.verticalVelocityNUM;
//衰减重力,将其上限设置为0以避免负片
manMC.verticalVelocityNUM=Math.max(0,manMC.verticalVelocityNUM-gravityNUM);
下降=!多重测试(manMC.positionPNT、gravityPNT、manMC.feetPNT、activeArray);
//痕迹(“下落”+下落);
如果(下降==真){
//manMC.y=重力nt.y;
移动点(重力点);
//不是跳就是跌
如果(manMC.verticalVelocityNUM==0){
manMC.mover=“坠落”;
}否则{
manMC.mover=“跳跃”;
}
}否则{
manMC.mover=“行走”;
}
}
//======================================================================
//======================================================================
//声明时添加
//VAR:阵列=新阵列(block0MC、block1MC、block2MC);
//声明,然后是新实例
//变量:数组;
//障碍物=新阵列(block6MC);
//声明并创建空数组
/*变量障碍:数组=新数组();
//推送将项目添加到数组的前面
障碍物。推(块0MC);//添加显示对象(或其他数据)的实例名称
障碍物。推(块1MC);
障碍物。推(block2MC);
障碍物。推(块3MC)*/
//跟踪(“列表长度”+障碍物长度);
//跟踪(障碍[0])//访问数组的第一个元素
//跟踪(块0MC[“x”]);//使用不同的方法访问x属性
函数多重测试(位置:点,目标:点,偏移:点