0

I have two buttons that moves a movieclip up/down the Y-axis. How do I get the movieclip to stop at a certain height, and still be able to move it in the opposite direction?

I need this to work like this:

When you press the down-button, the movieclip goes downwards, but not past Y=500. When you press the up-button, the movieclip goes upwards, but not past Y= 300.

I get the movieclip to stop at the correct point (500), but when it reaches this point it's stuck.. so it won't go upwards again if I press the up-button.

can someone help me please?:)

here's my code so far:

1) decrease = the down-button
2) increase = up-button
3) stempel = the movieclip I want to stop on the Y-axis



var moveStempel = 0;

decrease.addEventListener(MouseEvent.MOUSE_DOWN, decreasePressed);  
decrease.addEventListener(MouseEvent.MOUSE_UP, removeEnterFrame);

increase.addEventListener(MouseEvent.MOUSE_DOWN, increasePressed); 
increase.addEventListener(MouseEvent.MOUSE_UP, removeEnterFrame);

function decreasePressed(e:MouseEvent):void  
{   
    moveStempel = 2;
    addEnterFrame();
}


function increasePressed(e:MouseEvent):void  
{   
    moveStempel = -2;
    addEnterFrame();
}




// ADD ENTER FRAME 
function addEnterFrame():void 
{     
    this.addEventListener(Event.ENTER_FRAME, update); 
}  

function removeEnterFrame(e:MouseEvent):void 
{     
    this.removeEventListener(Event.ENTER_FRAME, update); 
}  

function update(e:Event):void 
{     
    if (stempel.y < 500)
    {
        stempel.y += moveStempel;
        trace("inside");
    }

    else if (stempel.y > 500)
    {
        stempel.stop();
        trace("outside");
    }
agamesh
  • 559
  • 1
  • 10
  • 26
Cat
  • 29
  • 6
  • 1
    Honestly, I have very little time to devote to such questions, and you need to put in a bit of effort yourself (descriptive method names, variable names that are legible in the common language of the forum) if I am going to quickly scan such a large block of code to try to see your problem. Maybe someone else has the time to look at your code and decipher it. – Amy Blankenship Jun 11 '12 at 12:27
  • Based on what you're describing, I'd suggest having the vertical movement of the object occur inside of another movieclip, and then that parent movieclip be what is moved horizontally. Just a suggestion... – CodeMouse92 Jun 14 '12 at 03:04

1 Answers1

0

In your code right now, once stempel has a y value greater than 500, it will never be able to move again.

There are two conditions when stempel can move: if it's moving down, it can't be at the bottom of the allowed area, and if it's moving up, it can't be at the top of the allowed area.

if ((stempel.y < 500 && moveStempel == 2) || (stempel.y > 300 && moveStempel == -2))
{
    stempel.y += moveStempel;
    trace("inside");
}
else
{
    stempel.stop();
    trace("outside");
} 
CheeseWarlock
  • 1,361
  • 1
  • 10
  • 11
  • Hi! thanks for answering:) but I still have problems getting it to work after the movieclip is stopped at 500. I've tried with hitTestObject and making barriers with different scripts, but still no proper solution.. any other suggestions? – Cat Jun 12 '12 at 08:06
  • What happens if you remove `stempel.stop();`? It's not needed to prevent `stempel` from moving too far. If that's not the problem, then the problem is most likely somewhere outside the code you posted. – CheeseWarlock Jun 12 '12 at 15:32