A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [RESOLVED] Easing with buttons

  1. #1
    Senior Member
    Join Date
    Mar 2006
    Posts
    139

    resolved [RESOLVED] Easing with buttons

    Hello!

    I've written some basic code to move a ball to the mouses _x and _y position with easing.

    Code:
    onClipEvent (enterFrame) {
    	xp = _xmouse/10;
    	yp = _ymouse/10;
    	_x = _x+xp;
    	_y = _y+yp;	
    }
    Thing is, i'd like to control the ball by pressing on screen keys (up, down, left, right) instead of using the mouse position as this will be appearing on a touch screen without a keyboard or mouse.
    How do I go about doing this?
    I've attached my .fla so show what I mean.

    Thanks!
    Attached Files Attached Files

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    On your circle_mc, I have replaced the code for this code to give you basic movement, it should get you started
    PHP Code:
    /*
    onClipEvent (enterFrame) {
        xp = _xmouse / 10;
        yp = _ymouse / 10;
        _x = _x + xp;
        _y = _y + yp;
        if (_x < _root._xmouse)
        {
            gotoAndPlay(2);
        }
        else
        {
            gotoAndStop(1);
        }
        updateAfterEvent();
    }
    */

    // (load) just sets up the movement boundaries, it is optional
    onClipEvent (load) {
        var 
    left:Number 0;
        var 
    right:Number Stage.width this._width;
        var 
    top:Number 0;
        var 
    bot:Number Stage.height this._height;
    }

    onClipEvent (enterFrame) {
        if (
    Key.isDown(Key.UP))
        {
            if (
    this._y top)
            {
                
    this._y -= 5;
            }
        }
        if (
    Key.isDown(Key.DOWN))
        {
            if (
    this._y bot)
            {
                
    this._y += 5;
            }
        }
        if (
    Key.isDown(Key.LEFT))
        {
            if (
    this._x left)
            {
                
    this._x -= 5;
            }
        }
        if (
    Key.isDown(Key.RIGHT))
        {
            if (
    this._x right)
            {
                
    this._x += 5;
            }
        }


    I am not entirely sure if AS2 will work with touch screen.

  3. #3
    Senior Member
    Join Date
    Mar 2006
    Posts
    139
    Hi Fruitbeard!

    thanks for that, really appreciate the help!
    Unfortunately its not quite what i'm after, the script you wrote uses key presses, i'd managed to get that working, the trouble is there wont be a keyboard.
    I dont think i explained it clearly enough, I need it to work with on screen button instances used to control the up, down, left and right movement of the ball.

    The good news is AS2 does work with the touch screens we're using, they simply replace the x y coordinates of the mouse with those of the users finger on the screen!

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Ooops, my bad,

    Perhaps this might help you out some.

    https://code.google.com/p/ideo-multi...sActionScript2

  5. #5
    Senior Member
    Join Date
    Mar 2006
    Posts
    139
    Thanks for that,

    that's a bit advanced for me tbh!
    I settled on a simpler solution which seems to work, although i need to figure out the easing.

    (I've attached the .fla for anyone who might find it useful)

    Cheers!
    Attached Files Attached Files

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    with your newer method I don't think you will be able to do the easing as you are not giving it a place to ease to.
    All you are doing is telling it to move a certain amount continually until you let go of the button.

    I believe you would need a destination point for you to ease to and from.


    You might find it easier to try and keep your code off of the buttons and clips so you can get to it at a glance (no trying to find code, all seeable)

    you could use switch method too, you would need to name your buttons

    PHP Code:
    var buttonArray:Array = new Array(leftButtonrightButtonupButtondownButton);
    var 
    directions:Array = new Array("left""right""up""down");
    var 
    moving:String;

    for (var 
    i:Number 0buttonArray.lengthi++)
    {
        var 
    button:Object buttonArray[i];
        
    button.value directions[i];
        
    button.onPress = function()
        {
            
    moving this.value;
            
    enterFrameHandle();
        };
        
    button.onRelease = function()
        {
            
    moving "";
        };
    }

    function 
    enterFrameHandle()
    {
        if (
    moving != "")
        {
            
    onEnterFrame doMove;
        }
        else
        {
            
    delete onEnterFrame;
        }
    }

    function 
    doMove():Void
    {
        switch (
    moving)
        {
            case 
    "left" :
                
    ball_mc._x ball_mc._x -= 5;
                break;

            case 
    "right" :
                
    ball_mc._x ball_mc._x += 5;
                break;

            case 
    "up" :
                
    ball_mc._y ball_mc._y -= 5;
                break;

            case 
    "down" :
                
    ball_mc._y ball_mc._y += 5;
                break;

            default :
                
    ball_mc._x ball_mc._x;
                
    ball_mc._y ball_mc._y;
                break;
        }

    althoug I say again there is no easing with this method
    Last edited by fruitbeard; 01-14-2015 at 11:13 AM.

  7. #7
    Senior Member
    Join Date
    Mar 2006
    Posts
    139
    You're right of course,
    I was thinking of simulating easing by slowing down the speed at which the ball moves once you release the button until its speed = 0.
    Although this too is proving troublesome!

  8. #8
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    You're right, you need a speed variable, actually two of them which keep track of the X and Y speed increment of your movieclip. This way, you can keep increasing your ball's X and Y coordinates by those two speed variables. Here, I fixed your file in accordance with your current code (although, I believe this could been in a way better method, but I don't know how much AS2 you know ) -- hopefully this is what you wanted?

    If you need help understanding any part of the code, feel free to ask and I'll give you an explanation Also, your overuse of enterFrame events cause a few "bugs" with the ball's movement (like, if you press and hold UP, and then let go and press and hold RIGHT, the ball will start moving diagonal and never really "recover" from the UP movement) -- if you want to fix this, the code will have to be rewritten from scratch, so just ask and we'll try our best to help you

    NEW FIXED FILE
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  9. #9
    Senior Member
    Join Date
    Mar 2006
    Posts
    139
    Thanks Nig!
    I see what you mean about the bugs... this is basically what i was after though yeah!


    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center