jeudi 9 février 2017

AS3 - How to Cycle States of Character Animations (moving & stopped)

I have a D-Pad for my mobile game, when it's pressed the character should move, the animation should swap from State-1 to State-2, and keeps repeating.

I do not know how to have the stationary animation switch to movement, then after a delay so the user can see it, it switches to stationary, back to movement.

This process should keep happening until the user lets go of the UP D-Pad or hits the stage collision barriers I have set up.

// NOTE: 1 means standing still, 2 means moving in that direction
var down1_anim:MovieClip = new anim_Down1;
var down2_anim:MovieClip = new anim_Down2;
var left1_anim:MovieClip = new anim_Left1;
var left2_anim:MovieClip = new anim_Left2;
var right1_anim:MovieClip = new anim_Right1;
var right2_anim:MovieClip = new anim_Right2;
var up1_anim:MovieClip = new anim_Up1;
var up2_anim:MovieClip = new anim_Up2;


function moveUpTouchDOWN(e:TouchEvent):void
{
    stage.addEventListener(TouchEvent.TOUCH_END,moveUpTouchUP); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
    addEventListener(Event.ENTER_FRAME,moveUp); //while the mouse is down, run the tick function once every frame as per the project frame rate
}

function moveUpTouchUP(e:TouchEvent):void
{
    removeEventListener(Event.ENTER_FRAME,moveUp);
    stage.removeEventListener(TouchEvent.TOUCH_END,moveUpTouchDOWN);
    // when FINGER LIFTED play up1_anim (stop movement animation)
    if(up2_anim.stage)
    {
        removeChild(up2_anim)
        addChild(up1_anim)
        up1_anim.x = charPosKeeper_mc.x
        up1_anim.y = charPosKeeper_mc.y
    }
    else if(up1_anim.stage)
    {
        // IN NO CASE, SHOULD IT REACH THIS, IF IT DOES, THAT'S A BUG.
        // not sure why i have this then hm
        removeChild(up1_anim)
    }

}

function moveUp(e:Event):void
{
    if(area1 == true)
    {
        if(charPosKeeper_mc.hitTestObject(barrierRoof1_game))
        {
            //maybe play a collision sound here?
        }
        else if(charPosKeeper_mc.hitTestObject(barrierRoof2_game))
        {
            charPosKeeper_mc.y = charPosKeeper_mc.y - walkspeed;
        }
        else{
            if(up1_anim.stage)
            {
                removeChild(up1_anim)
                charPosKeeper_mc.y = charPosKeeper_mc.y - walkspeed;
                addChild(up2_anim);
                up2_anim.x = charPosKeeper_mc.x
                up2_anim.y = charPosKeeper_mc.y
            }
            else{
                charPosKeeper_mc.y = charPosKeeper_mc.y - walkspeed;
                addChild(up2_anim);
                up2_anim.x = charPosKeeper_mc.x
                up2_anim.y = charPosKeeper_mc.y
            }
        }
    }
    // This else statement is a short term fix, when you add more regions
    // this else becomes else if checking if your in area2,area3,etc.
    else{
    }
}

Aucun commentaire:

Enregistrer un commentaire