views:

108

answers:

1

So, I'm working on the basics of Actionscript 3; making games and such. I designed a little space where everything is based on location of boundaries, using pixel-by-pixel movement, etc.

So far, my guy can push a box around, and stops when running into the border, or when try to the push the box when it's against the border.

So, next, I wanted to make it so when I bumped into the other box, it shot forward; a small jump sideways.

I attempted to use this (foolishly) at first:

// When right and left borders collide.    
if( (box1.x + box1.width/2) == (box2.x - box2.width/2) ) {

    // Nine times through
    for (var a:int = 1; a < 10; a++) {

        // Adds 1, 2, 3, 4, 5, 4, 3, 2, 1.
        if (a <= 5) {
            box2.x += a; }
        else {
            box2.x += a - (a - 5)*2 } } }

Though, using this in the function I had for the movement (constantly checking for keys up, etc) does this all at once. Where should I start going about a frame-by-frame movement like that? Further more, it's not actually frames in the scene, just in the movement.

This is a massive pile of garbage, I apologize, but any help would be appreciated.

A: 

try doing something like: (note ev.target is the box that you assigned the listener to)

var boxJumpDistance:Number = 0;

function jumpBox(ev:Event){
    if (boxJumpDistance<= 5) {
        ev.target.x += boxJumpDistance; }
    else if(boxJumpDistance<=10){
        ev.target.x += boxJumpDistance - (boxJumpDistance - 5)*2 
    }
    else{
        boxJumpDistance = 0;
        ev.target.removeEventListener(Event.ENTER_FRAME, jumpBox);
    }
}

then instead of running the loop, just add a listener:

box2.addEventListener(Event.ENTER_FRAME, jumpBox);

although this at the moment only works for a single box at a time (as it is only using one tracking variable for the speed), what you would really want to do is have that function internally to the box class, but im unsure how your structure goes. the other option would be to make an array for the boxes movement perhaps? loop through the array every frame. boxesMoveArray[1] >=5 for box 1, etc.

shortstick
How do I call that exactly?Like, in the nested if loop that checks for right side collisions, how would I call it to happen once?
Befall
the Event.ENTER_FRAME function is called once per frame for each object with a listener. to start the sequence through, just add the if loop to the conditions of adding the event listener, then it will only start when they collide. (replace the code from '//Nine times over' with the addEventListener() call)
shortstick