0

我正在制作一个平台游戏,我必须在两个数组(whiteBlocks 和 blackBlocks)之间的角色碰撞以及另一个在按钮碰撞时激活并计时的数组(redBlocks)之间切换。

但是,我使用 hitTest 代码来确保我可以同时使用 redBlocks 以及 whiteBlocks 或 blackBlocks 作为平台时遇到了问题。如果有人可以帮助我在触发计时器后让 redBlocks 持续激活并且通过在 blackBlocks 或 whiteBlocks 之间切换无效,我正在徘徊。

如果您希望我以更清晰的方式解释我的问题,我会这样做。请注意,我对 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;



    }*/





} 
}
4

1 回答 1

0

与其将 activeArray 设为对 whiteBlocks 或 blackBlocks 的引用,不如将其设为单独的 Array 对象。然后,在 toggleBlocks 中,您可以

  1. 从 activeArray 中删除所有元素
  2. 将 redBlocks 中的所有元素添加到 activeArray(根据当前状态可选)
  3. 将 whiteBlocks 或 blackBlocks 中的所有元素添加到 activeArray

这样,activeArray 将具有所有红色块 AND(所有 whiteBlocks 或所有 blackBlocks)。

于 2012-12-27T20:17:02.947 回答